Creating A MongoDB Replica Set Using Docker

Neville Bokdawalla
3 min readMar 6, 2021

A replica set in MongoDB is a group of database servers that maintain multiple copies of the same data. This helps in achieving redundancy and high availability of your data.

I will assume that you are already familiar with the following topics

Lets get started…

Download the latest official MongoDB image. Open your terminal, make sure docker is running and then run the command given below.

docker pull mongo

Create a custom virtual network in docker

We need to do this because we want all the servers in the replica set to be able to talk to each other. This simulates a real word scenario on our local machine where each MongoDB server is hosted in a separate physical/virtual server.

Run the command given below to create a virtual network named mongodb-local-replicaset

docker network create mongodb-local-replicaset

Now lets decide upon the number of servers we would like to have in our replica set

Since its recommended to have a minimum of 3 (1 primary and 2 secondary), I will go ahead with that number for the sake of demonstration here although the maximum you can have is 50 as per the official documentation at the time of writing this article.

Create 3 docker containers from the downloaded MongoDB image with each container representing a database server by running the commands given below.

docker run --name mongodb-local-1 --net mongodb-local-replicaset -d mongo --replSet "rs0"docker run --name mongodb-local-2 --net mongodb-local-replicaset -d mongo --replSet "rs0"docker run --name mongodb-local-3 --net mongodb-local-replicaset -d mongo --replSet "rs0"

The above commands create 3 MongoDB servers named mongodb-local-1,2 and 3.

It attaches them to the custom virtual network called mongodb-local-replicaset which we created earlier and sets the name of the replica set that each server is going to be a part of by passing it as an argument to the docker image name — mongo (the docker image is configured to pass in the argument to the mongod server process)

We should now have 3 containers running MongoDB servers in the background named mongodb-local-1, mongodb-local-2 and mongodb-local-3. Verify it by running the command given below.

docker ps -a

Time to initialize the replica set

Now that we have all the docker containers running with a MongoDB server, we need to connect to any 1 server via the mongo client executable in order to initialize the replica set.

Run the command given below to connect to the MongoDB server present within the container using the mongo client executable.

docker exec -it mongodb-local-1 mongo

This will create a client connection to the mongodb-local-1 server.

Now lets initiate the replica set specifying all the members by running the command given below

rs.initiate({ _id: "rs0", members: [{_id: 0, host: "mongodb-local-1:27017" }, {_id: 1, host: "mongodb-local-2:27017" },{_id: 2, host: "mongodb-local-3:27017" }]})

This will initiate the replica set with the value of _id as the same replica set name which we had given earlier while creating the containers and members containing the names of all the 3 containers along with the default port — 27017 on which the MongoDB server listens for client connections.

On successful execution, the above command gives the following output which indicates that the replica set is created successfully.

{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1615043092, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1615043092, 1)
}

To get more details on the replica set created, run the following commands and check the output.

// To get the entire configuration data of the replica set
rs.conf()
// To get the current status of the replica set
rs.status()

Hope you enjoyed reading this and were able to create a replica set by following all the steps mentioned in the article. Feel free to comment in case your stuck or have any doubts and I’ll be happy to help. Cheers! :)

Further Reading

--

--

Neville Bokdawalla

Software Engineer. Movie Buff. Love Instrumental Music. Always Open To Learning New Things. May The Force Be With You!👍