Resource monitoring per container in Docker - Time & Space Complexity
When monitoring resources for Docker containers, it's important to know how the time to gather data changes as the number of containers grows.
We want to understand how the monitoring process scales with more containers running.
Analyze the time complexity of the following Docker command snippet.
docker stats --no-stream --format "{{.Container}}: CPU {{.CPUPerc}}, Mem {{.MemUsage}}"
This command fetches the current CPU and memory usage for all running containers once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Gathering resource stats for each container.
- How many times: Once per container running on the system.
As the number of containers increases, the command collects stats for each one individually.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 resource checks |
| 100 | 100 resource checks |
| 1000 | 1000 resource checks |
Pattern observation: The work grows directly with the number of containers.
Time Complexity: O(n)
This means the time to get resource stats grows in a straight line as you add more containers.
[X] Wrong: "Getting stats for all containers takes the same time no matter how many containers run."
[OK] Correct: Each container adds extra work because the system must check its resource use separately.
Understanding how monitoring scales helps you design systems that stay fast and responsive as they grow.
"What if we changed the command to stream stats continuously instead of a single snapshot? How would the time complexity change?"