MongoDB Replica Set in Docker

Dmytro Nelipa
4 min readJul 13, 2021

--

THIS GUIDE IS NOT MADE FOR PRODUCTION
USE IT CAREFULLY, ONLY FOR DEVELOPMENT PURPOSES

OR JUST DON’T FORGET TO CONFIGURE YOUR REPLICA SET PROPERLY AND MAKE IT SECURE

To create a MongoDB Replica Set in Docker, first, we need to install Docker :)
You may pass to the second step, if you’re already have it installed

1. Installation

1.1. Docker installation

Official “Install Docker Engine” page at Docker Docs

To get an actual guide and install Docker, we need to visit the docs:
https://docs.docker.com/engine/install/

I use Arch Linux, so my guide was there:
https://wiki.archlinux.org/title/docker

1.2. Docker Compose installation

In this guide we will write compose.yml file, which will help us to deploy the replica set easily, so we need to install Docker Compose utility to use it.

The Guide is available there: https://docs.docker.com/compose/install/

Official “Install Compose” page at Docker Docs

Furthermore, instead of an official guide, you can use many other guides, which you can find just by typing “Docker Compose [Distro Name]” in any search engine:

Ease of searching and the results :)

2. Deploy & Configure

2.1. Deploying the Replica Set

I’ve created .docker folder in my home directory and splitted it into data and compose folders, because it gives me easy access to docker containers’ files and other compose.yml files. I just find it useful, but you can do that too.

In our compose.yml file, we will use .docker folder (for container volumes)

So, our compose.yml:

Don’t forget to change {your_username} on your actual username
To create volumes, you can use any available option, directory, etc.

NOTICE
In this guide, I keep containers’ data in ~/.docker/data and compose files in ~/.docker/compose

Then, we need to edit /etc/hosts file:

Let’s get our containers up and make them work:

docker-compose -f ~/.docker/compose/mongo.yml up -d
The result of executing docker-compose command

2.2. Make our replica set work!

To make it work, we need to tell one of our future replica set members to join together into one replica set with other mongo db instances.

Let’s create config and initiate a replica set by using it

$ docker exec -it mongo-0-a mongo --port 27017> config = {"_id" : "rs0", "members" : [{"_id" : 0,"host" : "mongo-0-a:27017"},{"_id" : 1,"host" : "mongo-0-b:27017"},{"_id" : 2,"host" : "mongo-0-c:27017"}]}{
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "mongo-0-a:27017"
},
{
"_id" : 1,
"host" : "mongo-0-b:27017"
},
{
"_id" : 2,
"host" : "mongo-0-c:27017"
}
]
}
> rs.initiate(config)
{ “ok” : 1 }

If you did everything correct, you will get { "ok": 1 } as a result.

Then, you can use rs.status() to get info about your replica set
In addition, you will see info about replica members, so you can find out, which member has primary state. Moreover, you can use db.hello(); (deprecated method db.isMaster();) that describes the role of the mongod instance.

3. Testing the connection

3.1. Connection setup

Here I use Robo 3T MongoDB client to demonstrate the connectivity

We need to add all the members of our replica set and enter its name — rs0 (It’s important to use the same name, that you entered in config)

Connection configuration

Then we will test it out, by pressing the “Test” button in the left bottom corner of configuration window:

Test results window

So, below you can see that each member is available to our Robo 3T client, as well as our databases:

That’s it! Have a nice day and good luck ;)

--

--

Dmytro Nelipa
Dmytro Nelipa

Written by Dmytro Nelipa

0 Followers

Full-Stack Software Engineer, BSCyS

No responses yet