Deploying MongoDB ReplicaSet with Docker (Public VPS IP and User Authentication Required)

Introduction Deploying a MongoDB ReplicaSet with Docker efficiently builds a robust database system, ensuring high availability and fault tolerance. I'd like to share the detailed steps based on my real-world experience below. So the other day, I was working on a project using NestJS + Prisma + MongoDB, and it turned out that Prisma requires a ReplicaSet to work properly. Deployment Steps Create Directory Structure Prepare the necessary directories for data and configuration: mkdir -p mongodb-replicaset/data/{primary,secondary1,secondary2} cd mongodb-replicaset Create a Keyfile for Authentication The keyfile ensures secure communication between nodes. mkdir -p mongodb-replicaset/keyfile openssl rand -base64 756 > keyfile/mongodb-keyfile chmod 400 keyfile/mongodb-keyfile sudo chown 999:999 keyfile/mongodb-keyfile Create a Docker Compose File Use the following Docker Compose configuration to initialize the ReplicaSet: version: '3.8' services: mongo-primary: image: mongo:latest container_name: mongo-primary command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile ports: - "27017:27017" volumes: - ./data/primary:/data/db - ./keyfile:/data/keyfile networks: - mongo-network restart: always mongo-secondary-1: image: mongo:latest container_name: mongo-secondary-1 command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile ports: - "27018:27017" volumes: - ./data/secondary1:/data/db - ./keyfile:/data/keyfile networks: - mongo-network restart: always depends_on: - mongo-primary mongo-secondary-2: image: mongo:latest container_name: mongo-secondary-2 command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile ports: - "27019:27017" volumes: - ./data/secondary2:/data/db - ./keyfile:/data/keyfile networks: - mongo-network restart: always depends_on: - mongo-primary networks: mongo-network: driver: bridge Start Docker Compose docker-compose up -d Configure the ReplicaSet Connect to the primary node and initialize the ReplicaSet: docker exec -it mongo-primary mongosh rs.initiate({ _id: "rs0", members: [ { _id: 0, host: "mongo-primary:27017", priority: 2 }, { _id: 1, host: "mongo-secondary-1:27017", priority: 1 }, { _id: 2, host: "mongo-secondary-2:27017", priority: 1 } ] }); Create an Admin User use admin; db.createUser({ user: "adminUser", pwd: "securePassword", roles: [ { role: "root", db: "admin" } ] }); MongoDB Connection String with Authentication (ReplicaSet + Public IP) In local mongodb://appUser:appPassword@mongo-primary:27017,mongo-secondary-1:27017,mongo-secondary-2:27017/myApp?replicaSet=rs0&authSource=myApp For public IP mongodb://appUser:appPassword@your_vps_ip:27017,your_vps_ip:27018,your_vps_ip:27019/myApp?replicaSet=rs0&authSource=myApp Conclusion With the above steps, you can easily and effectively deploy a MongoDB ReplicaSet with Docker. Don't forget to verify your connection and consider any additional security configurations as needed.

Apr 16, 2025 - 04:56
 0
Deploying MongoDB ReplicaSet with Docker (Public VPS IP and User Authentication Required)

Introduction

Deploying a MongoDB ReplicaSet with Docker efficiently builds a robust database system, ensuring high availability and fault tolerance. I'd like to share the detailed steps based on my real-world experience below.

So the other day, I was working on a project using NestJS + Prisma + MongoDB, and it turned out that Prisma requires a ReplicaSet to work properly.

Deployment Steps

  1. Create Directory Structure

    Prepare the necessary directories for data and configuration:

    mkdir -p mongodb-replicaset/data/{primary,secondary1,secondary2}
    cd mongodb-replicaset
    
    
  2. Create a Keyfile for Authentication

    The keyfile ensures secure communication between nodes.

    mkdir -p mongodb-replicaset/keyfile
    openssl rand -base64 756 > keyfile/mongodb-keyfile
    chmod 400 keyfile/mongodb-keyfile
    sudo chown 999:999 keyfile/mongodb-keyfile
    
    
  3. Create a Docker Compose File

    Use the following Docker Compose configuration to initialize the ReplicaSet:

    version: '3.8'
    
    services:
      mongo-primary:
        image: mongo:latest
        container_name: mongo-primary
        command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile
        ports:
          - "27017:27017"
        volumes:
          - ./data/primary:/data/db
          - ./keyfile:/data/keyfile
        networks:
          - mongo-network
        restart: always
    
      mongo-secondary-1:
        image: mongo:latest
        container_name: mongo-secondary-1
        command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile
        ports:
          - "27018:27017"
        volumes:
          - ./data/secondary1:/data/db
          - ./keyfile:/data/keyfile
        networks:
          - mongo-network
        restart: always
        depends_on:
          - mongo-primary
    
      mongo-secondary-2:
        image: mongo:latest
        container_name: mongo-secondary-2
        command: mongod --bind_ip_all --replSet rs0 --port 27017 --auth --keyFile /data/keyfile/mongodb-keyfile
        ports:
          - "27019:27017"
        volumes:
          - ./data/secondary2:/data/db
          - ./keyfile:/data/keyfile
        networks:
          - mongo-network
        restart: always
        depends_on:
          - mongo-primary
    
    networks:
      mongo-network:
        driver: bridge
    
    
  4. Start Docker Compose

    docker-compose up -d
    
    
  5. Configure the ReplicaSet

    Connect to the primary node and initialize the ReplicaSet:

    docker exec -it mongo-primary mongosh
    rs.initiate({
      _id: "rs0",
      members: [
        { _id: 0, host: "mongo-primary:27017", priority: 2 },
        { _id: 1, host: "mongo-secondary-1:27017", priority: 1 },
        { _id: 2, host: "mongo-secondary-2:27017", priority: 1 }
      ]
    });
    
    
  6. Create an Admin User

    use admin;
    db.createUser({
      user: "adminUser",
      pwd: "securePassword",
      roles: [ { role: "root", db: "admin" } ]
    });
    
    
  7. MongoDB Connection String with Authentication (ReplicaSet + Public IP)

In local

mongodb://appUser:appPassword@mongo-primary:27017,mongo-secondary-1:27017,mongo-secondary-2:27017/myApp?replicaSet=rs0&authSource=myApp

For public IP

mongodb://appUser:appPassword@your_vps_ip:27017,your_vps_ip:27018,your_vps_ip:27019/myApp?replicaSet=rs0&authSource=myApp

Conclusion

With the above steps, you can easily and effectively deploy a MongoDB ReplicaSet with Docker. Don't forget to verify your connection and consider any additional security configurations as needed.