how to find MAX memory from docker stats?

后端 未结 4 1264
庸人自扰
庸人自扰 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:45

    In my case I wanted to monitor a docker container which runs tests for my web application. The test suite is pretty big, it includes javascript tests in a real browser and consume significant amount of both, memory and time.

    Ideally, I wanted to watch the current memory usage real time, but to also keep the history for later analysis.

    I ended up using a modified and simplified version of the Keiran's solution:

    CONTAINER=$(docker ps -q -f name=CONTAINER_NAME)
    FORMAT='{{.MemPerc}}\t{{.MemUsage}}\t{{.Name}}'
    
    docker stats --format $FORMAT $CONTAINER | sed -u 's/\x1b\[[0-9;]*[a-zA-Z]//g' | tee stats
    

    Notes:

    • CONTAINER=$(docker ps -q -f name=NAME) # find container by name, but there are other options
    • FORMAT='{{.MemPerc}} ...}} # MemPerc goes first (for sorting); otherwise you can be creative
    • sed -u # the -u flag is important, it turns off buffering
    • | sed -u 's/\x1b\[[0-9;]*[a-zA-Z]//g' # removes ANSI escape sequences
    • | tee stats # not only show real time, but also write into the stats file
    • I Ctrl-C manually when it's ready – not ideal, but OK for me
    • after that it's easy to find the max with something like sort -n stats | tail

提交回复
热议问题