1

I have a spring-boot application running which accesses mongodb

When I use individual docker run commands, the application works perfectly fine. However, when I try to run all of them as a service, for some reason the application is never able to connect to mongo.

What I have tried:

  • To begin with compose file [listed below]
  • Run mongo separately and provide external_links to mongo [still fails]
  • In the compose file, also mentioned depends_on [still fails]
  • Override the hostname variable in compose file [fails]

docker-compose.yaml

version: '2'
services:
 mongo:
  image: mongo:latest
  container_name: mongo
  ports:
   - "27017:27017"

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

compose file with external dependency:

mongo started with docker run --name mongo -p 27017:27017 -d mongo

version: '2'
services:

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  external_links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
    - SPRING_DATA_MONGODB_HOST=mongo
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

In any of the cases listed above, the error I get is:

hygieia-api    | 2017-05-22 19:20:43,918 INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 172.17.0.2:27017 hygieia-api    | com.mongodb.MongoSocketOpenException: Exception opening socket hygieia-api    |    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |  at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |     at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] hygieia-api    | Caused by: java.net.SocketTimeoutException: connect timed out hygieia-api    |     at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111] hygieia-api    |   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111] hygieia-api    |    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111] hygieia-api    |     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111] hygieia-api    |  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111] hygieia-api    |  at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111] hygieia-api   |     at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongo-java-driver-3.0.2.jar!/:na] hygieia-api    |  ... 3 common frames omitted hygieia-api    | 2017-05-22 19:20:55,118 INFO  o.a.catalina.core.StandardService - Stopping service Tomcat hygieia-api    | 2017-05-22 19:20:55,127 WARN  o.a.c.loader.WebappClassLoaderBase - The web application [api] appears to have started a thread named [cluster-ClusterId{value='592339f7e03dd80008647086', description='null'}-`172.17.0.2:27017`] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:

docker inspect gives the correct IP address the application is referring to:

    "Ports": {
        "27017/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "27017"
            }
        ]
    },
    "SandboxKey": "/var/run/docker/netns/27e81b7954c9",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "ac2ded64c0bb0c9c1d82f4081b84ba2ae7f72e90527f310c7b107d0d3b7df1e0",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:02",
    "Networks": {
        "bridge": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": null,
            "NetworkID": "dc798dbca17eaced0a9bdb9f87da3672cdaa4424bafb0f49ba526c374cbfcc5b",
            "EndpointID": "ac2ded64c0bb0c9c1d82f4081b84ba2ae7f72e90527f310c7b107d0d3b7df1e0",
            "Gateway": "172.17.0.1",
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:11:00:02"

When the application and mongo is run independently it works as a charm

docker run --name mongo -p 27017:27017 -d mongo

docker run --name hygieia-api --link mongo:mongo -e "JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret"    -p 8080:8080 -d hygieia-api:latest

What am I missing?

1 Answer 1

3

When you're using docker compose you may reach your container with the name of your service. Therefore just change your spring application's mongodb IP configuration as follow:

mongo:27017

With your first compose file.

version: '2'
services:
 mongo:
  image: mongo:latest
  container_name: mongo
  ports:
   - "27017:27017"

 hygieia-api:
  image: hygieia-api:latest
  container_name: hygieia-api
  ports:
   - "8080:8080"
  volumes:
   - ./logs:/hygieia/logs
  links:
   - mongo:mongo
  environment:
    - JASYPT_ENCRYPTOR_PASSWORD=hygieiasecret
  depends_on:
   - mongo

 hygieia-ui:
  image: hygieia-ui:LATEST
  container_name: hygieia-ui
  ports:
   - "8088:80"
  links:
   - hygieia-api

Here's an example with **version: "3"

version: "3"
services:
    mongodb:
      image: mongo:3.2
      volumes:
       - ${HOME}/gocart_db:/data/db
      ports:
       - "27017:27017"

    blabla-service:
      image: blabla-service
      depends_on:
       - mongodb

In this example my blabla-service successfully connects mongodb:27017

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks! I tried that after posting and it still fails, it refers to the db as mongo:27017 then socket timeout
Also would you please try to put depends_on directive too, if it doesn't work I guess you need to create a network in your compose file which will be shared between your services.
Should we go through creating network though? Compose already creates a network. Cool, I'll try with version 3 and post the results back
I've added an example with the version:3 which works for me :)
Awesome! Thanks mate. Can you confirm it didn't work for in version 2?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.