Container Stats
We know how to inspect a container’s logs, but what if we want to see its resource utilization?
It’s common to spin up some Docker containers, forget about them, and then wonder why your host machine has gotten really slow. It’s crucial to see how much RAM and CPU each container is using, especially in production environments.
The docker stats command gives you a live data stream of resource usage for running containers.
docker stats [OPTIONS] [CONTAINER...]
Testing Resource Utilization
We can use the stress-ng image to artificially create containers that use a lot of resources.
-
Start a container that uses a full CPU core (with a timeout of 10m so it doesn’t run forever):
docker run -d --name cpu-stress alexeiled/stress-ng --cpu 2 --timeout 10m -
Start another container that allocates 1GB of memory:
docker run -d --name mem-stress alexeiled/stress-ng --vm 1 --vm-bytes 1G --timeout 10m -
Run
docker statsto see the live resource usage:docker stats
You will see a live updating table showing CPU %, MEM USAGE / LIMIT, NET I/O, and BLOCK I/O for each running container.
(Press Ctrl+C to exit the stats view)