0
0
Dockerdevops~5 mins

Container resource usage stats in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes containers use too much memory or CPU and slow down your system. Checking resource usage helps you find which container is causing the problem and fix it quickly.
When your server feels slow and you want to see if a container is using too much CPU.
When you want to monitor memory use of a container running a database to avoid crashes.
When you need to check network traffic of a container to troubleshoot slow responses.
When you want to track resource use over time to plan for scaling your app.
When you want to confirm that resource limits you set on containers are working.
Commands
This command lists all running containers so you can see their names and IDs before checking their stats.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1c2d3e4f5g6 nginx:1.23 "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp my-nginx 7h8i9j0k1l2m redis:7.0 "docker-entrypoint.s…" 3 hours ago Up 3 hours 6379/tcp my-redis
This command shows the current CPU, memory, and network usage of the container named 'my-nginx'. The --no-stream flag makes it show the stats once and then stop.
Terminal
docker stats my-nginx --no-stream
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c2d3e4f5g6 my-nginx 0.15% 12.3MiB / 1GiB 1.20% 1.2MB / 1.1MB 0B / 0B 5
--no-stream - Show stats only once instead of continuously updating
This command shows live resource usage stats for all running containers, updating every second until you stop it with Ctrl+C.
Terminal
docker stats
Expected OutputExpected
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS b1c2d3e4f5g6 my-nginx 0.20% 12.5MiB / 1GiB 1.22% 1.3MB / 1.2MB 0B / 0B 5 7h8i9j0k1l2m my-redis 0.05% 8.7MiB / 512MiB 1.70% 0.5MB / 0.4MB 0B / 0B 3
Key Concept

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

Common Mistakes
Running 'docker stats' without specifying --no-stream when you only want a single snapshot.
The command keeps running and updating, which can be confusing if you expect just one output.
Add the --no-stream flag to get a one-time snapshot of container stats.
Trying to check stats of a container that is not running.
Docker stats only works for running containers and will show an error or no output.
Use 'docker ps' first to confirm the container is running before checking stats.
Summary
Use 'docker ps' to list running containers and find their names or IDs.
Use 'docker stats container-name --no-stream' to get a one-time snapshot of resource usage.
Use 'docker stats' to watch live resource usage of all running containers until you stop it.