title: SpringBoot in Docker not connecting to Mongo in Docker
body: <p>I have a Spring Boot Application and developed it with a mongo db which was running in brew services. </p>
  
  <p>To get a connection to the db I just had to put the following into application.properties in Spring Boot </p>
  
  <pre><code>spring.data.mongodb.uri=mongodb://localhost:27017/db
  </code></pre>
  
  <p>changing the application properties to </p>
  
  <pre><code>spring.data.mongodb.uri=mongodb://mongo:27017/db
  </code></pre>
  
  <p>didtn't change anything, same Error as before.</p>
  
  <p>Now I'm trying to put the SpringBoot Application and the MongoDB into Docker-Containers, but cant get any connection working.</p>
  
  <p>So this is my Dockerfile in the SpringBoot Application:</p>
  
  <pre><code>FROM openjdk:8-jdk-alpine
  VOLUME /tmp
  EXPOSE 8080
  ADD /build/libs/dg-0.0.1-SNAPSHOT.jar dg-0.0.1-SNAPSHOT.jar
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]
  </code></pre>
  
  <p>This is my Docker-Compose.yml file:</p>
  
  <pre><code>version: '3'
  
  services:
  
  mongo:
   container_name: docker-mongo
   image: mongo:latest
   ports:
     - "27017:27017"
   volumes:
    - ./data/db:/data/db
  
  spring:
   depends_on:
     - mongo
   image:
     docker-spring-http-alpine
   ports:
     - "8080:8080"
   links:
     - mongo
  </code></pre>
  
  <p>After executing Docker-Compose with </p>
  
  <pre><code>docker-compose up
  </code></pre>
  
  <p>I get the following error: (this is the actual error message )</p>
  
  <pre><code>2019-07-08 23:10:19.990  INFO 1 --- [localhost:27017] org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017
  
  
      com.mongodb.MongoSocketOpenException: Exception opening socket
  at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar!/:na]
  at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar!/:na]
  at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar!/:na]
  at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
  at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
  at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
  at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar!/:na]
  at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar!/:na]
  ... 3 common frames omitted
  </code></pre>
  
  <p>Does someone know whats the problem here?
  In development environment it works like a charm.</p>
  
  <p>Thanks in advance</p>
  
  <p>Adding the following line to the Dockerfile </p>
  
  <pre><code>"-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg"
  </code></pre>
  
  <p>into Entrypoint like this solved the connection issue</p>
  
  <pre><code>ENTRYPOINT ["java", "-Dspring.data.mongodb.uri=mongodb://mongo:27017/dg","-Djava.security.egd=file:/dev/./urandom","-jar","/dg-0.0.1-SNAPSHOT.jar"]
  </code></pre>
  
  <p>I was able to get connection to the db without the mentioned line above in the Entrypoint in the Dockerfile. I guess this is if you like to connect your db over "links"</p>
  
  <p>But now I was able to connect over Network, this is my code:</p>
  
  <pre><code>version: '3.6'
  
  services:
  
   mongo:
   container_name: docker_mongo
   networks:
     - gateway
   ports:
     - "27017:27017"
   hostname: mongo
   image: mongo:latest
   volumes:
     - ./data/db:/data/db
  
   spring:
   container_name: docker-spring
   networks:
     - gateway
   ports:
     - "8080:8080"
   hostname: spring
   depends_on:
     - mongo
   image: dg-docker-spring-http-alpine-j
   networks:
  gateway:
  driver: "bridge"
  </code></pre>
  
  <p>and the following in application properties</p>
  
  <pre><code>spring.data.mongodb.host=docker_mongo
  spring.data.mongodb.port=27017
  spring.data.mongodb.database=db
  </code></pre>
  
  <p>So it looks like the connection is working over Network now.
  The same code did not work with Version 3.0</p>
  
  <p>To prevent SpringBoot to connect automatically to mongo over localhost it's also necessary to exclude MongoAutoConfiguration!</p>
  
  <pre><code>@SpringBootApplication(exclude={MongoAutoConfiguration.class})
  </code></pre>
  
  <p>Thank you all for your help</p>