Docker introduced a powerful new feature called “restart policies”. Restart policies replaces the old daemon “-r” option, which itself would try to restart all previously running containers upon daemon restart, however this was rife with trouble.
- It just flat out didn’t always work, and was basically unreliable in production
- Maybe you didn’t actually need all of your containers restarted
- It did not resolve links, so if a container had links and docker tried to start that container before the linked container was started, it would fail to start.
- Does not apply to container crashes/stopping
With restart policies this all changes.
- When your system boots and starts up docker, docker will reliably restart all containers that have a restart policy applied
- You can specify per-container what the restart policy should be
- Docker will walk link dependencies and start them in the correct order
- Will monitor/restart crashed containers
docker run -d --restart always --name myredis redis docker run -d --restart always --link myredis:redisdb myapp
Other restart policy modes are:
- no – no restart policy
- on-failure – restart if exit code is not 0
Docker will also back-off on restarts if they are too frequent, and when using “on-failure” you can set the max restarts.
On minimal distros such as boot2docker, restart policies can be used in place of an init system… I’d even argue that if you are containerizing everything it should always replace an init system.
Basically, if you aren’t using restart policies you are doing it wrong. If you aren’t using restart=always on your long running processes you are probably also doing it wrong.