How to Use Docker Stats: Monitor Container Resource Usage
Use the
docker stats command to see real-time resource usage statistics like CPU, memory, and network for running containers. Run docker stats [container_name_or_id] to monitor specific containers or just docker stats to see all running containers.Syntax
The basic syntax of the docker stats command is:
docker stats [OPTIONS] [CONTAINER...]
Here:
OPTIONSare optional flags to customize output.CONTAINERis one or more container names or IDs to monitor. If omitted, stats for all running containers are shown.
bash
docker stats [OPTIONS] [CONTAINER...]
Example
This example shows how to monitor all running containers' resource usage in real time. It displays CPU %, memory usage, network I/O, and more.
bash
docker stats
Output
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f3c1a1b2c3d4 my_container 0.07% 12.34MiB / 1GiB 1.21% 1.2kB / 2.3kB 0B / 0B 5
Common Pitfalls
Common mistakes when using docker stats include:
- Running the command without any running containers, which shows no output.
- Using container names or IDs incorrectly, causing errors.
- Expecting
docker statsto show historical data; it only shows live stats.
Always ensure containers are running before monitoring.
bash
docker stats wrong_container_name # Error: No such container: wrong_container_name # Correct usage: docker stats my_container
Output
Error: No such container: wrong_container_name
Quick Reference
| Option | Description |
|---|---|
| --no-stream | Show stats once and exit instead of continuous updates |
| --format | Format output using a Go template for custom display |
| [CONTAINER...] | Specify one or more containers to monitor |
Key Takeaways
Use
docker stats to monitor live resource usage of running containers.Specify container names or IDs to focus on specific containers.
The command shows CPU, memory, network, block I/O, and process count.
Use
--no-stream to get a single snapshot instead of continuous updates.Ensure containers are running before using
docker stats to see output.