Sub topic 8
Restart policies and crash loops
This chapter assumes you can read docker ps output and docker inspect state, and that you know how exit codes work. A restart policy tells the Docker daemon what to do when a container's main process exits. A crash loop appears when a policy keeps restarting a container that fails every time.
The four policies
You set a policy with --restart on docker run, or restart: in a Compose file.
| Policy | Restarts when the process exits | Notes |
|---|---|---|
| `no` | Never | Default. |
| `on-failure[:N]` | Only with a non-zero exit code | The optional `N` caps the number of retries. |
| `always` | Any exit, including code 0 | Also starts the container when the daemon starts. |
| `unless-stopped` | Any exit | Like `always`, but a container you stopped by hand stays stopped after a daemon restart. |
docker run -d --name web --restart on-failure:5 myimage:latestalways restarts a container that exits cleanly with code 0. A one-shot job with that policy will run forever, so use on-failure for jobs.
Recognizing a crash loop
A looping container never stays in Up for long. In docker ps, the status column alternates between these values:
Restarting (1) 12 seconds agoUp 3 seconds
Docker waits between restarts. The delay starts at 100 ms and doubles after each failure, up to a maximum of one minute. It resets once the container has run for 10 seconds. A loop therefore gets slower and slower, so the logs of a failing container can look quiet.
stateDiagram-v2Diagnosing the loop
Read the restart count and the last exit information together:
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' webThen read the output of the failed attempts. Logs from earlier runs are kept, so they are still there between restarts:
docker logs --timestamps --tail 100 webMatch what you find to a cause:
- Exit 1 with an application error in the logs. The app is failing on startup, often because of a bad configuration value or an unreachable database.
- Exit 126 or 127. The entrypoint is not executable or does not exist. Restarting cannot fix this.
- Exit 137 with
oom=true. The kernel killed the process for using too much memory. - Exit 0 under
always. The process finished normally. The policy is wrong for this workload.
Breaking the loop to debug
Do not debug a container that restarts every few seconds. Turn the policy off first, without recreating the container:
docker update --restart=no web
docker stop webThen reproduce the failure with an overridden entrypoint, as covered in the earlier chapter on failed starts.
Restarts hide problems
A policy keeps a service available, but it can also hide the cause. A service that restarts three times a day looks healthy in monitoring. Check RestartCount on containers that matter, even when they show Up.
Exercise
A container with --restart always shows Restarting (0) in docker ps, and its logs end with "job complete". What is wrong, and what should you change?
Show answer
The process exits successfully with code 0, and always restarts it regardless of the exit code. It is a one-shot job, not a service. Use --restart on-failure (or no) so it is restarted only when it fails.