Skip to content

Instantly share code, notes, and snippets.

@bketelsen
Created February 25, 2014 16:21
Show Gist options
  • Save bketelsen/9212213 to your computer and use it in GitHub Desktop.
Save bketelsen/9212213 to your computer and use it in GitHub Desktop.
bash script to start N docker containers, stop old versions, and update confd
#!/bin/bash
if [ -z "$1" ]
then
echo "usage : gophercon.sh 3 -- start three new instances"
exit -1
fi
echo "Getting currently running gophercon containers"
OLDPORTS=( `docker ps | grep gophercon | awk '{print $1}'` )
echo "starting new containers"
for i in `seq 1 $1` ; do
echo "inside loop $1"
JOB=`docker run -d -p 80 quay.io/bketelsen/gophercon | cut -c1-12`
echo $JOB
PORT=`docker inspect $JOB | grep HostPort | cut -d '"' -f 4 | head -1`
curl http://127.0.0.1:4001/v2/keys/gophercon/upstream/$JOB -XPUT -d value="127.0.0.1:$PORT"
done
echo "removing old containers"
for i in ${OLDPORTS[@]}
do
etcdctl rm /gophercon/upstream/$i
confd -onetime
docker kill $i
done
@bketelsen
Copy link
Author

This script starts N new containers of the gophercon docker image, registers them with etcd, removes old containers, and restarts nginx (through confd's script)

This script assumes the following:

  1. Etcd installed locally
  2. confd installed in $PATH, configured according to https://github.com/kelseyhightower/confd/blob/master/docs/templates-interation-example.md
  3. nginx installed locally

thoughts/comments/improvements on my crappy first bash script?
Worth a blog post?

@kelseyhightower
Copy link

Yes!

@bketelsen
Copy link
Author

Oh, this is confd:master, not the last release, you'll need to build from source until Kelsey makes a new release.

@bketelsen
Copy link
Author

next steps? maybe put this in an nginx/confd combined container, make confd run as a daemon beside nginx

@bketelsen
Copy link
Author

could easily make the docker image a shell argument too

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