Can not access replicaset mongodb(docker) from host

Viewed 98

I have my nodejs code running on my host machine(MACOS) and which is trying to connect to the mongodb replicaset which is running in the docker.

version: "3"

services:
  redis_master:
    image: redis:2.8
    volumes:
      - "/Users/hiteshbaldaniya/docker-redis/master:/data/"
    ports:
      - "6379:6379"
    networks:
      - database

  mongodb_primary:
    build:
      context: ./
      dockerfile: DockerfileDB
    command: mongod --replSet "hdbrs" --dbpath "/data/27017/" --port 27017
    ports:
      - "27017:27017"
    volumes:
      - "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
    networks:
      - database

  mongodb_secondary1:
    build:
      context: ./
      dockerfile: DockerfileDB
    command: mongod --replSet "hdbrs" --dbpath "/data/27018/" --port 27018
    ports:
      - "27018:27018"
    volumes:
      - "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
    networks:
      - database

  mongodb_secondary2:
    build:
      context: ./
      dockerfile: DockerfileDB
    command: mongod --replSet "hdbrs" --dbpath "/data/27019/" --port 27019
    ports:
      - "27019:27019"
    volumes:
      - "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
    networks:
      - database

  hdb_nginx:
    build:
      context: ./nginx/
      dockerfile: DockerfileNginx.dev
    ports:
      - "8081:80"
    volumes:
      - "/Users/hiteshbaldaniya/logs/docker-nginx/:/var/log/nginx/"
    networks:
      - backend

networks:
  backend:
    driver: bridge
  database:
    driver: bridge

All 3 ports are open on my host machine and I tried using telnet and I am able to connect to all the ports as well.

My nodejs application using mongodb-node-driver and using following configuration.

module.exports = {
  servers: [{
      host: 'localhost',
      port: 27017,
    },
    {
      host: 'localhost',
      port: 27018,
    },
    {
      host: 'localhost',
      port: 27019,
    },
  ],
  database: 'mydatabase',
  options: {
    "raw": false,
    "poolSize": 5,
    "readPreference": "primaryPreferred",
    "w": 1,
    "wtimeout": 12000,
    "replicaSet": "hdbrs"
  }
};

While connecting to mongodb my application throws following error can someone help me over here?

{ MongoNetworkError: failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]
    at Pool.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/topologies/server.js:505:11)
    at Pool.emit (events.js:198:13)
    at Connection.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/pool.js:329:12)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at Socket.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:286:20)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
  name: 'MongoNetworkError',
  message:
   'failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]' }

thanks,

1 Answers

I was able to figure out solution. By adding the host entries in my /etc/hosts file.

127.0.0.1 mongodb_primary
127.0.0.1 mongodb_secondary1
127.0.0.1 mongodb_secondary2

I am still not sure what is wrong with localhost while connecting to mongodb running inside docker from host machine, I would still need to understand the behaviour of this.

Just for the node I have also tried adding the --bind_ip_all in command line during command for mongodb run.

Do let me know if someone know it. thanks

Related