0
0
Dockerdevops~5 mins

Restarting containers in Docker - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Restarting containers
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

As the number of containers increases, the total time grows roughly in direct proportion.

Input Size (n)Approx. Operations
1010 restarts
100100 restarts
10001000 restarts

Pattern observation: Doubling the number of containers doubles the total restart operations.

Final Time Complexity

Time Complexity: O(n)

This means the time to restart containers grows linearly with the number of containers.

Common Mistake

[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.

Interview Connect

Understanding how operations scale with input size helps you explain system behavior clearly and shows you can reason about real-world scripts.

Self-Check

"What if we restarted all containers in parallel instead of one by one? How would the time complexity change?"