How to Monitor Container Resource Usage with Apple container stats
Problem
When I run multiple containers on my Mac, I need to know how much CPU and memory each one uses. Is the database container eating too much memory? Is the web server hitting the CPU limit? Without monitoring, I am guessing.
Container Stats Basics
The container stats command shows real-time resource usage for running containers:
container statsThis opens an interactive live display that updates continuously — similar to the top command. Press Ctrl+C to exit.
Sample Output
CONTAINER NAME CPU % MEM USAGE / LIMIT NET RX NET TX BLOCK I/O PIDSmy-web-server 12.5% 120.5 MiB / 2.00 GiB 1.2 GB 3.4 GB 256 MB / 0 B 8my-database 8.2% 845.3 MiB / 4.00 GiB 3.4 GB 1.2 GB 4.5 GB / 2 GB 34Key metrics explained:
- CPU %: percentage of available cores. A dual-core container can show up to 200%.
- MEM USAGE: current memory usage vs configured limit.
- NET RX/TX: network bytes received and transmitted.
- BLOCK I/O: disk read and write volumes.
- PIDS: number of processes inside the container.
Monitor Specific Containers
Pass container names or IDs:
container stats my-web-server my-databaseSingle Snapshot
For one-time checks, use --no-stream:
container stats --no-stream my-web-serverCONTAINER NAME CPU % MEM USAGE / LIMIT NET RX NET TX BLOCK I/O PIDSmy-web-server 12.5% 120.5 MiB / 2.00 GiB 1.2 GB 3.4 GB 256 MB / 0 B 8JSON Output for Automation
For monitoring scripts, use JSON format:
container stats --format json --no-stream my-web-server[ { "name": "my-web-server", "cpu_percent": 12.5, "memory_usage": 120500000, "memory_limit": 2147483648, "net_rx": 1200000000, "net_tx": 3400000000, "block_read": 256000000, "block_write": 0, "pids": 8 }]Other supported output formats: yaml and toml.
Use Cases
I use container stats for:
- Finding memory leaks: watch memory usage over time with
--no-streamin a loop - Detecting CPU spikes: correlated with recent deployments
- Verifying resource limits: confirming
--memoryand--cpusare applied correctly - Capacity planning: figuring out how many containers fit in the system’s resources
- Debugging performance regressions: comparing before/after a change
while true; do container stats --no-stream --format json my-app | \ python3 -c "import sys,json; d=json.load(sys.stdin)[0]; print(d['name'], d['memory_usage'])" sleep 5doneSummary
In this post, I showed how to monitor container resource usage with container stats. The key point is that it supports interactive live monitoring, single snapshots, and JSON output for automation — covering ad-hoc debugging and scripted monitoring.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments