Restart Policies

Difficulty: Beginner

Question

What restart policies does Docker support, how do they differ, and which would you use for a production service?

Answer

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.

The four policies side by side

PolicyRestarts on crash (non-zero exit)Restarts on clean exit (code 0)After manual docker stopWhen the daemon or host restarts
noNoNoStays stoppedStays stopped
on-failureYes (optionally capped)NoStays stoppedNot something to rely on; use unless-stopped for this
alwaysYesYesStays stopped until daemon restartStarted again, even if you had stopped it
unless-stoppedYesYesStays stoppedStarted again only if it was not manually stopped

Key behaviours:

  • A manual 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.
  • An OOM kill (exit 137) counts as a failure, so on-failure will restart it.

Backoff and the 10-second rule

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.

Choosing a policy

  • Web APIs, workers, databases on a single VM: unless-stopped. They come back after a reboot, but if you deliberately stopped one for maintenance, it stays down.
  • Batch or migration containers: 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.
  • Compose uses the same values (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.

Changing a policy on a running container

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.

What Docker does when a container exits. The policy is checked every time PID 1 exits, with a growing delay between attempts.
  1. PID 1 exits with a code: Docker records the exit code and time.
  2. Was it a manual docker stop?: If yes, no policy restarts it.
  3. Check the policy and exit code: on-failure only acts on non-zero codes.
  4. Check max retries: on-failure:N gives up after N attempts.
  5. Wait, then restart: Delay starts at 100 ms and doubles each time.
  6. Reset after 10 seconds up: A stable run clears the backoff delay.

Code examples

Setting and changing restart policies

# 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.

Restart policy in Compose

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.

Key points

  • Policies: no (default), on-failure[:N], always, unless-stopped.
  • A manual docker stop is never undone by a policy; always vs unless-stopped differs only after a daemon restart.
  • Backoff starts at 100 ms, doubles, and resets after 10 seconds of stable running.
  • Restart policies react to exits only; hung processes need health checks and an orchestrator.
  • docker update --restart changes the policy of an existing container.

Common follow-up questions

  • How is a restart policy different from a HEALTHCHECK?
  • What happens to containers with restart: always after a host reboot?
  • How does Kubernetes CrashLoopBackOff relate to Docker restart backoff?
  • Can you combine --rm with a restart policy?

Interview tips

  • Explain always vs unless-stopped through the daemon restart scenario; that is the real difference.
  • Mention that restart policies do not catch hung processes; it shows you know their limits.
  • Recommend a policy per workload type instead of one policy for everything.

Concepts covered

Restart policy, on-failure, unless-stopped, Exponential backoff, docker update

More Docker & Containers questions