DEV Community

Võ Tấn Đạt
Võ Tấn Đạt

Posted on

2

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
Enter fullscreen mode Exit fullscreen mode

For public IP

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

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.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this compelling article, highly praised by the collaborative DEV Community. All developers, whether just starting out or already experienced, are invited to share insights and grow our collective expertise.

A quick “thank you” can lift someone’s spirits—drop your kudos in the comments!

On DEV, sharing experiences sparks innovation and strengthens our connections. If this post resonated with you, a brief note of appreciation goes a long way.

Get Started