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?"