Restart policies and health checks
How Docker restarts containers that exit, the back-off it uses, what a HEALTHCHECK reports, and why an unhealthy container is not restarted on its own.
docker run and the container lifecycleInspecting containers: inspect, top, stats and events
Restart policies and health checks
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A restart policy decides what happens when PID 1 exits: no (the default), on-failure[:N], always, or unless-stopped.
- always and unless-stopped differ only after a manual stop. An always container comes back when the daemon restarts, and an unless-stopped one stays stopped.
- Restarts back off: 100 ms, doubling up to one minute, reset once the container has run for 10 seconds.
- A HEALTHCHECK runs a command inside the container on an interval. Exit 0 is healthy, and retries consecutive failures make it unhealthy.
- Standalone Docker only records health and emits an event. It does not restart an unhealthy container. Compose uses health for start order, and Swarm replaces unhealthy tasks.
- A good health check tests the app cheaply from inside, with a tool the image actually contains, and doesn't fail because a dependency is down.
Up, but not working
Two kinds of failure take a service down. In one, the process crashes: a panic, an unhandled exception, an OOM kill. In the other, the process is alive and useless: deadlocked, stuck on a broken database connection, answering every request with a 500. docker ps says Up for the second kind, which is the worst part.
Docker has a tool for each. A restart policy handles the first: when the process exits, start it again. A health check handles the second: run a probe and report whether the app actually works. They sound like a pair. Understanding where they don’t connect is most of this lesson.
Restart policies
Set with --restart on docker run, or restart: in Compose:
| Policy | Restarts when PID 1 exits… | After a daemon restart |
|---|---|---|
no (default) |
never | stays stopped |
on-failure[:N] |
with a non-zero code, up to N times if given | not restarted |
always |
for any reason | started, even if you’d stopped it manually |
unless-stopped |
for any reason | started, unless you’d stopped it manually |
A manual docker stop always wins for the moment: the policy ignores the exit you caused. The difference between always and unless-stopped appears at the next daemon restart or reboot. always brings your stopped container back, unless-stopped remembers. For long-running services, unless-stopped is usually what people mean. Change a policy without recreating the container using docker update --restart unless-stopped api.
The back-off
A container that crashes instantly would spin the CPU if restarted instantly. moby’s restartmanager.go sets the delay: it starts at 100 ms, doubles after each restart, caps at one minute, and resets to 100 ms once the container has run for 10 seconds. Simulating that rule for a container that dies at once:
restart: 1 2 3 4 5 6 7 8 9 10 11 12
delay s: 0.1 0.2 0.4 0.8 1.6 3.2 6.4 12.8 25.6 51.2 60 60
The first ten restarts take about 102 seconds in total, after which it’s one attempt a minute, indefinitely, unless on-failure:N sets a limit. That’s the Restarting (1) 40 seconds ago you see in docker ps for a crash-looping container. docker inspect -f '{{.RestartCount}}' api counts them.
Docker’s docs say a policy “only takes effect after a container starts successfully”, meaning it has been up for 10 seconds. In the code, the 10 seconds only resets the back-off: a container that dies at once is still restarted, just with growing delays.
Health checks
A HEALTHCHECK in the Dockerfile, or --health-* flags on docker run, tells the daemon to run a command inside the container on a schedule:
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --start-interval=2s --retries=3 \
CMD ["/app/healthcheck", "http://localhost:8080/health"]
Exit 0 means healthy and exit 1 means unhealthy. The defaults, from the Dockerfile reference, are interval 30 s, timeout 30 s, start period 0 s, start interval 5 s and 3 retries. The status starts as starting. Any pass makes it healthy, and retries consecutive failures make it unhealthy. During the start period, failures don’t count, and probes run every start-interval (Engine 25 and later) so a slow-booting app isn’t marked unhealthy too early. A check that exceeds the timeout is killed with SIGKILL and counts as a failure.
docker ps shows (healthy) in the status column. docker inspect -f '{{json .State.Health}}' api shows the record, which per moby’s health.go keeps the last 5 results and up to 4,096 bytes of each probe’s output.
Unhealthy doesn’t mean restarted
Here’s the gap. In health.go, when the failing streak reaches retries, the daemon sets the status to unhealthy and logs a health_status: unhealthy event. That’s all. Restart policies fire only when PID 1 exits. A deadlocked app with restart: always will sit unhealthy indefinitely.
What acts on health:
- Docker Compose uses it for start-up order:
depends_on: { db: { condition: service_healthy } }. See startup order. - Swarm services replace unhealthy tasks.
- Kubernetes ignores
HEALTHCHECKand uses its own liveness and readiness probes. - On plain Docker, a watcher that listens for
health_statusevents and restarts the container, such as theautohealsidecar the Debug Driven video in deeper sets up. Or make the app exit when it detects it’s wedged, so the restart policy can do its job.
Writing a good check
- Use what’s in the image.
curlisn’t in slim or distroless images, and a shell-form check needs/bin/sh. Compile a tiny health client into the app or copy one in, and use exec form. - Test this container, not its dependencies. If the check fails when the database is down, every replica goes unhealthy at once, and anything that restarts on health makes the outage worse.
- Keep it cheap and quick. It runs every interval, forever, with the container’s resources.
- Match the app’s real readiness. Port open isn’t the same as ready to serve. The backend lesson on health checks covers liveness against readiness.
About the video
The embed is all of DheerajTechInsight’s “Docker Healthchecks & Restart Policies Explained | Make Containers Production-Ready”, January 2026, 17 minutes. Its chapters cover why running isn’t the same as working, restart policies, restart policies in Compose, health checks for apps, health checks in Compose, combining health checks with restart policies, and production practices. Listen carefully to the “combining” chapter: on standalone Docker the two don’t trigger each other, as health.go shows. The video doesn’t show the back-off timing either. English was judged from its “English (US) original” audio track. Debug Driven’s short video in deeper is about exactly that gap.
What was run: the back-off table came from a three-line Python simulation of the rule in moby’s restartmanager.go, and the health behaviour was read from moby’s health.go. Nothing ran in Docker. Policy semantics and HEALTHCHECK defaults are from docs.docker.com and the Dockerfile reference, checked 2026-11-28.
Where this fits
Restarts and stopped containers leave things behind. Next: cleaning up, prune and disk usage.
Check yourself
You set --restart always, run docker stop, then reboot the host. Is the container running after boot? What if it was unless-stopped?
With always, yes. A manual stop is honoured only until the daemon restarts. With unless-stopped, no, because it remembers you stopped it.
A container with on-failure exits with code 0 after finishing its job. Does Docker restart it?
No. on-failure restarts only on a non-zero exit code. Exit 0 is success.
A container shows (unhealthy) in docker ps for an hour and has restart: always. Why hasn't Docker restarted it?
Restart policies trigger only when the process exits. Health status is separate: the daemon records unhealthy and emits a health_status event but takes no action. You need Swarm, an orchestrator, or a watcher such as autoheal to act on it.
Your image is distroless and HEALTHCHECK CMD curl -f http://localhost:8080/health always fails. Why?
There's no curl, or shell, in the image, so the check command can't run and each attempt counts as a failure. Use a small health binary built into the app or copied in, in exec form.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.