A way to run a Mongo replica set locally using Docker with minimal configuration required.
APACHE-2.0 License
⚠️ This setup is purely for local development purposes.
This setup should not be used for production applications as it was not built with that in mind.
This docker-compose
setup starts a local mongo replica set with 3 instances running on:
/etc/hosts
file:127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
Simples:
docker-compose up -d
docker exec -it mongo1 sh -c "mongo --port 30001"
docker exec -it mongo2 sh -c "mongo --port 30002"
docker exec -it mongo3 sh -c "mongo --port 30003"
rs.initiate(
{
_id : 'my-replica-set',
members: [
{ _id : 0, host : "mongo1:30001" },
{ _id : 1, host : "mongo2:30002" },
{ _id : 2, host : "mongo3:30003" }
]
}
)
my-replica-set
and start talking to each otherPRIMARY
and the other two become SECONDARY
instancesI used Robo 3T to test it locally and used the following config for the connection:
mongodb://mongo1:30001,mongo2:30002,mongo3:30003/?replicaSet=my-replica-set