Listing containers (docker ps, docker ps -a) - Time & Space Complexity
When we list containers using Docker commands, the time it takes depends on how many containers exist.
We want to understand how the command's work grows as the number of containers increases.
Analyze the time complexity of the following Docker commands.
docker ps
# Lists running containers
docker ps -a
# Lists all containers, including stopped ones
These commands show container information by scanning the container list and displaying details.
Both commands scan through the list of containers one by one.
- Primary operation: Iterating over each container to gather and display its info.
- How many times: Once for each container in the list.
The time to list containers grows directly with the number of containers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 container checks and displays |
| 100 | About 100 container checks and displays |
| 1000 | About 1000 container checks and displays |
Pattern observation: Doubling containers roughly doubles the work done.
Time Complexity: O(n)
This means the command takes longer in direct proportion to how many containers there are.
[X] Wrong: "Listing containers is always instant no matter how many exist."
[OK] Correct: The command must check each container, so more containers mean more work and longer time.
Understanding how commands scale with data size helps you explain system behavior clearly and shows you think about efficiency.
What if Docker cached container info to avoid scanning all containers every time? How would that change the time complexity?
Practice
docker ps show by default?Solution
Step 1: Understand the default behavior of
Thedocker psdocker pscommand lists containers that are currently running.Step 2: Compare with other options
To see all containers, including stopped ones, you usedocker ps -a. The command does not list images.Final Answer:
Only running containers -> Option AQuick Check:
docker ps= running containers [OK]
docker ps shows running containers only [OK]- Thinking
docker psshows all containers - Confusing containers with images
- Assuming stopped containers show without -a
Solution
Step 1: Identify the flag for listing all containers
The-aflag withdocker pslists all containers, running or stopped.Step 2: Check other options
docker psalone shows only running containers. The options--runningand--stoppedare invalid flags.Final Answer:
docker ps -a -> Option DQuick Check:
docker ps -a= all containers [OK]
-a to list all containers, not just running [OK]- Using
docker pswithout-ato see stopped containers - Trying invalid flags like
--running - Confusing container listing with image listing
docker ps -a:
CONTAINER ID IMAGE STATUS NAMES abc123 nginx Up 2 hours webserver def456 redis Exited (0) 3 days ago cacheWhat will
docker ps show?Solution
Step 1: Analyze the status of containers
The container namedwebserveris "Up 2 hours" meaning it is running. The container namedcacheis "Exited", meaning it is stopped.Step 2: Understand
docker psoutputdocker psshows only running containers, so it will list onlywebserver.Final Answer:
Only the webserver container -> Option CQuick Check:
Running containers = webserver only [OK]
- Listing stopped containers with
docker ps - Assuming all containers show without
-a - Confusing container names with images
docker ps -a but get an error: unknown flag: --all. What is the likely cause?Solution
Step 1: Check the flag syntax
The correct flag to list all containers is-a, not--all. The error shows the flag is unknown.Step 2: Rule out other causes
If the daemon was not running, the error would be different. Running as root is not required for listing containers. An empty list does not cause a flag error.Final Answer:
You typed--allinstead of-a-> Option AQuick Check:
Use-aflag, not--all[OK]
-a, not --all [OK]- Using invalid flag
--all - Assuming root is needed for listing
- Ignoring daemon status errors
Solution
Step 1: Understand formatting options
The--formatflag lets you customize output. Using{{.ID}} {{.Names}}shows only container IDs and names.Step 2: Evaluate other options
--quietshows only IDs, not names.--filteris for filtering, not formatting.--shortis not a valid flag.Final Answer:
docker ps -a --format "{{.ID}} {{.Names}}" -> Option BQuick Check:
Use--formatto customize output [OK]
--format to show only needed fields [OK]- Using invalid flags like
--short - Confusing
--quietwith showing names - Trying to filter instead of format output
Ready to prove it?
Pass the test and this pattern moves unknown
