Unable to connect to MongoDB cluster created through docker compose

Hi,

For my demo C# project I am trying to create a replica set through docker compose and connect to it first from NoSQLBooster/ Mongodb Compass and then from my C# application. However, the I am getting different errors during the process and not able to connect to the replica set at all.

After spending a lot of times searching through internet and trying various options, I finally decided to create a simple docker compose file to demonstrate my situation. This docker compose file is taken from one of the github samples I stumbled upon, which is very similar to other solutions I have seen as well, which creates 3 instances of mongodb, add them to a replica set name rs0 and then use mongo init to initialize the replicaset.

Here is the complete docker compose file:

version: "3"
services:
  mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    expose:
      - 27017
    ports:
      - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
      - ~/.volumes/mongo/data1/db:/data/db
      - ~/.volumes/mongo/data1/configdb:/data/configdb
  mongo2:
    hostname: mongo2
    container_name: localmongo2
    image: mongo
    expose:
      - 27017
    ports:
      - 27018:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
      - ~/.volumes/mongo/data2/db:/data/db
      - ~/.volumes/mongo/data2/configdb:/data/configdb
  mongo3:
    hostname: mongo3
    container_name: localmongo3
    image: mongo
    expose:
      - 27017
    ports:
      - 27019:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
    volumes:
      - ~/.volumes/mongo/data3/db:/data/db
      - ~/.volumes/mongo/data3/configdb:/data/configdb

  mongoinit:
    image: mongo:6.0.7
    # this container will exit after executing the command
    restart: "no"
    depends_on:
      - localmongo1
      - localmongo2
      - localmongo3
    command: >
      mongosh --host localhost:27017 --eval 
      '
      db = (new Mongo("localhost:27017")).getDB("test");
      config = {
      "_id" : "rs0",
      "members" : [
        {
          "_id" : 0,
          "host" : "mongo1:27017"
        },
        {
          "_id" : 1,
          "host" : "mongo2:27017"
        },
        {
          "_id" : 2,
          "host" : "mongo3:27017"
        }
      ]
      };
      rs.initiate(config);
      '      

After I run this, I get the following message:

When I open the mongoinit container logs, I get the following message:

Current Mongosh Log ID: 64e54c92cecb757d4468cd86
Connecting to:          mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

Then I tried to connect to the primary db from NoSQLBooster, got a connection success

mongodb://127.0.0.1:27017

image

But when I completed the connection, it’s showing this message: not primary and secondaryOK = false

image

I tried this connection string:

mongodb://localhost:27017?readPreference=primaryPreferred

but got the error as shown below:

Any help to solve this issue will be appreciated. Thank you!

Hi, @UB_K,

You have created 3 Docker instances, but have not configured a shared virtual network for them to communicate on. Thus the host can communicate with each Docker container, but the Docker containers cannot communicate amongst themselves. The following step-by-step guide will hopefully help:

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.