Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute the total memory used by Docker containers in Bash

Tags:

bash

docker

sh

How to compute in one Bash command line the total memory used by Docker containers running on the local Docker engine?

like image 810
Nicolas Henneaux Avatar asked Dec 14 '17 16:12

Nicolas Henneaux


People also ask

How do I check the memory of a docker container?

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.

How much memory does a docker container use?

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.

How do I find out my containers memory limit?

The memory limit is enforced via cgroups. Therefore you need to use cgget to find out the memory limit of the given cgroup.

How can I check CPU usage in docker?

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.


1 Answers

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"}'
like image 135
Nicolas Henneaux Avatar answered Sep 22 '22 02:09

Nicolas Henneaux