MongoDB Replica Set with Docker-compose

Ojo joseph
2 min readJan 14, 2024

--

picture credit: MongoDb

This post walks you through how to set up a Mongo db replica set with docker-compose. This assumes some familiarity with Docker and MongoDB. It’s worth noting that for production setups, it’s advisable to distribute replica set nodes across multiple servers. Before diving in, here is a brief intro to replica set.

What is a Replica Set?

A MongoDB replica set is a group of interconnected separate instances of MongoDB that work together as one to provide high availability and fault tolerance. In a replica set, one node is elected as the primary to handle write operations while the rest serve as secondaries and replicate its data. In case of a primary node failure, a secondary node steps up to take over as the new primary, guaranteeing high availability.

To create this, the following files will be created in your project folder

setup.sh

openssl rand -base64 756 > ${PWD}/rs_keyfile
chmod 0400 ${PWD}/rs_keyfile
chown 999:999 ${PWD}/rs_keyfile

Next, create a docker-compose.yml file with the following configuration:

version: "3.8"

services:
mongo1:
container_name: mongo1
image: mongo:7.0
command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017", "--keyFile", "/etc/mongodb/pki/keyfile"]
restart: always
ports:
- 27017:27017
networks:
mongo-cluster:
ipv4_address: 111.222.32.2
volumes:
- ${PWD}/rs_keyfile:/etc/mongodb/pki/keyfile
- "mongo1_data:/data/db"
- "mongo1_config:/data/configdb"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example



mongo2:
image: mongo:7.0
container_name: mongo2
command: ["--replSet", "rs0" ,"--bind_ip_all", "--port", "27018","--keyFile", "/etc/mongodb/pki/keyfile"]
restart: always
ports:
- 27018:27018
networks:
mongo-cluster:
ipv4_address: 111.222.32.3
volumes:
- ${PWD}/rs_keyfile:/etc/mongodb/pki/keyfile
- "mongo2_data:/data/db"
- "mongo2_config:/data/configdb"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example



mongo3:
image: mongo:7.0
container_name: mongo3
command: ["--replSet", "rs0" ,"--bind_ip_all", "--port", "27019","--keyFile", "/etc/mongodb/pki/keyfile"]
restart: always
ports:
- 27019:27019
networks:
mongo-cluster:
ipv4_address: 111.222.32.4
volumes:
- ${PWD}/rs_keyfile:/etc/mongodb/pki/keyfile
- "mongo3_data:/data/db"
- "mongo3_config:/data/configdb"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example



volumes:
mongo1_data:
mongo2_data:
mongo3_data:
mongo1_config:
mongo2_config:
mongo3_config:

networks:
mongo-cluster:
ipam:
config:
- subnet: 111.222.32.0/24

Initializing the Replica Set:

  1. Generate authentication key file bash setup.sh
  2. Start the containers: docker-compose up -d
  3. Bash into one container: docker exec -it mongo1 bash
  4. Run mongosh -u root -p example
  5. Initiate the replica set using:
 rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27018" },
{ _id: 2, host: "mongo3:27019" }
]
}
)

you can confirm replica status using rs.status()

Note:

  1. you might need to wait a few seconds for the primary to be elected
  2. if you have trouble connecting to it via compass, you'll need to add the container ip addresses to your etc/hosts in this format ip_adress container_name
  3. connection string: mongodb://root:example@localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0&authSource=admin

Congratulations! You’ve successfully set up a MongoDB replica set with Docker Compose.

--

--