How to compute in one Bash
command line the total memory used by Docker
containers running on the local Docker
engine?
If you need more detailed information about a container's resource usage, use the /containers/(id)/stats API endpoint. On Linux, the Docker CLI reports memory usage by subtracting cache usage from the total memory usage.
The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 6m (6 megabytes). That is, you must set the value to at least 6 megabytes.
The memory limit is enforced via cgroups. Therefore you need to use cgget to find out the memory limit of the given cgroup.
We can get CPU usage of docker container with docker stats command. The column CPU % will give the percentage of the host's CPU the container is using.
I use the following command to compute the total memory used in MB.
docker stats --no-stream --format 'table {{.MemUsage}}' | sed 's/[A-Za-z]*//g' | awk '{sum += $1} END {print sum "MB"}'
or if any are larger than 1GiB
docker stats --no-stream --format 'table {{.MemUsage}}' | sed 's/\.\([0-9]*\)GiB/\1MiB/g' | sed 's/[A-Za-z]*//g' | awk '{sum += $1} END {print sum "MB"}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With