chaudharyp
chaudharyp

Reputation: 3634

using system mongodb for multiple docker containers?

I am very new to Docker and I'm struggling with a configuration. My current configuration is that I have multiple Node/Express microservices on my local system which are using nodemon. All of these connect to mongodb running on mongodb://localhost:27017/localv2.

I'm trying to dockerize all my microservices. The issue is that they are not able to connect to mongodb on localhost:27017. I have 2 questions:

  1. Why are they not able to connect to localhost:27017?

  2. How to make them connect to the current running mongodb in my system?

Upvotes: 2

Views: 1258

Answers (3)

Bhagvat Lande
Bhagvat Lande

Reputation: 1480

first, -you need to find docker container ip addres by, docker inspect container_id -the field is like "IPAddress": "172.17.0.7"

second, -you neeed to start mongod on your local machine -you need to use mongochef(mongo browser) -from mongo browser you need to connect mongo client as localhost:27017 instead of this use container-ip:27017 and connect to mongodb instance

Upvotes: 0

Guillaume Simonneau
Guillaume Simonneau

Reputation: 87

You can try something like this:

```

version: '2'
services:

  mongod:
    image: khezen/mongo:3.4
    volumes:
      - /data/mongo/mongod1:/data/db
    ports:
      - "27017:27017"
    network_mode: bridge
    restart: unless-stopped

  helloworld:
    build: ./
    links: 
      - mongod:database
    ports:
      - "80:80"
    network_mode: bridge
    restart: unless-stopped

```

helloworld will resolve datatabase as the ip address of mongod

Upvotes: 1

Thanh Nguyen Van
Thanh Nguyen Van

Reputation: 11822

1 & 2. When you build a docker container, it creates VLAN for docker container with ip address gateway is : 172.17.42.1, so docker container would connect to mongo, it should be : mongodb://172.17.42.1:27017/localv2

Upvotes: 2

Related Questions