Sort by memory usage in docker stats
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
docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.MemUsage}}" | sort -k 3 -h
Commands sort only by memory
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;';
}