0

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

3 Answers3

1

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:secret@mongo1.example.com: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
bipe15
  • 111
  • 1
  • 8
  • Hi bipe15, I ran the following commands: `docker network create 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 run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_URI=mongodb://0.0.0.0:27017 doc40` Now I am getting: `Caused by: 'mongoDatabaseFactory' threw exception; nested exception is java.lang.IllegalArgument **Exception: Database name must not be empty!**` – Just Praveen Sep 23 '21 at 19:21
  • Right, docker run -p 4444:8080 --name mydoctest -e SPRING_DATA_MONGODB_URI=mongodb://my-mongodb-container:27017/test doc40. Mind I added the /test in the connection and also point to my-mongodb-container, not to 0.0.0.0 – bipe15 Sep 23 '21 at 19:28
  • Simply not connecting... after adding the host and DB, its throwing socket exception. I ran the jar on localhost and it connects without any issues to mongodb in docker. But I copy the jar to docker and I am able to access the URL and it is unable to connect to mongo. I use localhost to invoke the url. Both in case of running a jar on localhost or on docker... everything is same. The only difference is the dokcer container where I host my spring jar. – Just Praveen Sep 24 '21 at 13:06
1

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.

0

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.

KDW
  • 485
  • 3
  • 17
  • Hi KDW, following is the docker inspect o/p `docker inspect "Gateway": "172.17.0.1", "IPAddress": "172.17.0.4", docker inspect "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2",` – Just Praveen Sep 23 '21 at 18:22
  • Those are addresses used by Docker for the different containers and are different from the docker host address. Try the docker-machine command mentioned in the previous answer and see its output. Try to use this address instead of localhost in your connection string. Also take a look at @bipe15's comment since this is probably a more generic approach compared to my answer. – KDW Sep 23 '21 at 18:30
  • Actually, I am on windows. I am using docker desktop. I am unable to find the docker-machine command. – Just Praveen Sep 23 '21 at 18:52
  • Are you sure your Mongo container is running and accessible when the application is started? Can you access Mongo using localhost:27017 with some kind of GUI (e.g. MongoDB Compass)? I don't think Docker Desktop is using 192-addresses for the host anymore. – KDW Sep 23 '21 at 19:22
  • yes....So i run the spring boot jar through java -jar myapp.jar when I access the app from browser it does insert and read the values from mongo. I am also running mongo and have access to command prompt of the container. I am able to connect through mongo console and view the collections and docs created.. – Just Praveen Sep 23 '21 at 19:24