Sub topic 12
Choosing a policy for your workload
This chapter assumes you can run containers with docker run, read exit codes, and know that the four policies are no, on-failure, always and unless-stopped. It also assumes you know how docker stop and a daemon restart interact with them. Here you will turn that knowledge into a decision.
Start from the workload, not the policy
Ask three questions about the container:
- Should it run all the time, or run once and finish?
- If it exits with a non-zero code, is retrying likely to help?
- After you stop it on purpose, should it stay stopped, even across a host reboot?
The answers map onto the policies like this.
| Workload | Policy | Reason |
|---|---|---|
| One-off job, migration, or debugging container | `no` | Success and failure should both be visible, and nothing should rerun on its own. |
| Batch worker that can hit transient errors | `on-failure:3` | It retries failures, stops after a limit, and is not restarted after a clean exit. |
| Long-running service you manage by hand | `unless-stopped` | It survives crashes and reboots, and respects your manual stop. |
| Infrastructure that must always be up | `always` | It comes back after any exit, and after a daemon restart even if you stopped it. |
Services: prefer unless-stopped
For a web server, API, or database on a single host, unless-stopped is usually the right choice. It restarts the container after a crash and after the host reboots. If you run docker stop for maintenance, the container stays down, even if the daemon restarts later.
docker run -d --name web --restart unless-stopped nginx:alpinealways behaves the same until you stop the container by hand. After a daemon restart, an always container starts again, which can surprise you if you stopped it for maintenance.
Jobs: bound the retries
A job that fails because of a bug will fail again on every retry. An unlimited restart policy hides that bug behind a loop. Use on-failure with a maximum count so Docker gives up and leaves the container in the exited state for you to examine.
docker run -d --name flaky --restart on-failure:3 alpine sh -c 'exit 1'Because on-failure ignores exit code 0, a job that finishes successfully stays finished.
A decision flow
Common mistakes
- Using
alwayson everything. A broken container then loops forever, and a container you stopped on purpose returns after the next daemon restart. - Using
on-failurewithout a limit. Without a count, Docker keeps retrying a permanent failure. - Expecting a restart on unhealthy. A policy reacts to the process exiting. A container that is running but marked unhealthy is not restarted.
- Setting the policy only at creation. You can change it later without recreating the container.
docker update --restart unless-stopped webExercises
Exercise
You run a nightly report generator that sometimes fails because a database is briefly unavailable. When it succeeds, it exits with code 0 and must not run again until the next night. Which policy fits, and why?
Show answer
on-failure with a retry limit, for example on-failure:3. It retries the transient database failures, but it does not restart after the exit code 0, and the limit stops endless retries if the database stays down.
Exercise
You stopped a container with docker stop before a host maintenance window. After the host reboots, you find it running again. Which policy was it using, and which policy would have kept it stopped?
Show answer
It was using always, which starts the container again when the daemon restarts, even after a manual stop. unless-stopped would have kept it stopped.