无法在 Docker-Compose 中使用 Spring Boot 连接到 MongoDB
Posted
技术标签:
【中文标题】无法在 Docker-Compose 中使用 Spring Boot 连接到 MongoDB【英文标题】:Unable to connect to MongoDB using Spring Boot in Docker-Compose 【发布时间】:2021-06-14 20:44:41 【问题描述】:我正在尝试在 Docker Compose 中运行我的应用程序和 MongoDB 容器,但出现以下错误:
2021-03-17 13:30:08.776 INFO 1 --- [l'-mongo:27018] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server mongo:27018
com.mongodb.MongoSocketOpenException: Exception opening socket
app | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
app | Caused by: java.net.ConnectException: Connection refused (Connection refused)
app | at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
app | at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:na]
app | at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:na]
app | at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:na]
app | at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
app | at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
app | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:78) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.1.1.jar!/:na]
app | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.1.1.jar!/:na]
docker-compose.yml
version: '3'
services:
app:
build:
context: .
container_name: app
hostname: app
depends_on:
- mongo
ports:
- "8081:8081"
environment:
- SPRING_DATA_MONGODB_HOST=mongo
- SPRING_DATA_MONGODB_PORT=27018
- SPRING_DATA_MONGODB_DATABASE=tfood
mongo:
image: 'mongo:latest'
container_name: mongo
hostname: mongo
volumes:
- ./data/mongo:/data/db
ports:
- "27018:27017"
互联网上遇到与我相同问题的每个人总是在主机名方面遇到一些问题,我的情况不是这样。从错误日志中我看到 docker 获取了连接数据,只是无法连接
【问题讨论】:
【参考方案1】:如果您想从 Windows/Mac/Linux 桌面连接到 MongoDB,这会在主机上公开端口 27018
:
ports:
- "27018:27017"
在 Docker 网络内部,暴露给其他 Docker 容器的端口是27017
。所以你需要把这个改成27017
:
SPRING_DATA_MONGODB_PORT=27017
【讨论】:
以上是关于无法在 Docker-Compose 中使用 Spring Boot 连接到 MongoDB的主要内容,如果未能解决你的问题,请参考以下文章
django 无法在 docker-compose 中连接 mysql
无法在 Docker-Compose 中使用 Spring Boot 连接到 MongoDB
使用 docker-compose 无法在同一网络中连接 mysql 和 spring boot
使用`docker-compose run`时无法调试,但`docker-compose up`有效