Skip to content

Instantly share code, notes, and snippets.

@wyaeld
Forked from ismell/README.md
Created July 14, 2014 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyaeld/b837bf22dc37c3e4207d to your computer and use it in GitHub Desktop.
Save wyaeld/b837bf22dc37c3e4207d to your computer and use it in GitHub Desktop.

Production Ready Process Monitoring

  1. Install docker-manger.conf and docker-instance.conf in /etc/init

  2. Create a containers file in /etc/docker/ with the following format

     name: image cmd
    

    The name must be unique.

  3. sudo service docker-manager start

  4. You should now have upstart monitoring your container.

  5. Use docker stop to kill your container and notice it gets restarted

Due to a bug in upstart (https://bugs.launchpad.net/upstart/+bug/568288) doing service docker-instance stop NAME=XXXX fails to stop the instance. I'm not actually sure how to work around this.

nginx-0: docker.wdig.com/cws/base-service nginx
nginx-1: docker.wdig.com/cws/base-service nginx
nginx-2: docker.wdig.com/cws/base-service nginx
description "Docker Container"
stop on stopping docker
# Docker has a timeout of 10 seconds so as long as this
# is longer so we don't kill the wait process
kill timeout 20
# We don't want to TERM the `docker wait` process so we fake the signal
# we send to it. The pre-stop script issues the `docker stop` command
# which causes the `docker wait` process to exit
kill signal CONT
# Due to a bug in upstart we need to set the modes we consider
# successful exists https://bugs.launchpad.net/upstart/+bug/568288
normal exit 0 CONT
respawn
instance ${NAME}
pre-start script
[ ! -f /etc/docker/containers ] && { stop; exit 0; }
if ! grep -q "^${NAME}:" /etc/docker/containers; then
stop
exit 0
fi
end script
script
LINE=$(grep "^${NAME}:" /etc/docker/containers | cut -d: -f2-| sed 's/^\s*//')
IMAGE=$(echo "$LINE" | cut -d\ -f1)
CMD=$(echo "$LINE" | cut -d\ -f2- -s)
[ -f "/var/run/docker/${NAME}.cid" ] && { ID="$(cat /var/run/docker/${NAME}.cid)"; }
START=1
if [ ! -z "${ID}" ]; then
if docker ps | grep -q "${ID}"; then
START=0
else
NID=$(docker start ${ID})
[ "${NID}" = "${ID}" ] && START=0
fi
fi
if [ $START -ne 0 ]; then
ID=$(docker run -d "${IMAGE}" ${CMD})
echo ${ID} > /var/run/docker/${NAME}.cid
fi
exec docker wait ${ID}
end script
pre-stop script
[ -f "/var/run/docker/${NAME}.cid" ] && { ID="$(cat /var/run/docker/${NAME}.cid)"; }
if [ ! -z "${ID}" ]; then
docker stop "${ID}"
fi
end script
description "Manage Docker Containers"
start on started docker
stop on stopping docker
respawn
pre-start script
# Directory to store run data
[ ! -d /var/run/docker ] && mkdir -p /var/run/docker
# Directory to store config
[ ! -d /etc/docker ] && mkdir -p /etc/docker
[ ! -f /etc/docker/containers ] && { stop; exit 0; }
while read line; do
NAME=$(echo "$line" | cut -d: -f 1)
start docker-instance NAME="$NAME"
done < /etc/docker/containers
end script
post-stop script
for inst in `initctl list | grep "^docker-instance "|awk '{print $2}'|tr -d ')'|tr -d '('`
do
stop docker-instance NAME=$inst
done
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment