How does container restart policy work in Docker?
Learn Docker's four restart policies (no, on-failure, always, unless-stopped), how back-off works, and when to use each to keep containers running.
Expected Interview Answer
A Docker restart policy tells the Docker daemon whether and when to automatically restart a container after it stops, so you don't have to restart it manually or with an external supervisor.
You set it with the --restart flag (or restart: in Compose). The four options are no (never restart, the default), on-failure[:max-retries] (restart only on a non-zero exit code, optionally capped), always (restart whenever it stops, and on daemon start), and unless-stopped (like always, but respects a manual stop across daemon restarts). The daemon applies an increasing back-off delay between restart attempts to avoid tight crash loops.
- Keeps long-running services available without an external process manager
- Automatically recovers from transient crashes
- on-failure retries only real failures, not clean exits
- unless-stopped honors an operator's deliberate stop
- Back-off delay prevents runaway restart loops
AI Mentor Explanation
A restart policy is like a team's rule for sending in a nightwatchman. 'no' means once a batter is out, that's it for the innings. 'on-failure' means only send a replacement when a wicket actually falls, not when a batter retires by choice. 'always' keeps feeding fresh batters no matter how they left, and 'unless-stopped' means the captain's declaration is respected until the next match resumes.
Step-by-Step Explanation
Step 1
Choose the policy
Pick no, on-failure, always, or unless-stopped based on whether the container is a one-off job or a long-running service.
Step 2
Set it at run time
Pass --restart on docker run, or add restart: to the service in docker-compose.yml.
Step 3
Cap retries if needed
Use on-failure:5 to limit restart attempts for jobs that shouldn't loop forever on a persistent error.
Step 4
Understand back-off
The daemon waits progressively longer between attempts (starting around 100ms and doubling) to avoid hammering a crashing container.
Step 5
Verify behavior
Inspect with docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' and check RestartCount to confirm the policy is applied.
What Interviewer Expects
- Naming all four policies and their differences
- Difference between always and unless-stopped across daemon restarts
- Awareness of exponential back-off between retries
- When on-failure with a retry cap is appropriate
- That restart policies only apply to containers, not to failing health checks by themselves
Common Mistakes
- Confusing always with unless-stopped
- Thinking on-failure restarts on a clean zero exit code
- Assuming a restart policy fixes a fundamentally broken image
- Forgetting that a manually stopped 'always' container restarts when the daemon starts
- Expecting restart policies to replace an orchestrator's self-healing in production clusters
Best Answer (HR Friendly)
“A restart policy is a rule that tells Docker whether to bring a container back up automatically after it stops. You can say never restart, restart only if it crashed, or always keep it running, so your services stay available without someone watching them.”
Code Example
# Never restart (the default)
docker run --restart=no nginx
# Restart only on a non-zero exit, up to 5 times
docker run --restart=on-failure:5 my-worker
# Always restart, including when the daemon starts
docker run -d --restart=always nginx
# Like always, but a manual stop is remembered across reboots
docker run -d --restart=unless-stopped nginx
# Check the applied policy and how many times it restarted
docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' my-workerservices:
web:
image: nginx
restart: unless-stopped
batch:
image: my-worker
restart: on-failureFollow-up Questions
- What is the difference between restart: always and unless-stopped?
- How does Docker's restart back-off delay work?
- Why won't a restart policy fix a container that crashes instantly on every start?
- How do restart policies differ from a health check?
- How does Kubernetes restartPolicy compare to Docker's?
MCQ Practice
1. Which restart policy restarts a container ONLY when it exits with a non-zero code?
on-failure restarts only on a non-zero (error) exit code, and can be capped with a max retry count.
2. What is the key difference between 'always' and 'unless-stopped'?
Both restart on crash and daemon start, but unless-stopped remembers a deliberate manual stop and won't bring the container back after a daemon restart.
3. What is the default restart policy for a Docker container?
If you don't specify --restart, the policy is 'no' and the container is never restarted automatically.
Flash Cards
Four Docker restart policies? — no, on-failure[:max], always, unless-stopped.
When does on-failure restart? — Only on a non-zero exit code, optionally up to a retry cap.
always vs unless-stopped? — Both restart on crash and daemon start; unless-stopped respects a manual stop across daemon restarts.
Default policy? — 'no' — never restart automatically.
Why the back-off delay? — To prevent a tight crash loop from hammering the host; delay grows between attempts.
Continue Learning
Related Interview Questions
What is the difference between Docker Compose and Docker Swarm?
medium
Your container is marked unhealthy but Docker never restarts it. Why, and how do health checks and restart policies actually interact?
hard
What is the difference between docker run, docker start, and docker exec?
easy
What is Docker Compose and when should you use it?
easy