Sub topic 2
What a restart policy does
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 also assumes you know that a container stops when its main process exits.
The problem restart policies solve
A container runs one main process. When that process exits, the container stops. The cause might be a crash, an out-of-memory kill, a bug, or a normal finish. Without help, the container stays stopped until you start it again.
A restart policy is a setting on a container that tells the Docker daemon what to do after the container stops. The daemon is the background service (dockerd) that manages containers. The daemon checks the policy each time the main process exits, and it also checks it when the daemon itself starts.
What the policy looks at
The daemon decides using two facts:
- The exit code of the main process.
- Whether you stopped the container on purpose, for example with
docker stop.
It then applies the policy. If the policy says to restart, the daemon starts the same container again. It does not create a new one. The container keeps its ID, name, filesystem changes, and configuration.
The decision flow is shown below.
The four policies in brief
| Policy | Restarts after a non-zero exit | Restarts after exit code 0 |
|---|---|---|
| `no` | No | No |
| `on-failure[:max]` | Yes, up to the optional maximum | No |
| `always` | Yes | Yes |
| `unless-stopped` | Yes | Yes, unless you stopped it |
Later chapters cover each policy in detail. Here you only need the shape of the idea: the policy is a rule attached to the container, and the daemon follows it.
Seeing a policy work
Start a container with a policy and then make it fail:
docker run -d --name web --restart unless-stopped nginx:alpine
docker kill --signal=KILL web
docker ps --filter name=webThe docker kill command ends the main process with a non-zero exit. A moment later, docker ps shows web running again, with a status such as Up 2 seconds. The container was not recreated. The daemon restarted it.
You can check how many times this has happened:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' webThe output is the policy name, the restart count, and the current status, for example unless-stopped 1 running.
Common errors
- Expecting a restart after
docker stop. A container you stopped yourself is not restarted byon-failure, and not byunless-stoppedeither. Chapter material on manual stops coversalwaysand daemon restarts. - Treating a policy as a fix. A policy hides a crash by restarting it. The bug remains, and the container may loop.
- Assuming a policy applies to containers that never started. If
docker runfails before the process starts, for example because of a bad image name, there is nothing to restart.
Exercise
A container with --restart on-failure runs a script that finishes its work and exits with code 0. Does Docker restart it? What if the script exits with code 2?
Show answer
With exit code 0, Docker does not restart it, because on-failure only acts on non-zero exit codes. With exit code 2, Docker restarts it, up to the maximum retry count if one was set.