Use it to keep services running, or when a container restarts in a loop.
Container Restart Policies
Restart policies decide whether the runtime restarts a container when it exits: Docker no, on-failure, always, unless-stopped; Kubernetes pods use restartPolicy with backoff.
1. docker inspect <c> --format '{{.HostConfig.RestartPolicy}} {{.RestartCount}}'.
2. Exit code of the last run.
3. Logs of the previous run.
4. Fix the crash cause.
5. Set unless-stopped/always for services.
container exits → policy decides → restart with backoff
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Reboot the host (or restart the daemon) in a test and confirm the container comes back. For loops, read the exit code: 137 usually means killed (often OOM), 1 an application error.
Step 2 Common failures Narrow the fault
Policy always also restarts containers you stopped once the daemon restarts. A container that exits 0 is not restarted by on-failure. Docker restart policies do not replace a process supervisor for health; a hung process is not restarted.
Restarts hide crashes — monitor restart counts and logs. Kubernetes restarts with exponential backoff (CrashLoopBackOff).
docker ps -a— containers and exit codesdocker logs <c>— container outputdocker inspect <c>— config, mounts, network, state
ReferenceWhat it is1 section · depth on demand
How it works
The daemon tracks each container's policy and restarts it with increasing delay after repeated failures. Restart policies act only on containers managed by the daemon; Compose sets them from restart: in compose.yaml.
ReferenceImportant terms & boundaries3 sections · depth on demand
Policies
| Policy | Restarts when |
|---|---|
| no | Never |
| on-failure[:N] | Exit code ≠ 0 (max N times) |
| always | Always, also after daemon restart |
| unless-stopped | Always, except if stopped manually |
docker update --restart unless-stopped web
docker inspect -f '{{.RestartCount}} {{.State.ExitCode}}' web
Documented example, not captured output. In Kubernetes, Pod restartPolicy and the kubelet's back-off handle restarts.
In depth
Restart policies are handled by the Docker daemon. After repeated failures the daemon waits longer between restarts (starting at 100 ms and doubling), and a container must run for at least 10 seconds before the restart counts as successful and the delay resets.
Choosing a policy
| Workload | Policy | Reason |
|---|---|---|
| Long-running service | unless-stopped | Comes back after reboot, but respects a manual stop |
| Batch job that may fail | on-failure:5 | Retries errors, gives up after 5 attempts |
| One-off task | no | A finished task should stay finished |
| Must always run, even after manual stop + reboot | always | Rarely what you want |
Exit codes tell you why it restarted
docker inspect -f '{{.RestartCount}} {{.State.ExitCode}} {{.State.OOMKilled}} {{.State.Error}}' api
# 14 137 true → killed by the memory limit, restarted 14 times
| Exit code | Meaning |
|---|---|
| 0 | Clean exit — not restarted by on-failure |
| 1, 2 … | Application error |
| 125 / 126 / 127 | Docker could not run / not executable / command not found |
| 137 | SIGKILL (OOM or docker kill) |
| 143 | SIGTERM (normal stop) |
Health checks do not restart containers
A failing HEALTHCHECK marks the container unhealthy, but plain Docker does not restart it. Swarm services and Kubernetes liveness probes do; otherwise an external watchdog is needed.
Common mistakes
restart: alwayshiding crash loops.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.