Springboot 应用程序无法使用 docker-compose 连接到 MongoDB 容器
Posted
技术标签:
【中文标题】Springboot 应用程序无法使用 docker-compose 连接到 MongoDB 容器【英文标题】:Springboot app cannot connect to MongoDB container with docker-compose 【发布时间】:2021-10-25 14:56:13 【问题描述】:老实说,我已经与这个问题作斗争了几个星期,但仍然找不到我做错了什么。
我有一个基本的 springboot 服务应该连接 MongoDB 容器。当我在本地运行服务时(没有 Dockerised),它工作正常,但如果我在容器中运行服务和 MongoDB,我会得到 MongoSocketOpenException 并拒绝连接。
这是我的 Dockerfile
FROM openjdk:14-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY $JAR_FILE app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
version: '3.8'
services:
idv-service:
image: idv-service-img
container_name: idv-service
build: .
hostname: idvservice
#restart: on-failure
ports:
- 8091:8091
depends_on:
- mongodb
networks:
- gateway
mongodb:
image: mongo:5.0
hostname: mongodb
container_name: idv-mongodb
restart: unless-stopped
ports:
- 27011:27011
volumes:
- dbdata:/data/db
networks:
- gateway
command: --bind_ip 0.0.0.0
volumes:
dbdata:
networks:
gateway:
driver: bridge
最后是 application.properties 文件
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27011
spring.data.mongodb.database=ttm-idv-db
来自容器的日志
idv-service |
idv-service | com.mongodb.MongoSocketOpenException: Exception opening socket
idv-service | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
idv-service | Caused by: java.net.ConnectException: Connection refused
idv-service | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
idv-service | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:589) ~[na:na]
idv-service | at java.base/sun.nio.ch.NiosocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
idv-service | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
idv-service | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
idv-service | at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
idv-service | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.2.3.jar!/:na]
idv-service | ... 4 common frames omitted
idv-service |
idv-service | 2021-08-25 12:38:00.425 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
idv-service | 2021-08-25 12:38:00.440 INFO 1 --- [ main] c.m.t.a.idv.IdvServiceApplication : Started IdvServiceApplication in 3.433 seconds (JVM running for 4.093)
关于我可能做错了什么的任何线索?
Docker 引擎:20.10.8 编写:1.29.2
【问题讨论】:
【参考方案1】:您的端口映射不正确。 Mongo容器默认会监听27017,所以当你在Docker主机上发布27011端口时,需要将27011主机端口映射到27017容器端口,即
ports:
- 27011:27017
【讨论】:
【参考方案2】:嗯,看来我找到了解决方法
mongodb:
image: mongo:5.0
hostname: mongodb
container_name: idv-mongodb
restart: unless-stopped
#network_mode: "bridge"
# environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: 1234
ports:
- 27011:27011
volumes:
- dbdata:/data/db
# command: --bind_ip 0.0.0.0
command: mongod --port 27011
最后一行“command: mongod --port 27011”显然强制 MongoDB 开始侦听不同的端口。我的想法是它是由 Docker 容器处理的,以将 mongod 服务绑定映射到 docker-compose 文件中的给定端口,但事实证明这不是真的。
【讨论】:
以上是关于Springboot 应用程序无法使用 docker-compose 连接到 MongoDB 容器的主要内容,如果未能解决你的问题,请参考以下文章
使用 MySQL 和 Docker 的 Spring Boot:IllegalStateException:无法加载驱动程序类:com.mysql.cj.jdbc.Driver