Can not connect to mongodb replica set, deployed using docker-compose

108 views Asked by At

I am trying to setup MongoDB replica set deployment with docker-compose. It seems, something is missing in the network configuration and at the current moment I am getting ENOTFOUND or ECONNREFUSED error (see the description below).

Steps to reproduce

  1. Create docker-compose.yml file:
version: "3.8"

services:
  mongo1:
    hostname: mongo1
    image: mongo
    expose:
      - 27017
    ports:
      - 30001:27017
    restart: always
    entrypoint:
      [
        "/usr/bin/mongod",
        "--bind_ip_all",
        "--replSet",
        "rs0",
        "--dbpath",
        "/data/db",
      ]
    volumes:
      - ./my-volume/mongo/data1/db:/data/db
      - ./my-volume/mongo/data1/configdb:/data/configdb
  mongo2:
    hostname: mongo2
    image: mongo
    expose:
      - 27017
    ports:
      - 30002:27017
    restart: always
    entrypoint:
      [
        "/usr/bin/mongod",
        "--bind_ip_all",
        "--replSet",
        "rs0",
        "--dbpath",
        "/data/db",
      ]
    volumes:
      - ./my-volume/mongo/data2/db:/data/db
      - ./my-volume/mongo/data2/configdb:/data/configdb
  mongo3:
    hostname: mongo3
    image: mongo
    expose:
      - 27017
    ports:
      - 30003:27017
    restart: always
    entrypoint:
      [
        "/usr/bin/mongod",
        "--bind_ip_all",
        "--replSet",
        "rs0",
        "--dbpath",
        "/data/db",
      ]
    volumes:
      - ./my-volume/mongo/data3/db:/data/db
      - ./my-volume/mongo/data3/configdb:/data/configdb

  mongo-setup:
    image: mongo
    restart: on-failure
    depends_on:
      - mongo1
      - mongo2
      - mongo3
    command: >
      mongosh --host mongo1:27017 --eval
      '
      config = {
      "_id" : "rs0",
      "members" : [
        {
          "_id" : 0,
          "host" : "mongo1:27017"
        },
        {
          "_id" : 1,
          "host" : "mongo2:27017"
        },
        {
          "_id" : 2,
          "host" : "mongo3:27017"
        }
      ]
      };
      rs.initiate(config);
      '

  1. Run docker-compose up (you should see multiple attempts (and restarts) of mongo-setup service trying to initialize replica set. Withing a minute it should do its job and replica set will be successfully configured)

  2. Check that replica set is indeed initialized by running:

3.1. docker ps will output running docker containers:

0d254f165665   mongo     K   N   M  0.0.0.0:30002->27017/tcp   mongo_mongo2_1
ca1a6bbcd7fc   mongo     K   N   M  0.0.0.0:30001->27017/tcp   mongo_mongo1_1
c6a31065a78b   mongo     K   N   M  0.0.0.0:30003->27017/tcp   mongo_mongo3_1

3.2. docker exec -it mongo_mongo1_1 mongosh - enter one of the mongo containers

3.3. rs.status().members.map(m => m.stateStr) - check if primary is elected

  1. Connect to your replica set with mongosh tool:
./mongosh mongodb://mongo1:30001,mongo2:30003,mongo3:30003/test2?replicaSet=rs0

At step 4. I am getting an error:

Current Mongosh Log ID: 655cb0f059e685898230d913 Connecting to: mongodb://mongo1:30001,mongo2:30003,mongo3:30003/test2?replicaSet=adscan-mongo-set&appName=mongosh+2.0.2 MongoNetworkError: getaddrinfo ENOTFOUND mongo2

Note: I also tried to add to /etc/hosts

127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3

It did not help. I just getting another kind of error:

Current Mongosh Log ID: 655cb1d3aaedcb5ff1f652f3 Connecting to: mongodb://mongo1:30001,mongo2:30003,mongo3:30003/test2?replicaSet=adscan-mongo-set&appName=mongosh+2.0.2 MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

Tried connection with Mongo Compass and mongosh tool. Same result.

0

There are 0 answers