Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 15/ 160
01. Containerization & Docker Fundamentals

Restart Policies (no, always, unless-stopped, on-failure)

Choose restart policies so containers recover correctly after crashes and reboots.

Intermediatealways · unless-stopped3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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.0 for 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

Loading interactive 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.

Concrete always setup

docker update --restart on-failure:5 worker && docker run -d --restart unless-stopped --name api api:2.1.0

Key takeaway: Prove unless-stopped with 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.

Deliberate failure test

Exercise this failure safely: using always for a batch job, or treating restart policy as a replacement for readiness and application error budgets.

Key takeaway: Automatic restarts improve transient recovery; bounded retries surface persistent faults and keep crash loops observable.

How it works, step by step

Step 1 of 4
Design

1. 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.

Choose a policy around the failure you want to heal automatically without hiding a deterministic startup bug.
Key numbers at this step
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}} worker in 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

Explicit always
Best for: Choose restart policies so containers recover correctly after crashes and reboots, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived unless-stopped experiments with low blast radius.

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.

Managed on-failure
Best for: Teams that centralize on-failure policy across many workloads.

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.0 as proof and skipping docker 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.0 address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Naming, Tagging & Labeling Containers
Next topic
Copying Files Between Host & Container (docker cp)