Skip to content

Instantly share code, notes, and snippets.

@ismell
Last active September 10, 2016 15:39
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ismell/6281967 to your computer and use it in GitHub Desktop.
Save ismell/6281967 to your computer and use it in GitHub Desktop.
Docker Ubuntu upstart scripts

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
@deinspanjer
Copy link

To get this to work with images that are built with a CMD default, I replaced the docker run in line 49 with:

    if [ "${IMAGE}" = "${CMD}" ]; then
      ID=$(docker run -d "${IMAGE}")
    else
      ID=$(docker run -d "${IMAGE}" ${CMD})
    fi

@ismell
Copy link
Author

ismell commented Oct 17, 2013

The script should be fixed to start with the default command now.

@bryanlarsen
Copy link

Thanks for the starting point. I had two problems with your script. 1) as written, it prefers to run old containers rather than pulling new ones when appropriate. Deleting the 'start' clause fixes that. 2) One of my major reasons for using upstart is because it does logs better than docker. So I changed your docker run -d into an exec docker run, and then used ps -l -q in a post-start script to create the run file.

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