Home > Net >  Mongo db - connection exception from another container in Docker
Mongo db - connection exception from another container in Docker

Time:09-24

In Jenkins I am trying to run in docker container my backend services, which are dependant on Mongo DB.

Currently I have it like this:

 stage('Backend clone and build') {
                          steps { git ([url : 'https://github...', branch : 'develop', credentialsId : '***'])

                                 sh 'docker pull mongo'
                                 sh 'docker run -d -p 27017:27017 --name mongodb mongo'

                                 sh 'docker build -t myimage2 .'
                                 sh 'docker run myimage2'
                                                         }
                }

After I run the build, I have this exception:

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.2.jar!/:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.2.jar!/:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.2.jar!/:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.2.jar!/:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)

I tried to link the container with mongodb container as : sh 'docker run --link mongodb:27017 myimage2', but no success.

I was also thinking to define the host via mongodb://localhost:27017 but this command is not accepted in docker. I did not find any solution how to get rid of that exception.

If I dive deeper to that exception, I also found there this:

Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. 

So for some reason it is not able to connect to localhost:27017 where the mongodb should be running.

CodePudding user response:

To help others who may have this same problem, you have 2 options to set up correctly:

  1. Create a docker network and start all the containers on this network so that the containers can discover each other, the default is the bridge network type. In this case, the containers are isolated from the host networking interface and can't be accessed via "localhost".

  2. If you want the containers to run on the host network and be accessible via the host network interface aka localhost or a host IP, do as docker suggests in it's official docs here: https://docs.docker.com/network/host/

  • Related