Docker中的SpringBoot无法连接到Docker中的Mongo [英] SpringBoot in Docker not connecting to Mongo in Docker
问题描述
我有一个Spring Boot应用程序,并使用在brew服务中运行的mongo db开发了它.
I have a Spring Boot Application and developed it with a mongo db which was running in brew services.
要连接到数据库,我只需要在Spring Boot中将以下内容放到application.properties中即可
To get a connection to the db I just had to put the following into application.properties in Spring Boot
spring.data.mongodb.uri=mongodb://localhost:27017/db
将应用程序属性更改为
spring.data.mongodb.uri=mongodb://mongo:27017/db
没有做任何更改,与以前相同的错误.
didtn't change anything, same Error as before.
现在,我正在尝试将SpringBoot应用程序和MongoDB放入Docker-Containers中,但是无法使任何连接正常工作.
Now I'm trying to put the SpringBoot Application and the MongoDB into Docker-Containers, but cant get any connection working.
这是我在SpringBoot应用程序中的Dockerfile:
So this is my Dockerfile in the SpringBoot Application:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ADD /build/libs/dg-0.0.1-SNAPSHOT.jar dg-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]
这是我的Docker-Compose.yml文件:
This is my Docker-Compose.yml file:
version: '3'
services:
mongo:
container_name: docker-mongo
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./data/db:/data/db
spring:
depends_on:
- mongo
image:
docker-spring-http-alpine
ports:
- "8080:8080"
links:
- mongo
使用
docker-compose up
我收到以下错误:(这是实际的错误消息)
I get the following error: (this is the actual error message )
2019-07-08 23:10:19.990 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar!/:na]
... 3 common frames omitted
有人知道这里出了什么问题吗? 在开发环境中,它就像一种魅力.
Does someone know whats the problem here? In development environment it works like a charm.
预先感谢
将以下行添加到Dockerfile
Adding the following line to the Dockerfile
"-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg"
像这样进入Entrypoint解决了连接问题
into Entrypoint like this solved the connection issue
ENTRYPOINT ["java", "-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]
我能够在Dockerfile的Entrypoint中获得没有上面提到的行的数据库连接.我想这是如果您想通过链接"连接数据库
I was able to get connection to the db without the mentioned line above in the Entrypoint in the Dockerfile. I guess this is if you like to connect your db over "links"
但是现在我可以通过网络连接了,这是我的代码:
But now I was able to connect over Network, this is my code:
version: '3.6'
services:
mongo:
container_name: docker_mongo
networks:
- gateway
ports:
- "27017:27017"
hostname: mongo
image: mongo:latest
volumes:
- ./data/db:/data/db
spring:
container_name: docker-spring
networks:
- gateway
ports:
- "8080:8080"
hostname: spring
depends_on:
- mongo
image: dg-docker-spring-http-alpine-j
networks:
gateway:
driver: "bridge"
以及应用程序属性中的以下内容
and the following in application properties
spring.data.mongodb.host=docker_mongo
spring.data.mongodb.port=27017
spring.data.mongodb.database=db
因此,该连接现在似乎正在通过网络工作. 相同的代码不适用于3.0版
So it looks like the connection is working over Network now. The same code did not work with Version 3.0
要防止SpringBoot通过本地主机自动连接到mongo,还必须排除MongoAutoConfiguration!
To prevent SpringBoot to connect automatically to mongo over localhost it's also necessary to exclude MongoAutoConfiguration!
@SpringBootApplication(exclude={MongoAutoConfiguration.class})
谢谢大家的帮助
推荐答案
我从未做过spring-boot开发,但是您要显示的错误很可能与mongo问题无关.但是,以下是有关mongo连接失败的原因的说明:
I've never done spring-boot development, but the error you are saying is being displayed may very well be unrelated to the mongo issue. However, here is an explanation as to why your mongo-connection is failing:
docker-compose
会创建一个虚拟网络(例如您的情况).
docker-compose
creates a virtual network if one hasn't been specified in the file (like in your case).
所有应用程序都在此网络内运行,彼此完全隔离.因此,spring-boot容器中的localhost
实际上是指向自身的.这意味着您的spring-boot应用程序期望mongo
实例在其容器内运行(不是,它在另一个容器中).
All your applications run inside of this network, completely isolated from each other. As such, localhost
in your spring-boot container actually refers to itself. Meaning your spring-boot application is expecting the mongo
instance to be running inside of its container (which its not, it's in a different container).
当便携式计算机的网络上同时运行数据库和应用程序时,这会很好.但是如上所述,它们现在完全在docker-compose
网络中运行.
This would have been fine when both the database and application was running on your laptop's network. But as mentioned, they are now running in the docker-compose
network, in complete isolation.
但是,docker-compose
真的很聪明!它为每个容器创建一个DNS,该DNS使用在docker-compose
文件中指定的服务名称(在您的情况下为mongo
和spring
),以便轻松访问网络内部的容器.
However, docker-compose
is really clever! It creates a DNS for each of your containers which uses the service-name (in your case mongo
and spring
) specified in your docker-compose
file to allow for easy access to the containers inside of the network.
因此,您应该能够将spring.data.mongodb.uri=mongodb://localhost:27017/db
更改为spring.data.mongodb.uri=mongodb://mongo:27017/db
,这应该允许它进行连接.
So, you should be able to change spring.data.mongodb.uri=mongodb://localhost:27017/db
to spring.data.mongodb.uri=mongodb://mongo:27017/db
and that should allow it to connect.
这篇关于Docker中的SpringBoot无法连接到Docker中的Mongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!