2

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?

3 Answers 3

2

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

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

7 Comments

Cool. I get this point. But I'm running multiple microservices. If there is a docker container for each of them, there there will be a different mogodb URL for each of them. I want a single database. Can you please point me to the right direction of how to achieve this?
Using only 1 URL above for multiple docker containers. Because 172.17.42.1 is gateway ip address for all docker containers.
I got this to work by using a combination of your and Guillaume Simonneau's answer below. One more issue though: What to do if my mongodb is running remotely, like on modulus? My mongo connection string is something like: mongodb://uname:[email protected]:27017/db123. When I run my container, it gives error: MongoError: failed to connect to server [jello.modulusmongo.net:27017] on first connect.
[jello.modulusmongo.net:27017] means port 27017 hasn't opened on modulus, check it.
It is open. The same thing works when I run my app using normal node command. It is not working from inside docker container.
|
1

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

Comments

0

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

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.