Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raulingg/260f552bcae9606afd7becf79880edfc to your computer and use it in GitHub Desktop.
Save raulingg/260f552bcae9606afd7becf79880edfc to your computer and use it in GitHub Desktop.
[Local mongo replicaset with docker] #docker #mongo
# pull the official mongo docker container
docker pull mongo
# create network
docker network create mongo-cluster
# create mongos
docker run -d --net mongo-cluster -p 27017:27017 --name mongo-primary mongo mongod --replSet mongo-set --port 27017
docker run -d --net mongo-cluster -p 27018:27018 --name mongo-secondary mongo mongod --replSet mongo-set --port 27018
# add hosts
127.0.0.1 mongo-primary mongo-secondary
# setup replica set
docker exec -it mongo-primary mongo
db = (new Mongo('localhost:27017')).getDB('laboratoria-api')
config={"_id":"mongo-set","members":[{"_id":0,"host":"mongo-primary:27017"},{"_id":1,"host":"mongo-secondary:27018"}]}
rs.initiate(config)
# connection URI
mongodb://localhost:27017,localhost:27018/{db}?replicaSet=mongo-set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment