Member-only story

Mongodb Replica Set with docker

Shailesh
6 min readFeb 19, 2023

--

Recently, I came across a problem with mongodb replica set where I was not able to connect to replicaSet using the replicaSet url. Probably many of you have already overcome this problem. But for some reason, I could not find a solution that exactly worked for me. In the process, I managed to find a solution that worked for me. Documenting here if it helps anyone else. I was using docker to setup the cluster. The steps to setup a mongodb replicaSet are quite straight forward

  1. Setup 3 mongo servers. (primary, secondary and the arbiter). In this docker setup, 3 containers are running an instance of mongo server each.
version: "3.8"

services:
mongo-1-2:
container_name: "mongo-1-2"
image: mongo:5.0
ports:
- "27022:27022"
command: mongod --replSet rs1 --port 27022 --bind_ip_all
restart: always
networks:
- mongors-network

mongo-1-3:
container_name: "mongo-1-3"
image: mongo:5.0
ports:
- "27023:27023"
command: mongod --replSet rs1 --port 27023 --bind_ip_all
restart: always
networks:
- mongors-network

mongo-1-1:
container_name: "mongo-1-1"
image: mongo:5.0
ports:
- "27021:27021"
command: mongod --replSet rs1 --port 27021 --bind_ip_all
links:
- mongo-1-2:mongo-1-2
- mongo-1-3:mongo-1-3
restart: always
networks:
- mongors-network…

--

--

Shailesh
Shailesh

Written by Shailesh

Solving Problems. Making Software Better. Intrigued by Elegant Solutions. https://twitter.com/agent_hunt

Responses (2)