0

I have a nodejs application which dockerized and need a replicated MongoDB database. I have built my replicated MongoDB in docker-compose and working just fine. if I run the command docker inspect MongoDB-primary |grep IPAddress its print:

"IPAddress": "",
            "IPAddress": "172.18.0.2",

now in my application, i give this ip as mongoconnection string(of course with protocol names) but the application cannot connect to MongoDB and throw this error message(application also is a docker container):

 message: 'failed to connect to server [172.18.0.2:27017] on first connect [MongoNetworkError: connection 1 to 172.18.0.2:27017 timed out]',

here is my mongodb docker compose file:

version: '2'

services:
  mongodb-primary:
    image: 'bitnami/mongodb:latest'
    environment:
      - MONGODB_REPLICA_SET_MODE=primary
    volumes:
      - 'mongodb_master_data:/bitnami'

  mongodb-secondary:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_REPLICA_SET_MODE=secondary
      - MONGODB_PRIMARY_HOST=mongodb-primary
      - MONGODB_PRIMARY_PORT_NUMBER=27017

  mongodb-arbiter:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_REPLICA_SET_MODE=arbiter
      - MONGODB_PRIMARY_HOST=mongodb-primary
      - MONGODB_PRIMARY_PORT_NUMBER=27017

volumes:
  mongodb_master_data:
    driver: local

and my node js application dockerfile is:

FROM node:6.0

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=productio


# Bundle app source
COPY . .

EXPOSE 3001

CMD [ "npm", "start" ]

how can I fix this?

amir
  • 2,443
  • 3
  • 22
  • 49
  • More details are welcomed. At least `docker run` command and better `Dockerfile` – grapes Dec 25 '18 at 13:47
  • @grapes I edited my question. – amir Dec 25 '18 at 13:50
  • You may need to provide port mapping for containers, are you sure that's taken care – rex Dec 25 '18 at 13:53
  • @grapes when I type `mongo 172.18.0.2` its connect to primary mongo instance without any error. which I expect other containers should connect this way, but application cannot. – amir Dec 25 '18 at 13:55
  • Where are you trying to run the application from? You should never use that internal IP address for anything (or even look it up), it can change if you rebuild containers and isn't accessible from off-host. – David Maze Dec 25 '18 at 14:04

2 Answers2

4

Your docker-compose does not automatically expose tcp ports to the outer world, like your host PC (I assume your nodeJs runs on host and not included in docker-compose). This is the behavior of docker bridge networks, you can read more at https://docs.docker.com/network/bridge/

You have to do one of the following:

Include your NodeJs container into docker-compose

or

Expose ports from docker-compose.yml

grapes
  • 8,185
  • 1
  • 19
  • 31
  • thank you. when I added my node application on docker-compose, which address should I use? i think that IP address is no longer valid. should I use hostname or docker name instead of IP address? – amir Dec 25 '18 at 14:01
  • You should not use IP addresses anymore then - use host names. Host name is the name of a service, like `mongodb-primary` for example – grapes Dec 25 '18 at 14:02
0

I had the same issue and adding the ports fixed it for me. Make sure your connection url includes the image name: mongodb://mongo:27017/ and not localhost.

mongo:
    image: mongo
    expose:
      - 27017
    ports:
      - "27017:27017"
    volumes:
      - ./data/db:/data/db
Nätu
  • 304
  • 5
  • 16