Restart policies in Docker - Time & Space Complexity
We want to understand how the time it takes for Docker to restart containers changes as the number of containers grows.
How does Docker handle restarting many containers, and how does that affect the time spent?
Analyze the time complexity of the following Docker commands setting restart policies.
docker run --restart=always -d nginx
docker run --restart=on-failure:3 -d redis
docker run --restart=no -d alpine
docker ps --filter "restart=always"
This code runs three containers with different restart policies and lists containers with a specific restart policy.
Look for repeated actions that affect time.
- Primary operation: Docker checks each container's restart policy when a container stops.
- How many times: Once per container stop event, repeated for each container that stops.
As the number of containers increases, Docker must check more restart policies when containers stop.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of containers.
Time Complexity: O(n)
This means the time to handle restart policies grows linearly with the number of containers.
[X] Wrong: "Restart policies are handled instantly regardless of how many containers exist."
[OK] Correct: Docker must check each container's policy when it stops, so more containers mean more checks and more time.
Understanding how Docker manages restart policies helps you reason about system behavior as it scales, a useful skill in real-world DevOps tasks.
"What if Docker handled restart policies in parallel for all containers? How would the time complexity change?"
Practice
--restart=always policy do when used with docker run?Solution
Step 1: Understand the restart policy options
The--restartflag controls when Docker restarts a container automatically.Step 2: Analyze the
Thealwayspolicyalwayspolicy means Docker restarts the container whenever it stops, regardless of exit status or manual stop.Final Answer:
It restarts the container automatically whenever it stops, no matter the reason. -> Option CQuick Check:
--restart=always= restart always [OK]
- Confusing 'always' with 'on-failure'
- Thinking 'always' ignores manual stops
- Mixing 'no' restart with 'always'
Solution
Step 1: Recall the syntax for restart policies
The correct syntax uses--restart=policy[:max-retries]with an equals sign and optional retry count.Step 2: Identify the option that matches restart on failure with max retries
on-failure:5means restart only if the container fails, up to 5 times.Final Answer:
docker run --restart=on-failure:5 myimage -> Option AQuick Check:
Correct syntax uses equals sign and policy [OK]
- Missing equals sign (=) after --restart
- Using wrong policy names or formats
- Confusing 'always' with 'on-failure'
docker run --restart=unless-stopped myapp
Solution
Step 1: Understand the 'unless-stopped' policy
This policy restarts the container automatically except when you stop it manually.Step 2: Apply the policy to the command
Since the command uses--restart=unless-stopped, the container will restart on failures or Docker daemon restarts, but not if manually stopped.Final Answer:
The container restarts unless you manually stop it. -> Option AQuick Check:
unless-stoppedmeans restart except manual stop [OK]
- Thinking it restarts always even after manual stop
- Confusing 'unless-stopped' with 'on-failure'
- Assuming no restart happens
docker run --restart always:3 myimage
Why does it fail?
Solution
Step 1: Check the restart policy syntax
Thealwayspolicy does not support a retry count suffix like ':3'. Onlyon-failureaccepts that.Step 2: Identify the cause of failure
Usingalways:3is invalid syntax, so Docker rejects the command.Final Answer:
The 'always' policy does not accept a retry count. -> Option DQuick Check:
alwayspolicy has no retry count [OK]
- Adding retry count to 'always' policy
- Assuming all policies accept retry counts
- Ignoring error messages from Docker
Solution
Step 1: Understand the requirement
The container should restart only if it crashes (fails), but not if stopped manually.Step 2: Match the restart policy
on-failurerestarts the container only when it exits with an error, ignoring manual stops.Step 3: Verify the command
The commanddocker run --restart=on-failure mycontainercorrectly applies this policy.Final Answer:
docker run --restart=on-failure mycontainer -> Option BQuick Check:
on-failure= restart only on crash [OK]
- Using 'always' which restarts even after manual stop
- Using 'unless-stopped' which restarts after manual stop
- Choosing 'no' which disables restart
Ready to prove it?
Pass the test and this pattern moves unknown
