docker-compose-rs

Learn how to set up a Docker Compose MongoDB replica set (rs0) with multiple nodes and MongoDB Express for easy management. This guide provides a ready-to-use docker-compose.yml file.

Docker Compose MongoDB Replica Set

This document provides a Docker Compose configuration to set up a MongoDB replica set named rs0. A replica set ensures high availability and data redundancy for your MongoDB deployments. This setup includes three MongoDB nodes and MongoDB Express for easy web-based administration.

Docker Compose Configuration for MongoDB Replica Set

The following docker-compose.yml file defines the services for our MongoDB replica set. Each service represents a MongoDB instance, and one service is dedicated to MongoDB Express for managing the cluster.

MongoDB Replica Set Nodes

We define three MongoDB services (mongodb-rs-0, mongodb-rs-1, and mongodb-rs-2) to form the replica set. Each node is configured to join the rs0 replica set and uses specific ports.

MongoDB Express for Management

The mongodb-express service provides a user-friendly interface to connect to and manage your MongoDB replica set. It's configured to connect to all replica set members.

version: '2'

services:
  mongodb-rs-0:
    image: mongo:4.2
    container_name: mongodb-rs-0
    restart: unless-stopped
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "30001"]
    #environment:
    #  - MONGO_INITDB_ROOT_USERNAME=${MONGODB_ROOT_USERNAME}
    #  - MONGO_INITDB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
    volumes:
      - ${MONGODB_DATA}/mongodb-rs-0/data/db:/data/db
      - ${MONGODB_DATA}/mongodb-rs-0/data/backups:/dump
    ports:
     - 30001:30001
    networks:
      - public
    depends_on:
      - mongodb-rs-1
      - mongodb-rs-2
    healthcheck:
      test: test $$(echo "rs.initiate({_id:'rs0',members:[{_id:0,host:\"mongodb-rs-0:30001\"},{_id:1,host:\"mongodb-rs-1:30002\"},{_id:2,host:\"mongodb-rs-2:30003\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1
      interval: 15s
      timeout: 10s
      start_period: 30s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "1m"

  mongodb-rs-1:
    image: mongo:4.2
    container_name: mongodb-rs-1
    restart: unless-stopped
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "30002"]
    #environment:
    #  - MONGO_INITDB_ROOT_USERNAME=${MONGODB_ROOT_USERNAME}
    #  - MONGO_INITDB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
    volumes:
      - ${MONGODB_DATA}/mongodb-rs-1/data/db:/data/db
      - ${MONGODB_DATA}/mongodb-rs-1/data/backups:/dump
    ports:
     - 30002:30002
    networks:
      - public
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:30002/test --quiet
      interval: 60s
      timeout: 10s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "1m"

  mongodb-rs-2:
    image: mongo:4.2
    container_name: mongodb-rs-2
    restart: unless-stopped
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "30003"]
    #environment:
    #  - MONGO_INITDB_ROOT_USERNAME=${MONGODB_ROOT_USERNAME}
    #  - MONGO_INITDB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
    volumes:
      - ${MONGODB_DATA}/mongodb-rs-2/data/db:/data/db
      - ${MONGODB_DATA}/mongodb-rs-2/data/backups:/dump
    ports:
     - 30003:30003
    networks:
      - public
    healthcheck:
      test: echo 'db.runCommand("ping").ok' | mongo localhost:30003/test --quiet
      interval: 60s
      timeout: 10s
      retries: 3
    logging:
      driver: "json-file"
      options:
        max-size: "1m"

  mongodb-express:
    image: mongo-express
    container_name: mongo-express
    environment:
      - ME_CONFIG_MONGODB_URL=mongodb://mongodb-rs-0:30001,mongodb-rs-1:30002,mongodb-rs-2:30003/?replicaSet=rs0
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
    #  - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGODB_ROOT_USERNAME}
    #  - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGODB_ROOT_PASSWORD}
      - ME_CONFIG_BASICAUTH_USERNAME=${MONGODB_EXPRESS_USERNAME}
      - ME_CONFIG_BASICAUTH_PASSWORD=${MONGODB_EXPRESS_PASSWORD}
    ports:
      - 18086:8081
    networks:
      - public
    depends_on:
      - mongodb-rs-0
      - mongodb-rs-1
      - mongodb-rs-2
    logging:
      driver: "json-file"
      options:
        max-size: "1m"

networks:
  public:
    name: public

Setting Up Your Environment

Before running this Docker Compose file, ensure you have Docker and Docker Compose installed. You may also want to set up environment variables for MongoDB credentials and MongoDB Express authentication. For detailed instructions on setting up MongoDB replica sets with Docker, refer to the official MongoDB Replication documentation and Docker Compose documentation.

Running the Replica Set

Save the configuration above as docker-compose.yml. Then, navigate to the directory containing the file in your terminal and run:

docker-compose up -d

This command will start all the defined services in detached mode. You can then access MongoDB Express at http://localhost:18086.

Further Considerations

For production environments, consider implementing robust security measures, persistent storage solutions, and advanced monitoring. Understanding MongoDB's replication topology and election processes is crucial for managing replica sets effectively. You can find more information on MongoDB community forums and Stack Overflow.