Docker Stats

Docker Stats

I just added a new container to a customer project. Out of curiosity, I wanted to know how many resources the new container was using in the project. More specifically, I wanted to know how much RAM it consumed. That is when I stumbled across the useful docker stats command.

docker stats

It behaves much like the familiar top tool. The display updates every few seconds. You can also restrict the display to individual containers.

If you simply run the tool, it shows the currently running containers. Sometimes, however, you want information about all containers. In that case, you need to add the -a option.

For scripts, the --format option may be useful, as it lets you pass in a Go template.

The following variables can be used:

.Container Container Name or ID
.Name Container name
.ID Container ID
.CPUPerc CPU percentage
.MemUsage Memory usage
.NetIO Network IO
.BlockIO Block IO
.MemPerc Memory percentage (Not available on Windows)
.PIDs Number of PIDs (Not available on Windows)

https://docs.docker.com/engine/reference/commandline/stats/

If you want to see a nicely formatted table with the names and CPU usage of the running containers, simply run the following:

docker stats --format "table \\t "