Home Docker MongoDB multiple node replica set
Post
Cancel

Docker MongoDB multiple node replica set

Instructions

The steps to create a docker cluster are as follows.

  • Create a Docker network.
  • Start three instances of MongoDB.
  • Initiate the Replica Set.

Once you have a MongoDB cluster up and running, you will be able to experiment with it.

How it will look like:

graph LR
    Application[Application] -- Read --> MongoDB_1(MongoDB)
    Application[Application] -- Write --> MongoDB_1(MongoDB)
    MongoDB_1 -- replication --> MongoDB_2
    MongoDB_1 -- replication --> MongoDB_3

Create a Docker Network

1
docker network create mongodb-network

Run MongoDB in docker

1
2
3
docker run -d -p 27017:27017 --name mongodb  --network mongodb-network mongo:6.0.4 mongod --replSet myReplicaSet --bind_ip localhost,mongodb
docker run -d -p 27018:27017 --name mongodb2 --network mongodb-network mongo:6.0.4 mongod --replSet myReplicaSet --bind_ip localhost,mongodb2
docker run -d -p 27019:27017 --name mongodb3 --network mongodb-network mongo:6.0.4 mongod --replSet myReplicaSet --bind_ip localhost,mongodb3

Initiate replica set

1
2
3
4
5
6
7
8
docker exec -it mongodb mongosh --eval "rs.initiate({
 _id: \"myReplicaSet\",
 members: [
   {_id: 0, host: \"mongodb\"},
   {_id: 1, host: \"mongodb2\"},
   {_id: 2, host: \"mongodb3\"}
 ]
})"

Test and verify replica set

1
docker exec -it mongodb mongosh --eval "rs.status()"

References

This post is licensed under CC BY 4.0 by the author.