Skip to content

Instantly share code, notes, and snippets.

@jewzaam
Last active April 1, 2019 08:27
Show Gist options
  • Select an option

  • Save jewzaam/5a9496f1ca2e5feb07bb to your computer and use it in GitHub Desktop.

Select an option

Save jewzaam/5a9496f1ca2e5feb07bb to your computer and use it in GitHub Desktop.
MongoDB replica set in docker
# get it
docker pull mongo
# startup a 3 node replica set
docker run --name mongo-rs-1 -d mongo --nojournal --oplogSize 10 --replSet rs
docker run --name mongo-rs-2 -d mongo --nojournal --oplogSize 10 --replSet rs
docker run --name mongo-rs-3 -d mongo --nojournal --oplogSize 10 --replSet rs
# connect to first node
docker run -it --link mongo-rs-1:mongo1 --link mongo-rs-2:mongo2 --link mongo-rs-3:mongo3 --rm mongo /bin/bash
# find all IP's for later commands
MONGO1=`grep mongo1 /etc/hosts | awk '{print $1}'`
MONGO2=`grep mongo2 /etc/hosts | awk '{print $1}'`
MONGO3=`grep mongo3 /etc/hosts | awk '{print $1}'`
mongo mongo1:27017 --eval "rs.initiate(); myconf = rs.conf(); myconf.members[0].host = '$MONGO1:27017'; rs.reconfig(myconf,{force:true}); rs.add('$MONGO2:27017'); rs.add('$MONGO3:27017');"
# test it out!
# write new data on primary
mongo mongo1:27017/test --eval 'db.users.insert({"name":"Somebody", "city": "Raleigh"})'
# check data in secondary
mongo mongo2:27017/test --eval 'rs.slaveOk(); db.users.find().forEach(function(x){printjson(x);})'
# exit docker client container
exit
@jewzaam
Copy link
Copy Markdown
Author

jewzaam commented Nov 20, 2014

Mongo needs the IP address for each node and for the primary the host needs to be set to the IP + port. Docker container hostnames are the container hash.

@jewzaam
Copy link
Copy Markdown
Author

jewzaam commented Nov 20, 2014

@shubhag
Copy link
Copy Markdown

shubhag commented Apr 12, 2016

I am trying to link nodejs with mongodb replica set.
If there is only one mongo container having name mongo, then
docker run -P --name web1 --link mongo:mongo server

but for mongo replica set, I am unable to get how to link it with the server

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