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
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{{.MemPerc}}" | 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
Building off of the previous answers I created the following function and put in in my alias file.
Every second it captures the dockers stats and then generates 4 tables sorted descending by CPU %, MEM USAGE, NET I/O, and BLOCK I/O.
Also, this does maintain the table headers :-)
function dks() {
watch -n 1 'STATS=$(docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"); echo "$STATS" | (read -r; printf "%s\n" "$REPLY"; sort -k3hr) | head; echo; echo "$STATS" | (read -r; printf "%s\n" "$REPLY"; sort -k4hr) | head; echo; echo "$STATS" | (read -r; printf "%s\n" "$REPLY"; sort -k7hr) | head; echo; echo "$STATS" | (read -r; printf "%s\n" "$REPLY"; sort -k10hr) | head;';
}
docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.MemUsage}}" | sort -k 3 -h
Commands sort only by memory
Make sure your locale's decimal seperator is a DOT. If it is different sorting won't work.
That's why I use this command (Linux), note the LC_ALL=en_US.utf8:
docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" | LC_ALL=en_US.utf8 sort -k 4 -h