Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[Local mongo replicaset with docker] #docker #mongo
# pull the official mongo docker container
docker pull mongo
# create network
docker network create my-mongo-cluster
# create mongos
docker run -d --net my-mongo-cluster -p 27017:27017 --name mongo1 mongo mongod --replSet my-mongo-set --port 27017
docker run -d --net my-mongo-cluster -p 27018:27018 --name mongo2 mongo mongod --replSet my-mongo-set --port 27018
docker run -d --net my-mongo-cluster -p 27019:27019 --name mongo3 mongo mongod --replSet my-mongo-set --port 27019
# add hosts mongo1 mongo2 mongo3
# setup replica set
docker exec -it mongo1 mongo
db = (new Mongo('localhost:27017')).getDB('test')
# connection URI
Copy link

max-young commented May 16, 2019


Copy link

jayveersolanki commented Jun 7, 2019

how to connect this replication set on another pc (client side) ?

Copy link

crapthings commented Jun 28, 2019

how to write a docker-compose file?

Copy link

harveyconnor commented Jul 5, 2019

Copy link

gsantosgonzalez commented Mar 18, 2020

Excelent, thanks a lot. I didn't add mongos to the hosts file.

Copy link

SuddenGunter commented Mar 31, 2020

worked on local linux machine, but for robo3t connection I used mongo1, mongo2 and mongo3 names.
didn't work when try connect using localhost:...

Copy link

harveyconnor commented Apr 1, 2020

@SuddenGunter You'll need to add mongo1 mongo2 mongo3 to your hosts file

Copy link

Tariqs commented Mar 2, 2021

This replicaSet is created on a single machine. Can you please point to a resource where the writer has used 3 different machines?

Copy link

MahdadGhasemian commented Jun 8, 2021


Copy link

ddo88 commented Aug 27, 2021

I love you bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment