Last active
April 1, 2019 08:27
-
-
Save jewzaam/5a9496f1ca2e5feb07bb to your computer and use it in GitHub Desktop.
MongoDB replica set in docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
http://stackoverflow.com/questions/24252598/how-to-setup-linkage-between-docker-containers-so-that-restarting-wont-break-it