Issue
I am trying to dockerize a spring boot web app with mongodb backend. I run mongo and map it to host with following command:
docker run -p27017:27017 --name my-mongodb-container -d mongo:latest
I have built a jar with spring boot code and am able to run it successfully. It inserts and prints some data. Now I dockerize this jar with the following Dockerfile
FROM adoptopenjdk/openjdk11
EXPOSE 8080
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
I run the docker container as:
docker run -p 4444:8080 --name mydoctest --link my-mongodb-container doc40
The instance comes up, tries to connect to mongo an fails. But the app get loaded as I have another url that functions properly. However, it just returns hard coded data.
The error that i see in the console
2021-09-23 17:13:02.725 INFO 1 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {
hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-09-23 17:13:02.824 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread whi
le connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.2.3.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-cor
e-4.2.3.jar!/:na]
Any inputs are much appriciated
Solution
Finally got it to work.
The only change I did was to connect the instance to preferred n/w on startup.
docker run -p27017:27017 --name my-mongodb-container --network=app-network -d mongo:latest
docker run -p 4444:8080 --name mydoctest --network=app-network -e SPRING_DATA_MONGODB_URI=mongodb://my-mongodb-container:27017/test doc40
I was trying to run the container and then add it to a n/w.
Answered By - Just Praveen