Container metrics collection in Docker - Time & Space Complexity
When collecting container metrics, we want to know how the time to gather data changes as the number of containers grows.
We ask: How does the work increase when more containers run?
Analyze the time complexity of the following code snippet.
#!/bin/bash
containers=$(docker ps -q)
for container in $containers; do
docker stats --no-stream $container > metrics/$container.txt
done
wait
This script lists all running containers, then collects metrics for each one individually and saves them to files.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each container to run a metrics command.
- How many times: Once per container running on the system.
As the number of containers increases, the script runs more commands, one per container.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 metrics commands |
| 100 | 100 metrics commands |
| 1000 | 1000 metrics commands |
Pattern observation: The work grows directly with the number of containers.
Time Complexity: O(n)
This means the time to collect metrics grows in a straight line as more containers run.
[X] Wrong: "Collecting metrics for many containers takes the same time as for one container."
[OK] Correct: Each container adds extra work, so time increases with the number of containers.
Understanding how work grows with container count helps you design efficient monitoring tools and shows you can think about scaling in real systems.
"What if we collected metrics for all containers in one combined command instead of one by one? How would the time complexity change?"
Practice
docker stats command primarily show?Solution
Step 1: Understand the purpose of
This command is designed to display live resource usage statistics such as CPU, memory, and network for running containers.docker statsStep 2: Differentiate from other Docker commands
Other commands likedocker imageslist images,docker logsshow logs, anddocker infoshows daemon info, so they do not match the description.Final Answer:
Live resource usage metrics of running containers -> Option CQuick Check:
docker stats= live container metrics [OK]
- Confusing stats with logs
- Thinking stats shows images
- Assuming stats shows config
Solution
Step 1: Identify the flag for single snapshot
The--no-streamflag tellsdocker statsto show metrics once and exit instead of streaming live updates.Step 2: Verify other options
Flags like--single,--once, and--snapshotare not valid Docker flags for this command.Final Answer:
docker stats --no-stream -> Option AQuick Check:
Single snapshot flag = --no-stream [OK]
- Using invalid flags like --single
- Omitting the flag and expecting single output
- Confusing with other Docker commands
docker stats --no-stream my_container if my_container is running?Solution
Step 1: Understand the command components
docker stats --no-streamrequests a single snapshot, and specifyingmy_containerlimits output to that container only.Step 2: Predict the output
Sincemy_containeris running, the command outputs one snapshot of its resource usage metrics, not a stream or logs.Final Answer:
A single snapshot of resource usage metrics for my_container -> Option DQuick Check:
--no-stream + container name = single snapshot for that container [OK]
- Expecting continuous stream despite --no-stream
- Confusing stats output with logs
- Assuming error if container is running
docker stats --no-stream my_container but get an error: Error: No such container: my_container. What is the most likely cause?Solution
Step 1: Analyze the error message
The error clearly states the containermy_containercannot be found, meaning it is either stopped or never created.Step 2: Check other options
The--no-streamflag is valid, Docker daemon errors would be different, anddocker statsdoes not support--allto show stopped containers.Final Answer:
The container my_container is not running or does not exist -> Option BQuick Check:
Error 'No such container' means container missing [OK]
- Assuming flag is wrong
- Thinking Docker daemon is down
- Trying unsupported flags like --all
webapp and db with a single snapshot. Which command should you run?Solution
Step 1: Understand how to specify multiple containers
Docker stats accepts container names as arguments separated by spaces to monitor multiple containers.Step 2: Use the correct flag placement
The--no-streamflag should come before container names;docker stats --no-stream webapp dbplaces it correctly.Step 3: Evaluate other options
docker stats webapp db --no-stream incorrectly places the flag after container names, causing Docker to interpret--no-streamas a container name and resulting in an error. Options C and D use invalid or unsupported--filtersyntax for this command.Final Answer:
docker stats --no-stream webapp db -> Option AQuick Check:
Multiple containers + --no-stream = docker stats --no-stream webapp db [OK]
- Using invalid --filter syntax
- Placing flags after container names incorrectly
- Trying to monitor stopped containers
Ready to prove it?
Pass the test and this pattern moves unknown
