📖 Scenario: You are managing multiple Docker containers running different applications on your server. To keep your system healthy, you want to monitor how much CPU and memory each container is using.
🎯 Goal: Build a simple setup to list all running Docker containers and display their CPU and memory usage in a clear way.
📋 What You'll Learn
Use the docker ps command to list running containers.
Use the docker stats --no-stream command to get current CPU and memory usage per container.
Extract container IDs and names to show alongside resource usage.
Display the information in a readable format.
💡 Why This Matters
🌍 Real World
Monitoring resource usage per container helps keep servers stable and prevents any container from using too many resources.
💼 Career
DevOps engineers often monitor container resources to optimize performance and troubleshoot issues in production environments.
Progress0 / 4 steps
1
List running Docker containers
Run the command docker ps --format "{{.ID}} {{.Names}}" to list all running containers with their IDs and names.
Docker
Hint
This command shows container IDs and names in a simple list.
2
Get resource usage snapshot
Run the command docker stats --no-stream --format "{{.Container}} {{.CPUPerc}} {{.MemUsage}}" to get a one-time snapshot of CPU and memory usage for each container.
Docker
Hint
This command shows CPU and memory usage once, without continuous updates.
3
Match container names with resource usage
Use the command docker ps --format "{{.ID}} {{.Names}}" and docker stats --no-stream --format "{{.Container}} {{.CPUPerc}} {{.MemUsage}}" together to match container names with their CPU and memory usage. You can use a shell loop or script to combine this information.
Docker
Hint
This loop gets each container's name and resource usage, then prints them together.
4
Display resource usage per container
Print the container name followed by its CPU and memory usage in the format: container_name: CPU% MemoryUsage. Use the loop from Step 3 to display this information.
Docker
Hint
The output should show each container's name followed by its CPU and memory usage.
Practice
(1/5)
1. What does the docker stats command show for each running container?
easy
A. The container's file system contents
B. The container's log output
C. Live resource usage like CPU, memory, network, and disk I/O
D. The container's environment variables
Solution
Step 1: Understand the purpose of docker stats
The docker stats command is designed to show live resource usage statistics for running containers.
Step 2: Identify the resources monitored
It monitors CPU, memory, network I/O, and disk I/O per container to help track performance.
Final Answer:
Live resource usage like CPU, memory, network, and disk I/O -> Option C
Quick Check:
docker stats = Live resource usage [OK]
Hint: Remember: docker stats shows live resource use per container [OK]
Common Mistakes:
Confusing docker stats with docker logs
Thinking it shows file system contents
Assuming it shows environment variables
2. Which of the following is the correct command to show live resource usage for all running Docker containers?
easy
A. docker stats
B. docker monitor
C. docker inspect stats
D. docker top
Solution
Step 1: Recall the command for resource monitoring
The command docker stats is the official Docker command to show live resource usage for containers.
Step 2: Verify other options
docker monitor and docker inspect stats are invalid commands, and docker top shows running processes, not resource stats.
Final Answer:
docker stats -> Option A
Quick Check:
Correct command = docker stats [OK]
Hint: Use 'docker stats' to see live container resource use [OK]
Common Mistakes:
Using docker top to check resource usage
Trying docker monitor which doesn't exist
Confusing docker inspect with stats
3. You run docker stats --no-stream on a container. What will you see?
medium
A. Continuous live updates of resource usage
B. A single snapshot of current resource usage
C. Only CPU usage without memory info
D. Error because --no-stream is invalid
Solution
Step 1: Understand the --no-stream option
The --no-stream option tells docker stats to show resource usage only once, not continuously.
Step 2: Identify the output behavior
It outputs a single snapshot of CPU, memory, network, and disk I/O usage at the moment the command runs.
Final Answer:
A single snapshot of current resource usage -> Option B
Quick Check:
--no-stream = single snapshot [OK]
Hint: Use --no-stream for one-time resource snapshot [OK]
Common Mistakes:
Expecting continuous updates with --no-stream
Thinking it shows only CPU usage
Assuming --no-stream is invalid
4. You try to run docker stats --format '{{.CPUPerc}} but get no output. What is the likely problem?
medium
A. The format string is missing a closing single quote
B. The container is stopped, so no stats show
C. The --format option is not supported in docker stats
D. You need to add --no-stream to see output
Solution
Step 1: Check the format string syntax
The command uses --format '{{.CPUPerc}} which is missing the closing single quote after the curly braces.
Step 2: Understand impact of missing quote
Without the closing quote, the shell or Docker cannot parse the format string, so no output appears.
Final Answer:
The format string is missing a closing single quote -> Option A
Quick Check:
Format string syntax error = missing quote [OK]
Hint: Always close quotes in --format strings [OK]
Common Mistakes:
Assuming stopped containers show no stats (they do not appear in stats)
Thinking --format is unsupported
Believing --no-stream is required for output
5. You want to monitor CPU and memory usage of a specific container named webapp and save the output once to a file. Which command should you use?
hard
A. docker stats --stream=false webapp > usage.txt
B. docker stats webapp --output usage.txt
C. docker monitor webapp > usage.txt
D. docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}' webapp > usage.txt
Solution
Step 1: Choose the correct command and options
To get a one-time snapshot, use --no-stream. To format output with container name, CPU, and memory, use --format with a table layout.
Step 2: Redirect output to a file
Use shell redirection > usage.txt to save the output to a file named usage.txt.