Instant 3-Node Mongo Replication Cluster Using Docker

The aim of this post is to show you how easy it is to setup a 3-Node Mongo Replication Cluster. Assuming Docker is installed on the system(s) let’s go ahead.

1: Create Docker Network

Run the below command

$ docker network create mongo-cluster

The above command creates a network called ‘mongo-cluster’, we will use this cluster for all our mongo instances.

You can list all available networks using the below command

$ docker network ls

2: Create data folders on the host

Docker containers are ephemeral, i.e once the container is removed, the data is lost forever. To mitigate this, we will create folders on the host machine and then ask docker to use this (we mount this folder, so that it’s available inside the container)

$ mkdir -p data/mongo_01 data/mongo_02 data/mongo_03

This will create mongo_01, mongo_02 and mong0_03 directories inside data folder (at the current directory, use pwd to check the current working directory)

3: Create Mongo docker instances

We like to use stable OS’s that are lightweight and one such popular OS is the alpine linux

Create Mongo_01 instance

docker run \
-d \
-p 5001:27017 \
-v ~/data/mongo_01:/data/db \
 — name mongo_01 \
 — net mongo-cluster \
mvertes/alpine-mongo mongod — replSet mongo-replica-set — dbpath /data/db

Create Mongo_02 instance

docker run \
-d \
-p 5002:27017 \
-v ~/data/mongo_02:/data/db \
 — name mongo_02 \
 — net mongo-cluster \
mvertes/alpine-mongo mongod — replSet mongo-replica-set — dbpath /data/db

Create Mongo_03 instance

docker run \
-d \
-p 5003:27017 \
-v ~/data/mongo_03:/data/db \
 — name mongo_03 \
 — net mongo-cluster \
mvertes/alpine-mongo mongod — replSet mongo-replica-set — dbpath /data/db


We exposed 3 ports 5001, 5002 and 5003 on our host machine.

We are using the ‘mongo-cluster’ network we created in step 1.

We are using ‘mongo-replica-set’ for all of our instance, make sure it’s same for all as we will using it in the next step.

4: Enable Replication

Let’s get inside one of the container and setup the replication. Execute the below command.

$ docker exec -it mongo_01 mongo

We will now be inside the mongo_01 container and will be using the mongo shell.

We need to be inside a db, to setup the replication configuration, execute the below command, the db name I used is default, you can set it to anything, as this db will not be created.

> db = (new Mongo(‘localhost:27017’)).getDB(‘default’)

Next, create a config linking all the mongo instance.

config = {
 “_id” : “mongo-replica-set”,
 “members” : [
 “_id” : 0,
 “host” : “mongo_01:27017”
 “_id” : 1,
 “host” : “mongo_02:27017”
 “_id” : 2,
 “host” : “mongo_03:27017”

Finally, run the below command to initiate the replication using the above config.

> rs.initiate(config)

That’s it, replication is setup, and your data will be synchronized across all the instances.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.