Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort by memory usage in docker stats

Tags:

Is there a way to display the docker stats sorted by memory usage of the containers?

I am using the following command to display the container with their names and I want to sort the result by memory usage.

docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" 

The unsorted result is the following.

NAME                                                                   CONTAINER           CPU %               MEM USAGE / LIMIT kafka3.interactive.8a38c338742464ffb04d6f23fc6485391318d103            0d68b7fd49a0        1.39%               359.5 MiB / 4.833 GiB kafka2.interactive.8a38c338742464ffb04d6f23fc6485391318d103            7e5541b0b883        1.22%               309.4 MiB / 4.833 GiB kafka1.interactive.8a38c338742464ffb04d6f23fc6485391318d103            dff07c6d639c        0.68%               267.4 MiB / 4.833 GiB service2.interactive.8a38c338742464ffb04d6f23fc6485391318d103          0f20a7e75823        0.06%               617.8 MiB / 4.833 GiB consulakms.interactive.8a38c338742464ffb04d6f23fc6485391318d103        b5972262194d        3.82%               10.32 MiB / 4.833 GiB service1.interactive.8a38c338742464ffb04d6f23fc6485391318d103          be56185a37bf        0.09%               596.3 MiB / 4.833 GiB consumer1.interactive.8a38c338742464ffb04d6f23fc6485391318d103         05145beb209c        0.06%               574.6 MiB / 4.833 GiB consul1.interactive.8a38c338742464ffb04d6f23fc6485391318d103           3298a8159064        0.67%               10.57 MiB / 4.833 GiB consul3.interactive.8a38c338742464ffb04d6f23fc6485391318d103           4a1bbbd131ad        3.12%               9.664 MiB / 4.833 GiB zookeeper2.interactive.8a38c338742464ffb04d6f23fc6485391318d103        040f00b4bbc7        0.09%               42.45 MiB / 4.833 GiB consulbootstrap.interactive.8a38c338742464ffb04d6f23fc6485391318d103   45268a11f2f4        3.62%               11.46 MiB / 4.833 GiB zookeeper3.interactive.8a38c338742464ffb04d6f23fc6485391318d103        331772b27079        0.12%               51.27 MiB / 4.833 GiB consul2.interactive.8a38c338742464ffb04d6f23fc6485391318d103           77b63171e6b5        1.07%               12.59 MiB / 4.833 GiB zookeeper1.interactive.8a38c338742464ffb04d6f23fc6485391318d103        c5ad82730598        0.08%               43.17 MiB / 4.833 GiB service3.interactive.8a38c338742464ffb04d6f23fc6485391318d103          610da86c6949        3.79%               546.7 MiB / 4.833 GiB squid.interactive.8a38c338742464ffb04d6f23fc6485391318d103             928ddbb197fa        0.01%               144.2 MiB / 4.833 GiB 
like image 323
Nicolas Henneaux Avatar asked May 12 '17 05:05

Nicolas Henneaux


People also ask

How does Docker stats calculate memory usage?

On Linux, the Docker CLI reports memory usage by subtracting cache usage from the total memory usage. The API does not perform such a calculation but rather provides the total memory usage and the amount from the cache so that clients can use the data as needed.

How much memory is a docker container using?

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 get Docker container metrics?

You can use the docker stats command to live stream a container's runtime metrics. The command supports CPU, memory usage, memory limit, and network IO metrics. The docker stats reference page has more details about the docker stats command.


2 Answers

To sort by Mem Usage field you can use the following command:

GNU/Linux:

docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" | sort -k 4 -h

MacOS:

docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.M‌​emPerc}}" | sort -k 9 -n

Check this link to view all available options to --format option of docker stats: https://docs.docker.com/engine/reference/commandline/stats/#formatting

like image 179
kstromeiraos Avatar answered Oct 17 '22 05:10

kstromeiraos


docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.MemUsage}}" | sort -k 3 -h  

Commands sort only by memory

like image 23
Rengarajan G Avatar answered Oct 17 '22 03:10

Rengarajan G