A Lazy Way to Collect Runtime Stats About Running Docker Containers
2020 July 23

So I was recently messing with Docker to keep up on my skills, and I wanted to see how much resources were required by each container over a given time period. So I came up with the following command to run in the terminal to collect it.

docker stats -a --no-stream | head -n 1 | tr -d / > $FILE
while true
    docker stats -a --no-stream | \
        tail -n+2 | \
done | tr -d / >> $FILE

This command will produce an output similar to the following (details removed):

CONTAINER ID        NAME                         CPU %               MEM USAGE  LIMIT     MEM %               NET IO             BLOCK IO           PIDS
Jul 23 19:28:43 ece662a6f8f5        container_xxxx_0 0.00%               2.758MiB  15.26GiB   0.02%               10.3kB  0B         0B  0B             2
Jul 23 19:28:43 dfb14d35d485        container_xxxx_1 0.02%               79.5MiB  15.26GiB    0.51%               12.2kB  1.44kB     0B  0B             5

Important parts to note include the ts command this prepends the time stamp to every linen of output, it’s from the moreutils package. An alternate option would be to use xargs and date to add the time stamp, but every line but this is cleaner and faster cause otherwise we would be starting a lot more processes. The tr command is used to remove the extra slashes as we don’t need them. The head and tail commands are used to capture only the parts of the output we’re interested in like the headers or without the headers.

Things that could be improved are the formatting of the columns to be truly fixed width, and adding a date header, but it’s good for very quick analysis in whatever your favorite tool is. Mine just happens to currently be Excel for the power of pivot tables.

Remember you can also subscribe using RSS at the top of the page!

Share this on → Mastodon Twitter LinkedIn Reddit

A selected list of related posts that you might enjoy:

Written by Henry J Schmale on 2020 July 23
Hit Counter