title: Mongo db - connection exception from another container in Docker
body: <p>In Jenkins I am trying to run in docker container my backend services, which are dependant on Mongo DB.</p>
  <p>Currently I have it like this:</p>
  <pre><code> 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'
                                                           }
                  }
  </code></pre>
  <p>After I run the build, I have this exception:</p>
  <pre><code>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)
  </code></pre>
  <p>I tried to link the container with mongodb container as : <code>sh 'docker run --link mongodb:27017 myimage2'</code>, but no success.</p>
  <p>I was also thinking to define the host via <code>mongodb://localhost:27017</code> but this command is not accepted in docker. I did not find any solution how to get rid of that exception.</p>
  <p>If I dive deeper to that exception, I also found there this:</p>
  <pre><code>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. 
  </code></pre>
  <p>So for some reason it is not able to connect to localhost:27017 where the mongodb should be running.</p>