docker

Sort by memory usage in docker stats


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

Solution

  • 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