How do I auto-start docker containers at system boot?
Apparently, the current method to auto-start Docker containers (from Docker 1.2) is to use restart policies. This will control how Docker should handle starting of the container upon startup and re-starting of the container when it exits. I've used the 'always' option so far, and can confirm that it makes Docker auto-start the container at system boot:
sudo docker run --restart=always -d myimage
Restart Policies Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.
no - Do not restart the container when it exits.
on-failure - Restart the container only if it exits with a non zero exit status.
always - Always restart the container regardless of the exit status.
You can also specify the maximum amount of times Docker will try to restart the container when using the on-failure policy. The default is that Docker will try forever to restart the container.
$ sudo docker run --restart=always redis
This will run the redis container with a restart policy of always so that if the container exits, Docker will restart it.
$ sudo docker run --restart=on-failure:10 redis
This will run the redis container with a restart policy of on-failure and a maximum restart count of 10. If the redis container exits with a non-zero exit status more than 10 times in a row Docker will abort trying to restart the container. Providing a maximum restart limit is only valid for the on-failure policy.
Docker has this page that explains how to do it with upstart and systemd. I agree that it doesn't seem like the right thing for Docker. Their solution is to run
docker start, which assumes that you've already created your container. I would think that you'd either do
docker run --rm in the upstart script (treating it like a brand new process and container from an image) or just let the docker daemon restart the containers itself on boot (as it will by default if you do nothing else). Upstart has the advantage of allowing easy start/stop of processes, but you get that with docker's start/stop too!
I think it's weird to force the user to manually create a container (with all the correct port/volume bindings) before the upstart script will work.
But that doesn't feel like the right thing for Docker.
I use supervisord for this with great success.
Use what you know, use what works, use something that you can easily maintain and understand.