Connecting to MongoDB Replica Set from Outside Docker

62 views Asked by At

I'm encountering difficulties establishing a connection to a MongoDB replica set from outside a Docker environment. The replica set is running within Docker containers, and I'm attempting to connect from an external application.

Details:

  • MongoDB Replica Set Details:
    • Dockerized environment (docker-compose.yml)
    • Multiple containers (centraldatabase and centralm2 and centralm3)
    • Replica Set Name: mongoSet.

Compose file:

version: '3.8'
services:
  centralDatabase:
    image: mongo:jammy
    container_name: ${NODE_ENV}-centralDatabase
    restart: always
    env_file:
      - .env.docker
    ports:
      - '${SERVER_MAIN_CENTRAL_PORT}:27017'
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /etc/mongox/keyfile.txt
        chown 999:999 /etc/mongox/keyfile.txt
        chmod 555 /etc/init_mongo_repl.sh
        chmod 555 /etc/init_test_db.sh
        chmod 555 /etc/delete-test-central-db-data.sh
        exec docker-entrypoint.sh $$@
    command: 'mongod --bind_ip_all --replSet mongoSet --keyFile ./etc/mongox/keyfile.txt'
    volumes:
      - db1dev:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./mongoconf:/etc/mongox
      - ./scripts/init_mongo_repl.sh:/etc/init_mongo_repl.sh
      - ./scripts/init_test_db.sh:/etc/init_test_db.sh
      - ./scripts/delete-test-central-db-data.sh:/etc/delete-test-central-db-data.sh
    links:
      - centralm2
      - centralm3
  centralm2:
    image: mongo:jammy
    container_name: ${NODE_ENV}-m2
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 555 /etc/init_test_db.sh
        exec docker-entrypoint.sh $$@
    env_file:
      - .env.docker
    command: 'mongod --bind_ip_all --replSet mongoSet --keyFile ./etc/mongox/keyfile.txt'
    ports:
      - '${SERVER_FIRST_CENTRAL_REPLICA_PORT}:27017'
    volumes:
      - db2dev:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./mongoconf:/etc/mongox
      - ./scripts/init_test_db.sh:/etc/init_test_db.sh
  centralm3:
    image: mongo:jammy
    container_name: ${NODE_ENV}-m3
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 555 /etc/init_test_db.sh
        exec docker-entrypoint.sh $$@
    env_file:
      - .env.docker
    command: 'mongod --bind_ip_all --replSet mongoSet --keyFile ./etc/mongox/keyfile.txt'
    ports:
      - '${SERVER_SECOND_CENTRAL_REPLICA_PORT}:27017'
    volumes:
      - db3dev:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      - ./mongoconf:/etc/mongox
      - ./scripts/init_test_db.sh:/etc/init_test_db.sh

volumes:
  db1dev:
  db2dev:
  db3dev:

Connection Code:


    const mongoose = require('mongoose');
    
    mongoose.connect("mongodb://centraldatabase:27017,centralm2:27017/tamcentral?replicaSet=mongoSet", { useNewUrlParser: true, useUnifiedTopology: true })
      .then(() => {
        console.log('Connected to MongoDB');
      })
      .catch((error) => {
        console.error('Error connecting to MongoDB:', error);
      });

Issue:

The connection attempt results in the following error:

MongooseServerSelectionError: Server selection timed out after 30000 ms
...
0

There are 0 answers