简体   繁体   English

Docker Java Image无法连接到Docker Mongo

[英]Docker Java Image cannot connect to Docker Mongo

I'm getting timeout errors from my Java application (Spring Boot) that uses a MongoDB, the connection works when I run the jar, however when it is Dockerized, the connection times out. 我从使用MongoDB的Java应用程序(春季启动)中收到超时错误,当我运行jar时,该连接有效,但是当它被Dockerized时,连接超时。 I'm not sure if I'm just not configuring something correctly with Docker? 我不确定是否只是在Docker中配置不正确?

If your dockerized Spring Boot app is using 'localhost' to try to connect to the dockerized Mongo instance, it will fail because 'localhost' in that context refers to the container (the one running the Spring Boot app, which is not running Mongo). 如果您的dockerized的Spring Boot应用程序正在使用'localhost'尝试连接到dockerized的Mongo实例,它将失败,因为该上下文中的'localhost'引用了该容器(运行Spring Boot应用程序的那个容器,而不是运行Mongo的那个容器) 。

If the name of your Mongo container is 'mongo' you can find the docker network IP address of that container with 如果您的Mongo容器名称为'mongo',则可以使用以下命令找到该容器的docker网络IP地址

$ docker inspect mongo | grep IPAddress

Then you should be able to configure your Spring Boot container to connect to mongo at that address. 然后,您应该能够配置Spring Boot容器以在该地址连接到mongo。

使用--link run选项可以使用指定的主机名轻松连接到mongo容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM