HomeView summary of all Docker containers memory usage

View summary of all Docker containers memory usage

Published: 5/23/2023

You can get pretty much all resource usage information with docker stats, but what you will lack there is the summary.

However, with some command line magic you can get the summary. You can put this in a .sh script, and make it executable with chmod +x docker-memory.sh:

mem_amount_total_with_unit=$(docker system info \
| grep 'Total Memory: ' \
| tr -d 'Total Memory: ')

unit=$(echo ${mem_amount_total_with_unit} \
| sed 's/[0-9\.]*//g')

mem_amount_total=$(echo ${mem_amount_total_with_unit} \
| sed 's/[^0-9\.]*//g')

mem_percent_used=$(docker stats --no-stream --format '{{.MemPerc}}' \
| tr -d '%' \
| paste -s -d '+' - \
| bc)

mem_percent_used=${mem_percent_used:-0}

mem_amount_used=$(echo "scale=2; ${mem_amount_total} * ${mem_percent_used} / 100" \
| bc)

echo "Memory Amount Total: ${mem_amount_total}${unit}"
echo "Memory Amount Used: ${mem_amount_used}${unit}"
echo "Memory Percent Used: ${mem_percent_used}%"

After running the script ./docker-memory.sh, you will get something like this:

Memory Amount Total: 7.639GiB
Memory Amount Used: 5.55GiB
Memory Percent Used: 72.75%

On some systems, bc is not present. You might need to install it with yum install bc or apt-get install bc or similar (depending on your system OS).

About Code with Node.js

This is a personal blog and reference point of a Node.js developer.

I write and explain how different Node and JavaScript aspects work, as well as research popular and cool packages, and of course fail time to time.