Sort by memory usage in docker stats

前端 未结 4 1865
庸人自扰
庸人自扰 2020-12-31 02:12

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

相关标签:
4条回答
  • 2020-12-31 02:46

    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

    0 讨论(0)
  • 2020-12-31 02:51

    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;'; 
    }
    
    0 讨论(0)
  • 2020-12-31 02:59
    docker stats --no-stream --format "table {{.Name}}\t{{.Container}}\t{{.MemUsage}}" | sort -k 3 -h 
    

    Commands sort only by memory

    0 讨论(0)
  • 2020-12-31 03:02

    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
    
    0 讨论(0)
提交回复
热议问题