Restart Policies (no, always, unless-stopped, on-failure)
Choose restart policies so containers recover correctly after crashes and reboots.
In plain words
Choose a policy around the failure you want to heal automatically without hiding a deterministic startup bug.
Think of it like…
A smoke alarm that relights the pilot can recover a transient outage, but should stop trying after a gas leak-the equivalent of a crash loop with no investigation.
Where you’ve seen it
In practice, docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0 is the concrete move for this job: choose restart policies so containers recover correctly after crashes and reboots. The release is not accepted until docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker confirms the result.
Words you’ll hear
- always
- The implementation control in
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0for choose restart policies so containers recover correctly after crashes and reboots. - unless-stopped
- The result to observe with
docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker, rather than infer from a successful command alone. - on-failure
- The production boundary exposed when using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.
Visual diagram
Theory & deep dive
always control path
The concrete mechanism is docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0. The observable result is docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker; if those disagree, inspect the runtime evidence before changing unrelated settings.
Choose restart policies so containers recover correctly after crashes and reboots. Keep always, unless-stopped and on-failure in the same deployment record so the intent and the observed state cannot drift apart.
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0
docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker, not with command success alone.on-failure failure boundary
A crash loop can consume logs and CPU while on-call thinks a service is healthy. always can revive a container that an operator intentionally stopped before host reboot, while unless-stopped preserves that maintenance intent. The narrow failure to design for is using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.
Accept this compromise explicitly: Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable. The controlled check-docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker-also identifies the point where a different design is safer.
Exercise this failure safely: using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A crash loop can consume logs and CPU while on-call thinks a service is healthy. always can revive a container that an operator intentionally stopped before host reboot, while unless-stopped preserves that maintenance intent.
- Intent
- always
When to use it & trade-offs
What it is
Restart policy is a daemon-side recovery rule: always restarts after any exit, unless-stopped ignores a daemon restart after a human stop, on-failure restarts only non-zero exits and can have a maximum retry count, and no never restarts automatically.
Why it matters: A crash loop can consume logs and CPU while on-call thinks a service is healthy. always can revive a container that an operator intentionally stopped before host reboot, while unless-stopped preserves that maintenance intent.
When to reach for it
- The fit is strongest for this job: choose restart policies so containers recover correctly after crashes and reboots. Pair it with
docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} workerin the runbook. - Production always changes should make unless-stopped observable through
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0, rather than trusting the runtime default. - Keep a rollback example for the case where using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.
When to avoid or be careful
- Do not accept command success while the system can still fail by using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.
- Avoid erasing on-failure protection merely to dodge this cost: “Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable”.
- Do not replace the evidence path-
docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker-with a screenshot or a mutable tag.
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0: practical choices
Strengths: Restart policy is a daemon-side recovery rule: always restarts after any exit, unless-stopped ignores a daemon restart after a human stop, on-failure restarts only non-zero exits and can have a maximum retry count, and no never restarts automatically.
Watch out: Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable.
Strengths: It can begin with docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0 and fewer surrounding controls.
Watch out: It silently accepts using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets, which makes the result hard to reproduce after promotion.
Strengths: A crash loop can consume logs and CPU while on-call thinks a service is healthy. always can revive a container that an operator intentionally stopped before host reboot, while unless-stopped preserves that maintenance intent.
Watch out: The platform adds an ownership and debugging layer; retain docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0
- docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker
- Failure signature: using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets
- Trade-off: Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable.
Benchmarks & rules of thumb
- 5 retries
- one bounded on-failure policy
- 0 retries
- means unlimited retries for on-failure without a suffix
- 2 intents
- daemon reboot recovery and operator stop intent must be distinguished
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0as proof and skippingdocker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker. - Using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets
- Optimizing always without accepting that automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable.
Likely interviewer follow-ups
- Why does
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0address always here rather than through application code? - How would you distinguish healthy unless-stopped from the failure “using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets” using
docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker? - When would this trade-off-“Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable”-justify a different on-failure design?
docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0-and one proof-docker inspect -f {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}:{{.RestartCount}} worker. Then name the boundary: using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Restart Policies (no, always, unless-stopped, on-failure) with generative AI interview Q&A and hands-on practice.