1

I have created a new docker services and am determining its required resources. Since applying the RAM to a new service is greedy--saying the container can have 8GB of RAM it will get them--I don't want to waste the cluster's resources.

Now I am trying to find out how much RAM a docker run took at its peak.

For example, I created a httpie-image (for the rightfully paranoid, the Dockerfile is also on dockerhub that I execute via:

docker run -it k0pernikus/httpie-docker-alpine HEAD https://stackoverflow.com/ 

I know that there is a docker stats command, yet it appears to show the current memory usage, and I don't really want to monitor that.

If I run it after the container ended, it will show 0. (To get the container id, I use the d flag.)

$ docker run -itd k0pernikus/httpie-docker-alpine HEAD https://stackoverflow.com/ 132a93ffc9e297250b8ca37b2563aa2b5e423e146890fe3383a91a7f26ef990c $ docker stats 132a93ffc9e297250b8ca37b2563aa2b5e423e146890fe3383a91a7f26ef990c 

it will show:

CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 132a93ffc9e297250b8ca37b2563aa2b5e423e146890fe3383a91a7f26ef990c 0.00% 0 B / 0 B 0.00% 0 B / 0 B 0 B / 0 B 0 

Yet how much RAM did it consume at maximum?

2
  • try docker run -itd... ; docker stats $(docker ps -lq) or docker run -itd --name mycontainer... and in another terminal, you launch as soon as possible docker stats mycontainer Commented Jun 29, 2017 at 12:59
  • Check cadvisor Commented Jun 29, 2017 at 13:41

1 Answer 1

3

tl;dr:

  • Enable memory accounting
  • cat /sys/fs/cgroup/memory/docker/${CONTAINER_ID}/memory.max_usage_in_bytes

Docker uses cgroups under the hood. We only have to ask the kernel the right question, that is to cat the correct file. For this to work memory accounting has to be enabled (as noted in the docs).

On systemd based systems this is quite straight forward. Create a drop-in config for the docker daemon:

systemctl set-property docker.service MemoryAccounting=yes systemctl daemon-reload systemctl restart docker.service 

(This will add a little overhead though as each time the container allocates RAM a counter has to be updated. Further details: https://lwn.net/Articles/606004/)

Then by using the full container id that you can discover via docker inspect:

docker inspect --format="{{.Id}}" $CONTAINER_NAME 

you can get the maximum memory used:

cat /sys/fs/cgroup/memory/docker/${CONTAINER_ID}/memory.max_usage_in_bytes 

The container has to be running for this to work.

Sign up to request clarification or add additional context in comments.

2 Comments

It appears that sudo service docker restart suffices. No reboot required.
Maybe even sudo service docker reload suffices.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.