Sub topic 13
What restart policies do not do: unhealthy containers
This chapter assumes you can run containers with docker run, read docker ps, and know that a container stops when its main process exits. It also assumes you know the policies no, on-failure, always and unless-stopped.
The trigger is process exit
A restart policy reacts to one event: the container's main process exits. It does not look at whether the application inside is working. A web server can hang, deadlock, or return errors on every request while its process stays alive. Docker sees a running process, so no policy restarts it.
A health check fills part of that gap. It is a command Docker runs inside the container at set intervals. Docker records the result as the container's health status: starting, healthy, or unhealthy.
A health check does not trigger a restart
An unhealthy status is information only. With plain docker run or Compose, Docker does not restart, stop, or kill a container because it became unhealthy. The status appears in docker ps and docker inspect, and other tools can act on it.
docker run -d --name web --restart unless-stopped \
--health-cmd 'wget -qO- http://localhost/ || exit 1' \
--health-interval 10s \
--health-retries 3 \
nginx:alpineHere, if nginx stops answering but the process keeps running, docker ps shows (unhealthy) and the container is not restarted. The unless-stopped policy only applies if the process exits.
Reading the health status
Use docker inspect to read the status next to the restart data.
docker inspect -f '{{.State.Health.Status}} {{.RestartCount}}' webThis prints something like unhealthy 0. A health status with a restart count of 0 confirms the container is failing its check but has never been restarted. A container with no health check defined has no .State.Health field, so this template fails on it.
Other limits to remember
- A restart policy does not fix the cause of a failure. If the container exits for a bad reason, such as a missing config file, it will keep failing.
- Policies apply to one Docker host. If the host goes down, nothing moves the container elsewhere.
- Policies do not check dependencies. A container can restart before its database is ready, and then exit again.
- A restart does not preserve in-memory state. The process starts fresh, though the container's writable layer and volumes remain.
Ways to act on an unhealthy container
You have a few options, each with a trade-off.
| Approach | How it works | Trade-off |
|---|---|---|
| Make the process exit | Have the app or a wrapper script exit when it detects it is broken | Simple, but you must build the detection into the app |
| External automation | A script or monitor watches health events and runs `docker restart` | Flexible, but you must run and maintain it |
| Orchestrator | Swarm replaces tasks whose health check fails | Needs Swarm, covered in the next chapters |
For the external option, Docker emits events you can filter on.
docker events --filter event=health_statusEach unhealthy report arrives as an event, which a script can use to restart the container.
Exercise
A container runs with --restart always and a health check. The app deadlocks, but its process keeps running, and docker ps shows (unhealthy). Will Docker restart it? Why, and how could you get it restarted?
Show answer
No. A restart policy triggers only when the main process exits, and a failing health check only changes the status to unhealthy. To get a restart, make the process exit when the app is broken, run a script that listens to docker events --filter event=health_status and calls docker restart, or use an orchestrator such as Swarm that replaces unhealthy tasks.