Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 1.26 KB

how-to-set-up-a-mongodb-replica-with-docker-compose.md

File metadata and controls

54 lines (43 loc) · 1.26 KB

How to set up a MongoDB replica with Docker Compose?

// plain

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

group: docker

onelinerhub: How to set up a MongoDB replica with Docker Compose?