When I trying to run my spring-boot application on docker , it is not able to connect to the Mongodb container running in docker.
Application.properties
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
Dockerfile for spring application
FROM openjdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yaml
version: '3'
services:
mongo:
image: mongo:latest
container_name: docker-mongo
ports:
- "27017:27017"
wacaj_docker:
image: wacaj
depends_on:
- mongo
container_name: wacaj
build: .
ports:
- "8080:8080"
I am able to connect to the mongodb container from my local system , but the spring-application container is not able to connect to mongo container. And also if I run the spring application in local it is able to connect to the mongo runneing in docker container. I am unable to troubleshoot the problem.
I tried changing hostname to both servicename i.e. mongo and container name i.e. docker-mongo, but it is still not working.
2023-01-07 08:50:03.146 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
wacaj |
wacaj | com.mongodb.MongoSocketOpenException: Exception opening socket
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
wacaj | Caused by: java.net.ConnectException: Connection refused
wacaj | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
wacaj | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:539) ~[na:na]
wacaj | at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:594) ~[na:na]
wacaj | at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
wacaj | at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
wacaj | at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.6.1.jar!/:na]
wacaj | at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.6.1.jar!/:na]
CodePudding user response:
You need to change the hostname:
spring.data.mongodb.host=mongo
Docker makes every service in a docker compose setup available under its service name, in your case mongo
and wacaj_docker
.
Please refer to the docs for further details.