0
0
Dockerdevops~5 mins

Container metrics collection in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run containers, you want to know how much CPU, memory, and other resources they use. Container metrics collection helps you see this information so you can keep your apps healthy and fix problems quickly.
When you want to check if a container is using too much memory and might crash.
When you need to monitor CPU usage of your app containers to avoid slowdowns.
When you want to track network traffic inside containers to detect unusual activity.
When you want to collect data to improve your app's performance over time.
When you want to see resource usage before scaling your containers up or down.
Commands
This command shows the current CPU, memory, and network usage of all running containers once. It helps you get a quick snapshot of resource use.
Terminal
docker stats --no-stream
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c3f2a4d5e6 my-app 0.15% 50MiB / 1GiB 4.88% 1.2MB / 1.1MB 0B / 0B 5
--no-stream - Show metrics only once instead of continuously updating
This command continuously shows live metrics for all running containers. It updates every second so you can watch resource use over time.
Terminal
docker stats
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c3f2a4d5e6 my-app 0.20% 52MiB / 1GiB 5.07% 1.3MB / 1.1MB 0B / 0B 5 b1c3f2a4d5e6 my-app 0.18% 51MiB / 1GiB 4.98% 1.3MB / 1.1MB 0B / 0B 5
This command shows CPU and memory usage for the container named 'my-app' once, using a simple custom format for easier reading.
Terminal
docker stats my-app --no-stream --format "{{.Name}}: CPU {{.CPUPerc}}, MEM {{.MemUsage}}"
Expected OutputExpected
my-app: CPU 0.15%, MEM 50MiB / 1GiB
--format - Customize the output format for easier reading
Key Concept

If you remember nothing else from this pattern, remember: docker stats shows real-time resource use of containers to help you monitor their health.

Common Mistakes
Running 'docker stats' without specifying --no-stream when you only want a snapshot.
The command will keep running and updating, which can be confusing if you only want one result.
Add the --no-stream flag to get a single snapshot of container metrics.
Trying to get metrics for a container that is not running.
Docker stats only works for running containers and will show no output or an error.
Make sure the container is running before using docker stats.
Not using --format to simplify output when scripting or automating monitoring.
Raw output can be hard to parse and use in scripts.
Use the --format flag to get only the needed fields in a clean format.
Summary
Use 'docker stats --no-stream' to get a one-time snapshot of container resource usage.
Use 'docker stats' to watch live, updating metrics for all running containers.
Use '--format' to customize the output for easier reading or automation.