1

I'm facing MongoDB connection issue from docker container, i read few stackoverflow answers but not helped me to solve my issue.

My application.yml file has the following,

spring:
   application:
    name: web-ui-service
   data:
     mongodb:
       uri: mongodb://<username>:<pwd>@172.17.0.1:27017/database

and My docker-compose file has the following,

version: '3.5' services:

sp-app-web-ui:
  container_name: sp-app-web-ui
  image: swimming-pool/web-ui-service
    environment:
    - DOCKER_HOST_IP=mongodb://172.17.0.1:27017
     - SPRING_DATA_MONGODB_DATABASE=swimming-pool
   ports:
     - 8080:8080
   networks:
     - swimming-pool-app
       restart: unless-stopped

     networks:
       swimming-pool-app:
     default:
       driver: bridge

I read this blog From inside of a Docker container, how do I connect to the localhost of the machine?

but still am facing the following issue,

sp-app-web-ui | com.mongodb.MongoSocketOpenException: Exception opening socket sp-app-web-ui | at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui | at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui | at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui | at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111] sp-app-web-ui | Caused by: java.net.ConnectException: Connection refused (Connection refused) sp-app-web-ui | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111] sp-app-web-ui | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111] sp-app-web-ui | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111] sp-app-web-ui | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111] sp-app-web-ui | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111] sp-app-web-ui | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111] sp-app-web-ui | at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar!/:na] sp-app-web-ui | at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar!/:na]

I'm using Debian and Docker 18.06. It would be great if you anyone help me out. Thanks in advance!

3 Answers 3

1

I have solved the question, my docker-compose file looks as below,

version: '3.5'

services:

  sp-app-web-ui:
  container_name: sp-app-web-ui
  image: swimming-pool/web-ui-service
  environment:
    - SPRING_PROFILES_ACTIVE=dev
  ports:
    - 8080:8080
  network_mode: host

and add "127.0.0.1" in mongo.conf file in the bindIp variable.

My application.yml has the following,

spring:
   application:
    name: web-ui-service
    data:
     mongodb:
       uri: mongodb://<username>:<pwd>@172.0.0.1:27017/database

This IP 127.0.0.1 got it from docker0 when you ping ip a on linux.

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

Comments

0

What you may be looking for is to add network_mode: "host" to your docker compose service:

sp-app-web-ui:
  container_name: sp-app-web-ui
  image: swimming-pool/web-ui-service
    environment:
    - DOCKER_HOST_IP=mongodb://172.17.0.1:27017
     - SPRING_DATA_MONGODB_DATABASE=swimming-pool
   ports:
     - 8080:8080

   # https://docs.docker.com/compose/compose-file/#network_mode
   network_mode: "host"

You can read more about in the docs

3 Comments

Thank you very much for your support, i tried to used it but gives me an docker ERROR: 'network_mode' and 'networks' cannot be combined I changed driver bridge to host also but not connecting.
My mongo.conf file has net: port: 27017 bindIp: 127.0.0.1,172.17.0.1,0.0.0.0
Still the same issue :-(
0

Use the docker-compose.yml below. Then run mongodb and mongo-express (UI to access mongodb)

version: '3.1'

services:

  mongo:
    image: mongo:4.4.5
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
  
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

Comments

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.