0
0
Dockerdevops~10 mins

Restart policies in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Restart policies
Start Container
Container Stops?
NoKeep Running
Yes
Check Restart Policy
no
Do Not Restart
always
Restart Container
on-failure
Restart if Exit Code != 0
unless-stopped
Restart unless Manually Stopped
Restart Container
Container Stops?
This flow shows how Docker checks if a container stops and then applies the chosen restart policy to decide whether to restart it.
Execution Sample
Docker
docker run --restart=on-failure:2 myapp
# Container runs, stops, and Docker restarts it up to 2 times if it fails
Runs a container with a restart policy that restarts it up to 2 times on failure.
Process Table
StepContainer StateRestart PolicyActionRestart CountResult
1Runningon-failure:2No action0Container runs normally
2Stopped with exit code 1on-failure:2Restart container1Container restarts
3Stopped with exit code 1on-failure:2Restart container2Container restarts again
4Stopped with exit code 1on-failure:2No restart (limit reached)2Container stays stopped
💡 Restart limit 2 reached, container stopped with failure exit code, no more restarts
Status Tracker
VariableStartAfter 1After 2After 3Final
Restart Count01222
Container StateRunningStopped (exit 1)Stopped (exit 1)Stopped (exit 1)Stopped
Key Moments - 3 Insights
Why does the container stop restarting after the third failure?
Because the restart policy 'on-failure:2' limits restarts to 2 times, as shown in execution_table step 4 where Restart Count is 2 and no restart happens.
What happens if the container exits with code 0 under 'on-failure' policy?
The container will not restart because 'on-failure' only restarts on non-zero exit codes, so no restart action is taken.
How does 'always' restart policy differ from 'on-failure'?
'always' restarts the container regardless of exit code or failure count, unlike 'on-failure' which restarts only on failure and up to a limit.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Restart Count at step 3?
A1
B2
C3
D0
💡 Hint
Check the Restart Count column in execution_table row for step 3
At which step does the container stop restarting due to reaching the restart limit?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the Action and Result columns in execution_table for when no restart occurs
If the restart policy was changed to 'always', what would happen at step 4?
AContainer would not restart
BContainer would restart again
CContainer would stop permanently
DContainer would restart only once more
💡 Hint
Recall that 'always' restarts regardless of exit code or count
Concept Snapshot
Docker Restart Policies:
- no: Do not restart container
- always: Restart container no matter what
- on-failure[:max]: Restart only on failure, up to max times
- unless-stopped: Restart unless container stopped manually
Use with docker run --restart option
Controls container availability after stops
Full Transcript
This visual execution shows how Docker restart policies work. When a container stops, Docker checks the restart policy. For 'on-failure:2', Docker restarts the container up to 2 times if it exits with a failure code. The execution table tracks container state, restart count, and actions taken. Key moments clarify why restarts stop after the limit and how different policies behave. The quiz tests understanding of restart counts and policy effects. The snapshot summarizes the main restart policies and their behavior.