Restarting containers in Docker - Time & Space Complexity
We want to understand how the time to restart containers changes as the number of containers grows.
How does restarting many containers affect the total time taken?
Analyze the time complexity of the following code snippet.
for container in $(docker ps -q); do
docker restart $container
echo "Restarted container $container"
done
This script lists all running containers and restarts each one, printing a message after each restart.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Restarting each container one by one inside a loop.
- How many times: Once for each running container (n times).
As the number of containers increases, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 restarts |
| 100 | 100 restarts |
| 1000 | 1000 restarts |
Pattern observation: Doubling the number of containers doubles the total restart operations.
Time Complexity: O(n)
This means the time to restart containers grows linearly with the number of containers.
[X] Wrong: "Restarting multiple containers happens all at once, so time stays the same no matter how many containers there are."
[OK] Correct: Each container restart is done one after another, so total time adds up with each container.
Understanding how operations scale with input size helps you explain system behavior clearly and shows you can reason about real-world scripts.
"What if we restarted all containers in parallel instead of one by one? How would the time complexity change?"
Practice
docker restart <container_name> command do?Solution
Step 1: Understand the restart command
Thedocker restartcommand is designed to stop a running container and then start it again.Step 2: Compare with other commands
Stopping only halts the container, starting only launches it, and deleting removes it. Restart does both stop and start.Final Answer:
Stops and then starts the specified container -> Option DQuick Check:
Restart = Stop + Start [OK]
- Thinking restart only stops or only starts
- Confusing restart with remove or delete
- Assuming restart creates a new container
webapp?Solution
Step 1: Recall the basic restart syntax
The correct syntax isdocker restart <container_name>without extra flags for container name.Step 2: Evaluate the options
Options B, C, and D add invalid flags or keywords not recognized by Docker CLI for restart.Final Answer:
docker restart webapp -> Option BQuick Check:
Simple restart command = docker restart container_name [OK]
- Adding unnecessary flags like -c or --container
- Using extra keywords after restart
- Confusing restart syntax with other commands
docker restart myapp if the container myapp is currently stopped?Solution
Step 1: Understand restart behavior on stopped container
If the container is stopped,docker restartwill start it and print the container name.Step 2: Check Docker CLI output format
The command outputs the container name on success, not a success message or error if stopped.Final Answer:
myapp -> Option AQuick Check:
Restart prints container name on success [OK]
- Expecting an error if container is stopped
- Expecting a success message instead of container name
- Thinking restart does nothing if container stopped
docker restart 123abc but get an error: Error: No such container: 123abc. What is the likely cause?Solution
Step 1: Analyze the error message
The error says no such container, meaning Docker cannot find a container with ID or name '123abc'.Step 2: Check other options
Running container does not cause this error, Docker daemon not running causes different errors, and --force is not a valid restart flag.Final Answer:
The container ID or name is incorrect or does not exist -> Option CQuick Check:
No such container = wrong ID/name [OK]
- Assuming container must be stopped to restart
- Trying to force restart with invalid flags
- Ignoring Docker daemon status
db with this behavior?Solution
Step 1: Understand restart policies
The--restart=alwayspolicy restarts the container whenever it stops, including crashes and Docker daemon restarts.Step 2: Compare other policies
neverdisables restart,on-failurerestarts only on errors, andunless-stoppedrestarts except when manually stopped.Final Answer:
docker run --restart=always db -> Option AQuick Check:
Always restart = auto restart on crash and daemon restart [OK]
- Using --restart=never which disables restart
- Confusing on-failure with always
- Not setting restart policy at container start
Ready to prove it?
Pass the test and this pattern moves unknown
