Docker Restart Policy
A Docker restart policy tells the daemon whether to restart a container after it exits. The four policies are no, on-failure, always, and unless-stopped.
The four policies
| Policy | Restarts when | After manual docker stop |
|---|---|---|
no |
Never | Stays stopped |
on-failure[:max] |
Exit code is non-zero | Stays stopped |
always |
Any exit | Stays stopped until daemon restarts, then starts again |
unless-stopped |
Any exit | Stays stopped, even after daemon restart |
on-failure:5 gives up after five consecutive failures, which prevents endless restart loops. A container must run for a few seconds before Docker treats the start as successful, and restarts use an increasing delay.
Usage
docker run -d --name payment-api --restart unless-stopped registry.example.com/payment-api:v3.1.0docker run -d --name worker --restart on-failure:5 worker:latestdocker run --rm --restart no migrate:latestdocker update --restart unless-stopped payment-apiservices: api: restart: unless-stopped migration: restart: "no"Check restarts
docker inspect payment-api --format '{{.RestartCount}} {{.HostConfig.RestartPolicy.Name}}'A rising restart count means a crash loop. Fix the cause rather than relying on the policy.
Scope
A restart policy only handles process exits on the same host. It does not move workloads when a node fails. Kubernetes and Swarm reschedule across nodes.
Common MistakeUsing
alwayson a container that crashes at startup. It loops, filling logs and burning CPU.
Tip
unless-stoppedis a good default for long-running services, paired with a health check and monitoring of restart count.
Frequently Asked Questions
What is the practical difference between always and unless-stopped?
Both restart after crashes. After you manually stop a container, always will start it again when the Docker daemon restarts, for example after a reboot, while unless-stopped keeps it stopped. That matters during maintenance windows.
Does on-failure restart an OOM-killed container?
Yes, if it exits with a non-zero code, which an OOM kill does (137). It stops retrying only after the max-retry count if you set one.
Does a restart policy replace monitoring?
No. It hides crash loops unless you watch restart count and logs, and it cannot recover from host failure.