Container states (created, running, paused, stopped) in Docker - Time & Space Complexity
We want to understand how checking container states scales as the number of containers grows.
How does the time to list containers change when we have more containers?
Analyze the time complexity of the following Docker commands.
docker ps -a
# Lists all containers with their states
docker inspect <container_id>
# Shows detailed state of one container
These commands help us see container states like created, running, paused, or stopped.
Look for repeated steps in these commands.
- Primary operation: Iterating over all containers to list their states.
- How many times: Once per container in the list.
As the number of containers grows, the time to list all containers grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 container checks |
| 100 | 100 container checks |
| 1000 | 1000 container checks |
Pattern observation: The time grows linearly as the number of containers increases.
Time Complexity: O(n)
This means the time to check container states grows directly with the number of containers.
[X] Wrong: "Checking container states is always instant, no matter how many containers exist."
[OK] Correct: Each container must be checked individually, so more containers mean more work and more time.
Understanding how operations scale with container count helps you manage and troubleshoot Docker environments efficiently.
"What if we only check running containers instead of all containers? How would the time complexity change?"
Practice
Solution
Step 1: Understand container states
The 'running' state means the container is actively executing its processes.Step 2: Compare other states
'Created' means container is made but not started, 'paused' means temporarily halted, 'stopped' means container is not running.Final Answer:
running -> Option DQuick Check:
Active container = running [OK]
- Confusing 'created' with 'running'
- Thinking 'paused' means running
- Mixing 'stopped' with 'running'
Solution
Step 1: Recall docker ps usage
'docker ps' shows only running containers by default.Step 2: Use option to show all containers
'docker ps -a' lists all containers, including stopped and paused ones.Final Answer:
docker ps -a -> Option AQuick Check:
Show all containers = docker ps -a [OK]
- Using 'docker ps' alone shows only running containers
- Confusing 'docker start' with listing containers
- Thinking 'docker pause' lists containers
docker create nginx docker start <container_id> docker pause <container_id>
Solution
Step 1: Analyze commands sequence
'docker create' makes container in 'created' state, 'docker start' moves it to 'running'.Step 2: Effect of 'docker pause'
'docker pause' suspends container processes without stopping it, changing state to 'paused'.Final Answer:
paused -> Option AQuick Check:
Pause command sets state to paused [OK]
- Assuming container stays running after pause
- Confusing created with paused
- Thinking pause stops the container
docker pause mycontainer but got an error saying container is not running. What is the likely cause?Solution
Step 1: Understand pause command requirements
'docker pause' only works on running containers to suspend them temporarily.Step 2: Interpret error message
Error about container not running means it is stopped or never started, so pause cannot apply.Final Answer:
Container is stopped or not started -> Option BQuick Check:
Pause needs running container [OK]
- Trying to pause stopped containers
- Ignoring container state before pausing
- Assuming pause works on any container
Solution
Step 1: Understand stopping vs pausing
'docker stop' stops container and loses running state; 'docker pause' suspends without stopping.Step 2: Identify commands to pause and resume
'docker pause' suspends container, 'docker unpause' resumes it preserving state.Final Answer:
docker pause & docker unpause -> Option CQuick Check:
Pause/unpause = temporary stop and resume [OK]
- Using stop loses container state
- Confusing kill with pause
- Restarting instead of unpausing
Ready to prove it?
Pass the test and this pattern moves unknown
