Bird
Raised Fist0
Dockerdevops~5 mins

Resource monitoring per container in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes, you want to see how much memory or CPU each container is using. This helps you find slow or heavy containers so you can fix or stop them.
When you want to check if a container is using too much memory and slowing down your server
When you want to compare CPU usage between different containers running on the same machine
When you need to find out if a container is stuck or not responding because it uses too many resources
When you want to monitor resource use during testing to optimize your app's performance
When you want to keep your server healthy by stopping containers that use too many resources
Commands
This command shows the current CPU, memory, and network usage for all running containers once. It helps you quickly see resource use without continuous updates.
Terminal
docker stats --no-stream
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c2d3e4f5g6 my-app 2.35% 50.3MiB / 1GiB 4.91% 1.2MB / 1.1MB 0B / 0B 12 c7d8e9f0g1h2 db-server 0.00% 120MiB / 2GiB 5.86% 500kB / 450kB 0B / 0B 20
→
--no-stream - Show resource usage only once instead of updating continuously
This command shows live resource usage for the container named 'my-app'. It updates every second so you can watch how usage changes over time.
Terminal
docker stats my-app
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c2d3e4f5g6 my-app 2.50% 51.0MiB / 1GiB 5.00% 1.3MB / 1.2MB 0B / 0B 12 b1c2d3e4f5g6 my-app 2.70% 52.1MiB / 1GiB 5.09% 1.4MB / 1.3MB 0B / 0B 12 b1c2d3e4f5g6 my-app 2.60% 51.5MiB / 1GiB 5.04% 1.5MB / 1.4MB 0B / 0B 12
This command shows the memory limit set for the container 'my-app'. It helps you know if the container has a memory cap.
Terminal
docker inspect --format='{{.Name}} uses {{.HostConfig.Memory}} bytes memory limit' my-app
Expected OutputExpected
/my-app uses 1073741824 bytes memory limit
→
--format - Customize output to show only specific information
Key Concept

If you remember nothing else from this pattern, remember: docker stats shows real-time resource use per container to help you monitor and manage your apps.

Common Mistakes
Running 'docker stats' without specifying --no-stream when you only want a snapshot
The command keeps running and updating, which can clutter your terminal and confuse you
Add the --no-stream flag to get a single snapshot of resource usage
Trying to monitor a container that is not running
Docker stats only works on running containers and will show an error or no output
Make sure the container is running before using docker stats
Not checking container resource limits with docker inspect
You might think a container can use unlimited memory or CPU, but limits may be set and affect performance
Use docker inspect with --format to see resource limits set on containers
Summary
Use 'docker stats --no-stream' to get a one-time snapshot of resource use for all running containers.
Use 'docker stats container-name' to watch live resource use for a specific container.
Use 'docker inspect --format' to check resource limits set on a container.

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

  1. Step 1: Understand the purpose of docker stats

    The docker stats command is designed to show live resource usage statistics for running containers.
  2. Step 2: Identify the resources monitored

    It monitors CPU, memory, network I/O, and disk I/O per container to help track performance.
  3. Final Answer:

    Live resource usage like CPU, memory, network, and disk I/O -> Option C
  4. 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

  1. Step 1: Recall the command for resource monitoring

    The command docker stats is the official Docker command to show live resource usage for containers.
  2. Step 2: Verify other options

    docker monitor and docker inspect stats are invalid commands, and docker top shows running processes, not resource stats.
  3. Final Answer:

    docker stats -> Option A
  4. 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

  1. Step 1: Understand the --no-stream option

    The --no-stream option tells docker stats to show resource usage only once, not continuously.
  2. 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.
  3. Final Answer:

    A single snapshot of current resource usage -> Option B
  4. 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

  1. Step 1: Check the format string syntax

    The command uses --format '{{.CPUPerc}} which is missing the closing single quote after the curly braces.
  2. Step 2: Understand impact of missing quote

    Without the closing quote, the shell or Docker cannot parse the format string, so no output appears.
  3. Final Answer:

    The format string is missing a closing single quote -> Option A
  4. 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

  1. 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.
  2. Step 2: Redirect output to a file

    Use shell redirection > usage.txt to save the output to a file named usage.txt.
  3. Final Answer:

    docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}' webapp > usage.txt -> Option D
  4. Quick Check:

    One-time formatted output saved to file = docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}' webapp > usage.txt [OK]
Hint: Use --no-stream and --format, redirect output to file [OK]
Common Mistakes:
  • Using invalid docker monitor command
  • Assuming --output option exists
  • Using --stream=false instead of --no-stream
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard