Sub topic 5
always: restart whatever the exit code
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes (0 means success, non-zero means failure). It covers the always restart policy: what it restarts, what it ignores, and how it behaves around manual stops and daemon restarts.
What always does
With --restart always, Docker restarts the container every time its main process exits. The exit code does not matter. A clean exit with code 0 and a crash with code 137 both lead to a restart.
This differs from on-failure, which restarts only on a non-zero exit code. It also differs from the default no, which never restarts.
docker run -d --name ticker --restart always alpine sh -c 'echo tick; sleep 5; exit 0'The process exits with code 0 after five seconds. Docker starts it again anyway. Watch it with docker ps: the status shows Up for a few seconds, then Restarting, then Up again.
Restart flow
Manual stops and daemon restarts
The always policy has a detail that surprises many people. If you stop the container with docker stop, Docker leaves it stopped. It does not immediately restart it. But when the Docker daemon restarts (for example after a host reboot), Docker starts the container again, even though you stopped it by hand.
You can see this in practice:
- Run
docker run -d --name web --restart always nginx:alpine. - Run
docker stop web. The container stays stopped. - Restart the Docker daemon, for example with
sudo systemctl restart docker. - Run
docker ps. Thewebcontainer is running again.
That is the one behavioral difference from unless-stopped, which the next chapter covers. With unless-stopped, the manual stop is remembered across daemon restarts.
Setting and checking the policy
You can set the policy when you create the container, as shown above. To change a running container, use docker update:
docker update --restart always webTo confirm the policy and see how many restarts Docker has performed, use docker inspect:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' tickerThe output looks like always 3 running. The middle number rises each time Docker restarts the container.
In a Compose file, set the restart key on the service:
services:
web:
image: nginx:alpine
restart: alwaysCommon mistakes
- Using always for a job that should finish. A batch task that exits with code 0 is a success, but
alwaysruns it again forever. Usenooron-failurefor jobs. - Expecting a manual stop to stick. After a daemon restart, an
alwayscontainer comes back. If that is not what you want, useunless-stopped. - Hiding a crash loop. A container that exits at once will restart again and again. Check
RestartCountanddocker logswhen a service seems unstable. - Expecting it to fix unhealthy containers. The policy reacts to process exit only. A running container that fails its health check is not restarted.
Exercise
You run docker run -d --name job --restart always alpine sh -c 'exit 0'. What does Docker do, and why? Then, after you run docker stop job on a similar container and reboot the host, what state is it in?
Show answer
Docker keeps restarting job. The always policy ignores the exit code, so a successful exit with code 0 still triggers a restart, with a growing delay between attempts. For the stopped container, docker stop leaves it stopped, but when the Docker daemon starts after the reboot, an always container is started again, so it is running.