0

I had a previous question that (I think at least) that more or less became useless as I managed to fix the errors I was presented with.. However my new error, is that I cannot seem to connect to mongodb from within my docker image

docker-compose.yml:

version: '3.1'
services:
  company-repo-docker-app:
    image: company-repo-image
    build:
        context: ./
    dockerfile: Dockerfile
    depends_on:
        - mongo
ports:
    - 8080:8080
mongo:
image: mongo
ports:
  - '27017:27017'
networks:
  mongo_net:
    ipv4_address: 172.28.0.2

networks:
  mongo_net:
    driver: bridge
    ipam:
      driver: default
      config:
      -
       subnet: 172.28.0.2/24

application.properties:

spring.data.mongodb.database=myDb
spring.data.mongodb.host=172.28.0.2 
spring.data.mongodb.port=27017

And the actual error:

 com.mongodb.MongoSocketOpenException: Exception opening socket
   at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar!/:na]
   at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: java.net.SocketTimeoutException: connect timed out 
3
  • Firstly, you created a custom network and pointed mongo container to run in that network but doesnt point app to run in the same network ? Commented Jan 28, 2019 at 16:07
  • I honestly didn't think I needed to, every time I ran the application, the app was running on that network Commented Jan 28, 2019 at 16:13
  • what happens when you set spring.data.mongodb.host=mongo. Does it work ? Also share the output of docker inspect network mongo_net. let us find both the containers are attached to that network and its ip`s. Commented Jan 28, 2019 at 16:37

2 Answers 2

1

You’ve explicitly created a network in your docker-compose.yml file, and set your database to use that network. Docker Compose also creates a default network for you, and since your application doesn’t explicitly declare any networks, that attaches to only the default network. Then the two containers are on different networks and the one can’t reach the other.

The easiest solution here is to just delete all of the manual network configuration:

version: '3.1'
services:
  company-repo-docker-app:
    image: company-repo-image
    build:
      context: ./
    depends_on:
      - mongo
    ports:
      - '8080:8080'
  mongo:
    image: mongo
    ports:
      - '27017:27017'

The name of the service block will be a valid DNS hostname, and in your configuration you can set spring.data.mongodb.host=mongo.

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

1 Comment

How can I set "spring.data.mongodb.host=mongo" in my application.properties file in spring and run maven install? My build fails if I do that. I tried "spring.data.mongodb.uri=mongodb://mongo/test" and its the same thing. Is there a way I don't have to build a .jar file and still have my spring application run on a container?
1

I agree David answer is right. But here is a takeaway, your configuration works too, I tried with my sample and here is the result.

Issue is your app is not attached to the network.

services:
  company-repo-docker-app:
    image: company-repo-image
    build:
        context: ./
    dockerfile: Dockerfile
    depends_on:
        - mongo
    ports:
     - 8080:8080
    networks:
     - mongo_net

sample docker-compose.yml

version: '3.1'
services:
  mongo-app:
    image: barath-mongo
    ports:
      - "9000:9000"
    depends_on:
      - mongo
    environment:     
      SPRING_DATA_MONGODB_PORT: 27017
      SPRING_DATA_MONGODB_HOST: 172.28.0.2 
      SPRING_DATA_MONGODB_DATABASE: myDb
    networks:
      - mongo_net
  mongo:
    image: mongo
    ports:
    - '27017:27017'
    networks:
     mongo_net:
        ipv4_address: 172.28.0.2
networks:
  mongo_net:
    driver: bridge
    ipam:
      driver: default
      config:
      -
       subnet: 172.28.0.2/24

mongo container ip address:

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ec2-user_mongo_1
172.28.0.2

docker network settings

 "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.28.0.2/24"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "02b8b4148dd830eaddeb7e9ebd6305a6ae3c106bae5a495103c632aec7999274": {
                "Name": "ec2-user_mongo-app_1",
                "EndpointID": "885088d551888e1dec95b1c0e337c6a4b84f1bb2aebc4407963d3c0b4f6ec09d",
                "MacAddress": "02:42:ac:1c:00:03",
                "IPv4Address": "172.28.0.3/24",
                "IPv6Address": ""
            },
            "3e4266fca3c8163412cc1c4733cad004aa0f366dc9fe30d03c7ecafe87b4e826": {
                "Name": "ec2-user_mongo_1",
                "EndpointID": "a6a72c7fc314a294c53d5c394ac1757702a0a95c588af2f730e971953dbbaa5f",
                "MacAddress": "02:42:ac:1c:00:02",
                "IPv4Address": "172.28.0.2/24",
                "IPv6Address": ""
            }
        },

output :

ongo-app_1  | 2019-01-28 17:07:29.710  INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to 172.28.0.2:27017
mongo-app_1  | 2019-01-28 17:07:29.735  INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=172.28.0.2:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=20314562}
mongo-app_1  | 2019-01-28 17:07:29.742  INFO 1 --- [72.28.0.2:27017] org.mongodb.driver.cluster               : Discovered cluster type of STANDALONE
mongo-app_1  | 2019-01-28 17:07:30.387  INFO 1 --- [           main] com.barath.app.service.CustomerService   : Saving the customer with customer details com.barath.app.document.Customer@37654521
mongo_1      | 2019-01-28T17:07:30.488+0000 I NETWORK  [listener] connection accepted from 172.28.0.3:56250 #2 (2 connections now open)
mongo_1      | 2019-01-28T17:07:30.491+0000 I NETWORK  [conn2] received client metadata from 172.28.0.3:56250 conn2: { driver: { name: "mongo-java-driver", version: "3.8.2" }, os: { type: "Linux", name: "Linux", architecture: "amd64", version: "4.14.77-81.59.amzn2.x86_64" }, platform: "Java/Oracle Corporation/1.8.0_191-b12" }
mongo-app_1  | 2019-01-28 17:07:30.497  INFO 1 --- [           main] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:2}] to 172.28.0.2:27017
mongo_1      | 2019-01-28T17:07:30.576+0000 I STORAGE  [conn2] createCollection: myDb.customer with generated UUID: 6bd5b022-16b6-4a04-96b0-d94988155a53
mongo-app_1  | 2019-01-28 17:07:31.514  INFO 1 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
mongo-app_1  | 2019-01-28 17:07:31.879  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
mongo-app_1  | 2019-01-28 17:07:32.372  INFO 1 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
mongo-app_1  | 2019-01-28 17:07:32.432  INFO 1 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
mongo-app_1  | 2019-01-28 17:07:32.522  INFO 1 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
mongo-app_1  | 2019-01-28 17:07:32.977  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9000 (http) with context path ''
mongo-app_1  | 2019-01-28 17:07:32.984  INFO 1 --- [           main] com.barath.app.Application               : Started Application in 11.151 seconds (JVM running for 13.149)

1 Comment

Thanks for the answer :D I ended up going with David's solution for now (KISS :'D ) but your's was great for learngin for me, and i am sure it will come up later in development!

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.