0
0
Dockerdevops~5 mins

Resource monitoring per container in Docker - Commands & Configuration

Choose your learning style9 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.