Difficulty: Beginner
What restart policies does Docker support, how do they differ, and which would you use for a production service?
Docker has four restart policies set with --restart: no (the default, never restart), on-failure[:max-retries] (restart only when the process exits with a non-zero code), always (restart whenever it stops, and also when the daemon starts) and unless-stopped (like always, but stays stopped after a daemon restart if you stopped it manually). For a long-running service on a single host, unless-stopped is usually the right choice.
| Policy | Restarts on crash (non-zero exit) | Restarts on clean exit (code 0) | After manual docker stop | When the daemon or host restarts |
|---|---|---|---|---|
no | No | No | Stays stopped | Stays stopped |
on-failure | Yes (optionally capped) | No | Stays stopped | Not something to rely on; use unless-stopped for this |
always | Yes | Yes | Stays stopped until daemon restart | Started again, even if you had stopped it |
unless-stopped | Yes | Yes | Stays stopped | Started again only if it was not manually stopped |
Key behaviours:
docker stop never triggers a restart under any policy. The difference between always and unless-stopped only shows up when the Docker daemon itself restarts, for example after a host reboot or an Engine upgrade.on-failure:5 gives up after five restart attempts. This is useful for batch jobs where repeated failure means a real bug.on-failure will restart it.Docker does not restart in a tight loop. It waits with an increasing delay, starting at 100 ms and doubling each time, which you see as the Restarting (1) 5 seconds ago status. The delay resets once the container stays up for at least 10 seconds. A restart policy also only becomes effective after the container has started successfully, so a container that fails before ever running properly does not loop forever.
unless-stopped. They come back after a reboot, but if you deliberately stopped one for maintenance, it stays down.on-failure:3 or no. A migration that exits 0 must not run again.always: fine for infrastructure you never want down, such as a log shipper, but surprising if someone stopped it on purpose.restart: unless-stopped). In Kubernetes, the pod's restartPolicy (Always, OnFailure, Never) plays this role and the kubelet handles backoff (CrashLoopBackOff).A restart policy is not a health system. It only reacts to the process exiting. A hung process that is still alive will not be restarted; you need a HEALTHCHECK plus an orchestrator (Swarm or Kubernetes) to replace unhealthy containers.
docker update --restart unless-stopped <container> changes it without recreating the container. docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' shows the policy and how many times it has restarted, which is a quick way to spot a crash loop.
# Long-running API that should survive reboots
docker run -d --name orders-api --restart unless-stopped orders:2.3
# Batch job: retry a crash up to 3 times, never rerun on success
docker run -d --name nightly-settlement --restart on-failure:3 settlement:1.0
# Change the policy without recreating the container
docker update --restart always orders-api
# Inspect policy and restart count to spot crash loops
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} restarts={{.RestartCount}}' orders-api
Pick the policy per workload, and use RestartCount to notice a container that keeps dying.
services:
api:
image: orders:2.3
restart: unless-stopped
migrate:
image: orders:2.3
command: ["node", "migrate.js"]
restart: "no" # quote it; bare no is parsed as a YAML boolean
Compose uses the same policy names. Quoting "no" avoids YAML turning it into false.
Restart policy, on-failure, unless-stopped, Exponential backoff, docker update