9951 explained code solutions for 126 technologies


mongodbHow to set up a MongoDB replica with Docker Compose?


Setting up a MongoDB replica with Docker Compose is a straightforward process.

  1. Create a docker-compose.yml file with the following content:
version: '3'
services:
  mongo-primary:
    image: mongo
    ports:
      - 27017:27017
    volumes:
      - ./data/primary:/data/db
    command: mongod --replSet rs0
  mongo-secondary:
    image: mongo
    ports:
      - 27018:27017
    volumes:
      - ./data/secondary:/data/db
    command: mongod --replSet rs0
  1. Start the containers with docker-compose up -d

  2. Connect to the primary container with docker exec -it mongo-primary mongo

  3. Initialize the replica set with the following command:

rs.initiate(
  {
    _id: "rs0",
    members: [
      { _id: 0, host: "mongo-primary:27017" },
      { _id: 1, host: "mongo-secondary:27017" }
    ]
  }
)
  1. Verify the replica set is working with rs.status()

Helpful links

Edit this code on GitHub