Skip to content

Instantly share code, notes, and snippets.

@groovyghoul
Last active March 25, 2016 04:03
Show Gist options
  • Save groovyghoul/804313d067170fd8c445 to your computer and use it in GitHub Desktop.
Save groovyghoul/804313d067170fd8c445 to your computer and use it in GitHub Desktop.
Spin up 3 Docker containers with MongoDb replicaSet
#!/bin/bash
mkdir -p ~/mongodb/data
# Define Hostnames
h1="bytemares1"
h2="bytemares2"
h3="bytemares3"
# Start containers with specific hostnames and replica set name
docker run -d -P -p 33331:27017 --name bytemares1 -v ~/mongodb/data:/data/db --hostname="$h1" mongo mongod --replSet rset --noprealloc --smallfiles
docker run -d -P -p 33332:27017 --name bytemares2 --hostname="$h2" mongo mongod --replSet rset --noprealloc --smallfiles
docker run -d -P -p 33333:27017 --name bytemares3 --hostname="$h3" mongo mongod --replSet rset --noprealloc --smallfiles
# Commands to extract IP addresses of containers
echo $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares1) "$h1" > getip.txt
echo $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares2) "$h2" >> getip.txt
echo $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares3) "$h3" >> getip.txt
# Commands to cp getip.txt to containers
docker cp getip.txt bytemares1:/etc
docker cp getip.txt bytemares2:/etc
docker cp getip.txt bytemares3:/etc
# Commands to create new file updateHost.sh (to update /etc/hosts file in all the three docker containers)
echo "#!/bin/bash
cat /etc/hosts > /etc/hosts1
sed -i '/ttnd.com/d' /etc/hosts1
cat /etc/getip.txt >> /etc/hosts1
cat /etc/hosts1 > /etc/hosts" > updateHost.sh
# Change permission of updateHost.sh and cp files to docker container
chmod +x updateHost.sh
docker cp updateHost.sh bytemares1:/etc
docker cp updateHost.sh bytemares2:/etc
docker cp updateHost.sh bytemares3:/etc
docker exec -it bytemares1 chmod +x /etc/updateHost.sh
docker exec -it bytemares2 chmod +x /etc/updateHost.sh
docker exec -it bytemares3 chmod +x /etc/updateHost.sh
# Execute scripts on all the three containers
docker exec -it bytemares1 /etc/updateHost.sh
docker exec -it bytemares2 /etc/updateHost.sh
docker exec -it bytemares3 /etc/updateHost.sh
# Start MongoDB Replica Set with Primary
docker exec -it bytemares1 mongo --eval "rs.status()"
docker exec -it bytemares1 mongo --eval "db"
docker exec -it bytemares1 mongo --eval "rs.initiate()"
printf "\n\nWaiting 2 minutes to add slaves to replicaSet\n"
TIMER=1
until [ $TIMER -eq 60 ]; do
printf '.'
sleep 1
TIMER=$((TIMER + 1))
done
echo " Adding slaves"
#sleep 120
docker exec -it bytemares1 mongo --eval "rs.add(\"$h2:27017\")"
docker exec -it bytemares1 mongo --eval "rs.add(\"$h3:27017\")"
docker exec -it bytemares2 mongo --eval "rs.slaveOk()"
docker exec -it bytemares3 mongo --eval "rs.slaveOk()"
echo ""
echo "=============================="
echo "Display IPs for new containers"
echo "=============================="
echo ""
MONGODB1=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares1)
MONGODB2=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares2)
MONGODB3=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares3)
echo $MONGODB1
echo $MONGODB2
echo $MONGODB3
echo ""
#sudo iptables -t nat -L -n
echo ""
BYTEMARES1_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares1)
BYTEMARES2_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares2)
BYTEMARES3_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' bytemares3)
BYTEMARES1_PORT=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' bytemares1)
BYTEMARES2_PORT=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' bytemares2)
BYTEMARES3_PORT=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' bytemares3)
#docker port bytemares1
#docker port bytemares2
#docker port bytemares3
echo "====================================="
echo "Mongo replicaSet instances running at"
echo "====================================="
echo "$BYTEMARES1_IP $BYTEMARES1_PORT"
echo "$BYTEMARES2_IP $BYTEMARES2_PORT"
echo "$BYTEMARES3_IP $BYTEMARES3_PORT"
#!/bin/bash
echo "==================="
echo "Stopping containers"
echo "==================="
echo ""
docker stop bytemares1
docker stop bytemares2
docker stop bytemares3
echo ""
echo "==================="
echo "Removing containers"
echo "==================="
echo ""
docker rm bytemares1
docker rm bytemares2
docker rm bytemares3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment