Sub topic 13
Restart policies
A restart policy tells the Docker daemon what to do when a container's main process exits or when the daemon itself restarts. This chapter assumes you can start containers with docker run, read their state with docker ps, and stop and remove them. It also assumes you know what a process exit code is.
Why restart policies exist
A container stops when its main process (PID 1 inside the container) ends. Without a policy, it stays stopped. That is fine for a one-off job. It is a problem for a web server that crashed at 3 a.m. or a host that rebooted after a kernel update.
You set the policy with the --restart flag on docker run.
The four policies
| Policy | Restarts after a crash (non-zero exit) | Restarts after a clean exit (code 0) | Restarts after daemon or host restart |
|---|---|---|---|
| `no` (default) | No | No | No |
| `on-failure[:max]` | Yes, up to `max` times if given | No | Only if it was running and had failed before |
| `always` | Yes | Yes | Yes, even if you stopped it manually |
| `unless-stopped` | Yes | Yes | Yes, unless you stopped it manually |
The difference between always and unless-stopped matters most. With always, a container you stopped with docker stop starts again when the daemon restarts. With unless-stopped, it stays stopped. For long-running services, unless-stopped is usually the better choice, because your manual stop is respected.
Trying it
Start the container from the chapter's example. It uses unless-stopped along with resource limits.
docker run -d --name limited --memory 256m --cpus 0.5 --restart unless-stopped nginx:1.27Check the policy that Docker stored:
docker inspect --format '{{.HostConfig.RestartPolicy.Name}}' limitedThe output is unless-stopped.
To see a restart happen, run a container that fails on purpose:
docker run -d --name flaky --restart on-failure:3 alpine sh -c "echo starting; exit 1"Wait a few seconds, then check how many times Docker restarted it:
docker inspect --format '{{.RestartCount}} {{.State.Status}}' flakyAfter the third retry, Docker gives up. The count reads 3 and the status is exited.
How the restart loop behaves
Docker does not restart a container instantly each time. It waits before each attempt. The delay starts at 100 milliseconds and doubles after every restart, up to a maximum of one minute. If the container runs for at least 10 seconds, the delay resets. This backoff stops a broken container from using the CPU in a tight loop.
Common errors
- A restart policy hides a crash. A container that loops through restarts shows as
Up 5 secondsorRestarting. Always readdocker logsand checkRestartCountwhen a service seems unstable. - Using
alwaysand being surprised. A container you stopped comes back after a daemon restart. Useunless-stoppedto avoid this. - Expecting
on-failureto restart after exit code 0. It does not. A clean exit counts as success. - Changing the policy by recreating the container. You do not need to.
docker updatechanges the policy on an existing container.
docker update --restart no limited- Treating a restart policy as a health check. A policy reacts only when the process exits. A container that is running but hung is not restarted.
Cleaning up
Containers with a restart policy come back by themselves, so stop them before removing them, or remove them with force.
docker rm -f flaky limitedExercise
You run a database with --restart always, then stop it with docker stop for maintenance. The host reboots. What happens to the container, and which policy would have kept it stopped?
Show answer
With always, Docker starts the container again after the reboot, even though you stopped it manually. The unless-stopped policy would have kept it stopped.
Exercise
A container started with --restart on-failure:3 exits with code 0. How many times does Docker restart it?
Show answer
Zero times. The on-failure policy restarts only after a non-zero exit code, and code 0 means the process finished successfully.