Here is the simplest way to create a 3-node MongoDb replica set using Docker. These instructions were created using Docker on Windows 10 and setup to use Linux containers.
Step 1: Create data volumes for each node
Step 2: Create a file called docker-compose.yml
Step 3: Start the Docker containers
docker-compose.exe up -d
Step 4: Start an interactive MongoDb shell
docker exec -it mongo0 mongo --port 30000
Step 5: Configure the replica set
From the MongoDb shell, type (or paste) the following
If you use this, you probably have to update your hosts
file, as well.
On Windows you can find it at:
C:\Windows\System32\drivers\etc\hosts
Add
127.0.0.1 mongo0 mongo1 mongo2
to the file and save it.
Step 6: Initiate the replica set
Still in the MongoDb shell, type (or paste) the following
End result
You should see the MongoDb shell switch to SECONDARY and if you hit Enter a few times it will switch to PRIMARY (it may start out as primary, as well).
You can exit the interactive MongoDb session with quit()
or juct ctrl-c
Now you should be able to connect to the replica set using the following connection string
mongodb://localhost:30000,localhost:30001,localhost:30002/?replicaSet=rs0