Home > Back-end >  Dockerize spring boot mongo
Dockerize spring boot mongo

Time:09-24

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

CodePudding user response:

What's the IP address of your Docker host machine?

Your app tries to connect to Mongo using localhost. Depending on what Docker installation you are using (taking the problem into account I assume Docker Toolbox), localhost won't work. You might try the IP address of Docker host instead of localhost. Most of the times, the IP is 192.168.99.100 but could be something else as well.

You can find the address by executing docker-machine ip using Docker command line.

CodePudding user response:

The problem begins with your connectionString to mongoDb and the architecture of containers

You are trying to connect to localhost:27017 from the java container. In the java container, Mongo is not running. Instead is running in another container. You have to change your connection string to point to my-mongodb-container:27017

I'll recommend using docker networks instead of --link since it is deprecated https://docs.docker.com/network/links/

I'll give you a quick example

docker network create -d brigde app-network
docker run -p27017:27017 --name my-mongodb-container -d mongo:latest
docker network connect app-network my-mongodb-container
docker run -p 4444:8080 --name mydoctest doc40
docker network connect app-network mydoctest

(I have not tested it, let me know if there is any mistake)

Depends on the Mongo driver you are using to you have to set 2 different variables to change your connection string as stated in Spring Boot and how to configure connection details to MongoDB?

spring.data.mongodb.uri=mongodb://user:[email protected]:12345
spring.data.mongodb.host=127.0.0.1

So you can create the container respectively using

docker run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_URI=mongodb://my-mongodb-container:27017 doc40
or
docker run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_HOST=my-mongodb-container doc40
  • Related