Post

MongoDB single node replica set with Docker

Instructions

The steps to create a docker cluster are as follows.

  • Start one instance 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(MongoDB)
    Application[Application] -- Write --> MongoDB(MongoDB)

Run MongoDB in docker

1
docker run -d -p 27017:27017 --name mongodb mongo:6.0.4 mongod --replSet myReplicaSet

Initiate replica set

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

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.