how to find MAX memory from docker stats?

后端 未结 4 1244
庸人自扰
庸人自扰 2021-02-18 15:26

With docker stats you can see the memory usage of a container over time.

Is there a way to find what the highest value of memory usage was while running

4条回答
  •  长情又很酷
    2021-02-18 15:57

    I took a sampling script from here and aggregated data by @pl_rock. But be careful - the sort command only compares string values - so the results are usually wrong (but ok for me). Also mind that docker is sometimes reporting wrong numbers (ie. more allocated mem than physical RAM).

    Here is the script:

    #!/bin/bash
    
    "$@" & # Run the given command line in the background.
    pid=$!
    
    echo "" > stats
    
    while true; do
      sleep 1
      sample="$(ps -o rss= $pid 2> /dev/null)" || break
    
      docker stats --no-stream --format "{{.MemUsage}} {{.Name}} {{.Container}}" | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 }' >> stats
    done
    
    for containerid in `awk '/.+/ { print $7 }' stats | sort | uniq`
    do
        grep "$containerid" stats | sort -r -k3 | tail -n 1
        # maybe: | sort -r -k3 -h | head -n 1
        # see comment below (didnt tested)
    done
    

提交回复
热议问题