Skip to content

Docker restart policy — no, on-failure, always, unless-stopped

How each Docker restart policy behaves on exit codes, backoff, and daemon restart — plus the common myth that a restart policy reacts to healthcheck status.

A Docker restart policy tells the daemon what to do when a container’s main process exits: leave it stopped (no), bring it back only after a non-zero exit (on-failure[:max-retries]), always bring it back (always), or bring it back except when you deliberately stopped it (unless-stopped). The policy reacts to the process exiting and to the daemon starting — nothing else. It does not read your healthcheck. A container marked unhealthy keeps running under any policy; only the process actually exiting triggers a restart.

What you’ll accomplish

  • Pick the right policy for a service, a batch job, or a dev container
  • Set a policy at docker run time and change it live with docker update
  • Read the policy and restart count back out with docker inspect
  • Configure restarts in Compose with both restart: and deploy.restart_policy
  • Avoid the three traps that make restart policies hide real bugs

Prerequisites

  • Docker Engine, latest stable (27.x or 28.x). Check with docker version.
  • A throwaway container to experiment on. The examples use alpine and a tiny script.
  • Shell access to the host running the daemon (restart policies are enforced by dockerd, not the CLI).

How it works

Docker supports exactly four restart policies. You set one per container.

  • no (the default) — Docker never restarts the container. When the process exits, it stays stopped.
  • on-failure[:max-retries] — restart only when the process exits with a non-zero exit code. An optional :N caps the number of consecutive attempts, after which Docker gives up. A clean exit (code 0) is left stopped.
  • always — restart the container whenever it stops, regardless of exit code. If you docker stop it manually, it stays stopped until the daemon next starts, at which point it is started again.
  • unless-stopped — same as always, except that if the container was in a stopped state (because you stopped it) when the daemon shuts down, it is not started back up when the daemon restarts.

Exit-code semantics. Only on-failure looks at the exit code. always and unless-stopped restart on any exit, clean or not. no restarts on nothing. So a job that finishes successfully (exit 0) will loop forever under always but run once under on-failure.

Backoff. Docker does not hammer a crashing container. It uses an exponential back-off starting at 100ms and doubling each attempt (100ms, 200ms, 400ms, …) so a tight crash loop does not saturate the host. The delay resets once the container has stayed up for at least 10 seconds.

Daemon restart. When dockerd itself starts (host reboot, package upgrade, systemctl restart docker), it re-applies policies. always starts every container that has it. unless-stopped starts them too — unless they were stopped by you before the daemon went down. This one difference is the entire reason to prefer unless-stopped for most long-running services.

The retry counter. For on-failure:N, Docker counts consecutive failed restart attempts. Once the container stays up long enough to be considered running, the counter resets, so a service that fails once an hour will not eventually exhaust its budget. RestartCount in docker inspect, by contrast, is a lifetime tally of every restart and never resets while the container exists — do not confuse the two when reasoning about whether Docker has given up.

Healthcheck is separate. A restart policy and a HEALTHCHECK are two independent mechanisms. The healthcheck sets State.Health to healthy/unhealthy; the restart policy watches process exits. They never talk to each other. Making an unhealthy container restart requires an external actor — Compose depends_on, Swarm, Kubernetes, or your own tooling.

Decision tree for choosing a Docker restart policy

Restart policy and healthcheck are independent mechanisms

Step-by-step

Set a policy when you run the container. Use --restart on docker run:

docker run -d --name web --restart unless-stopped nginx:alpine
b2f4c9a1e7d3...   # container id printed; container is Up

Cap the attempts for a flaky job. on-failure:3 retries a crashing process at most three times, then stops trying:

docker run -d --name importer --restart on-failure:3 alpine \
  sh -c 'echo attempt; exit 1'
# docker ps -a shows the container Exited after the retries are exhausted
importer   Exited (1)   ...

Inspect the policy and the restart count. docker inspect exposes both under HostConfig and RestartCount:

docker inspect importer \
  --format 'policy={{.HostConfig.RestartPolicy.Name}} max={{.HostConfig.RestartPolicy.MaximumRetryCount}} count={{.RestartCount}}'
policy=on-failure max=3 count=3

Change the policy on a running container. docker update --restart rewrites the policy live — no recreate, no downtime:

docker update --restart=always web
web   # the container name is echoed back on success

Re-inspect to confirm the change took:

docker inspect web --format '{{.HostConfig.RestartPolicy.Name}}'
always

Configure it in Compose. For a single-host Compose project, use the top-level restart: key. It maps directly to the docker run --restart values:

services:
  web:
    image: nginx:alpine
    restart: unless-stopped
  importer:
    image: alpine
    command: sh -c "run-import.sh"
    restart: on-failure

To cap attempts in Compose, quote the value so YAML treats it as a string rather than parsing the colon oddly: restart: "on-failure:5". The unquoted restart: on-failure form uses Docker’s default of unlimited retries.

For a Swarm stack (docker stack deploy), the restart: key is ignored — use deploy.restart_policy instead, which the Swarm scheduler enforces:

services:
  web:
    image: nginx:alpine
    deploy:
      restart_policy:
        condition: on-failure
        max_attempts: 3
        delay: 5s

Validate it works

Read the policy and count back out. These two formats are the fastest way to confirm what Docker actually stored:

docker inspect web --format 'RestartPolicy={{json .HostConfig.RestartPolicy}}'
docker inspect web --format 'RestartCount={{.RestartCount}}'
RestartPolicy={"Name":"unless-stopped","MaximumRetryCount":0}
RestartCount=0

Kill the process and watch it come back. Send SIGKILL to the container’s main process and confirm the daemon restarts it:

docker kill --signal=KILL web
sleep 2
docker inspect web --format 'Running={{.State.Running}} count={{.RestartCount}}'
Running=true count=1

Running=true with an incremented RestartCount means the policy fired. If Running=false, the policy is no or on-failure with exhausted retries.

Troubleshooting

A container restart-loops and hides a crash. Under always/unless-stopped, a container that crashes on boot will restart forever, and docker ps may briefly show it Up between attempts. Look at RestartCount climbing and the logs of the last exit:

docker inspect web --format 'count={{.RestartCount}} exit={{.State.ExitCode}}'
docker logs --tail 50 web

A high, ever-climbing count is the signal that the policy is masking a real failure. Switch to on-failure:5 while you debug so Docker eventually gives up and leaves the evidence.

The policy does not stop docker stop from working. docker stop and docker kill are explicit user actions. Under unless-stopped the container stays down after you stop it; under always it also stays down until the daemon restarts, then comes back. If a container you stopped keeps reappearing after a reboot, its policy is always, not unless-stopped.

unless-stopped vs always after a daemon restart. This is the difference that surprises people: both restart a running container when the daemon comes back, but only always also restarts a container you had deliberately stopped. If you want “stay stopped when I stop it, come back on reboot otherwise,” use unless-stopped.

Conflict with --rm. You cannot combine --restart with --rm. --rm removes the container as soon as it exits, so there is nothing left to restart. Docker rejects the combination:

docker run --rm --restart always alpine true
docker: conflicting options: --restart and --rm cannot be combined.

Healthcheck doesn’t trigger a restart. This is the most common misconception. A failing HEALTHCHECK flips the container to unhealthy but never restarts it — the restart policy only reacts to the process exiting. If you need an unhealthy container recycled, you need an orchestrator or an external watcher; on a plain daemon, unhealthy just sits there. Confirm with:

docker inspect web --format 'health={{.State.Health.Status}} running={{.State.Running}}'
health=unhealthy running=true

When this is the wrong approach

Restart policies are a single-host, daemon-level safety net. They are the wrong tool when:

  • An orchestrator already owns restarts. On Kubernetes or Swarm, the scheduler decides when to recreate a workload based on health and desired state. Setting --restart always underneath it fights the orchestrator. Use restartPolicy in the Pod spec or deploy.restart_policy in the stack instead, and leave the container policy at no.
  • A crash loop is masking a bug. Auto-restart is not a fix for a container that crashes because of a missing env var, a bad migration, or an unreachable dependency. Restarting harder just burns CPU and buries the error. Fix the root cause; use on-failure:N so failures stay visible.
  • You need restart-on-unhealthy. Restart policies cannot do this. Reach for an orchestrator, or a purpose-built watcher, rather than trying to bend the policy to react to health.

For deeper container mechanics, see Docker health checks and Docker logging drivers. The authoritative reference is Docker’s own Start containers automatically and the Compose file services reference.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.