Sub topic 4
on-failure and limiting retries
This chapter assumes you can run containers with docker run, list them with docker ps, and read exit codes (0 means success, non-zero means failure). It covers the on-failure restart policy and how to cap its retries.
What on-failure does
The on-failure policy restarts a container only when its main process exits with a non-zero exit code. If the process exits with code 0, Docker leaves the container stopped.
That makes it a good fit for jobs that should finish once but may fail along the way, such as a batch import or a worker that crashes on a bad connection.
| Exit code | Meaning | Restarted by on-failure? |
|---|---|---|
| 0 | Success | No |
| 1 | General error | Yes |
| 137 | Killed (for example out of memory) | Yes |
Limiting retries
A bare on-failure retries forever. To cap the retries, add a colon and a number:
docker run -d --name flaky --restart on-failure:3 alpine sh -c 'exit 1'Here on-failure:3 means Docker restarts the container at most three times. The container always fails, so Docker starts it once, then retries three times. After the third retry fails, Docker gives up and the container stays in the exited state.
The cap counts restarts, not total starts. With on-failure:3, the container runs up to four times in total.
Following the retries
Use docker inspect to see the policy, the number of restarts Docker has performed, and the current status:
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flakyRun it a few times in the first seconds after starting the container. The output moves through values like these:
on-failure 1 restarting
on-failure 2 running
on-failure 3 exitedWhen the count reaches 3 and the status is exited, the limit has been hit. Docker will not restart the container again by itself.
Common errors
- Expecting a restart after a clean exit. A process that exits with code 0 is never restarted by
on-failure. If you need that, you wantalwaysorunless-stopped, which later chapters cover. - Forgetting the cap. Without
:N, a container with a permanent bug retries indefinitely. Docker waits longer between attempts as failures pile up, but it never stops. - Misreading the count.
RestartCountis the number of restarts, not the number of runs. A count of 3 means four runs. - Assuming a daemon restart resets the limit. Treat the count as a guard for one failing streak, not as a lifetime budget. Check
RestartCountwithdocker inspectrather than guessing.
Compose equivalent
In a Compose file, the same cap goes in the service-level restart key:
services:
worker:
image: alpine
command: ["sh", "-c", "exit 1"]
restart: on-failure:3Exercise
You start a container with --restart on-failure:2 and its process always exits with code 1. How many times does the process run in total, and what is RestartCount at the end? What changes if the process exits with code 0 on its first run?
Show answer
The process runs three times: the first start plus two restarts. RestartCount ends at 2 and the container stays exited. If the first run exits with code 0, Docker does not restart it at all, so the process runs once and RestartCount stays 0.
Exercise
Which command shows the policy, the restart count and the status of a container named flaky on one line?
Show answer
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}} {{.State.Status}}' flaky