Memory usage of Docker containers
Asked Answered
C

5

78

I am using Docker to run some containerized apps. I am interested in measuring how much resources they consume (as far as regarding CPU and Memory usage).

Is there any way to measure the resources consumed by Docker containers like RAM & CPU usage?

Thank you.

Cythera answered 23/9, 2013 at 9:38 Comment(1)
for line in docker ps | awk '{print $1}' | grep -v CONTAINER; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes / 1024 / 1024 ))MB ; doneVagabond
B
121

You can get this from docker stats e.g:

$ docker stats --no-stream
CONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O             PIDS
6b5c0fcfa7d4        0.13%               2.203 MiB / 4 MiB   55.08%              5.223 kB / 648 B    102.4 kB / 876.5 kB   3
Burkhart answered 10/1, 2017 at 20:50 Comment(2)
is there any way to measure max resource used by container at any particular time during its complete lifecycle?Eleazar
@Eleazar No easy way (at least that I know of). I think you'd have to use some monitoring solution e.g. prometheus and take samples.Burkhart
M
17

Update: See @Adrian Mouat's answer below as docker now supports docker stats!

There isn't a way to do this that's built into docker in the current version. Future versions will support this via an api or plugin.

It does look like there's an lxc project that you should be able to use to track CPU and Memory.

Mumford answered 24/9, 2013 at 10:33 Comment(1)
You can do this now with docker stats. I've added an answer, but you might want to update this one as well as it's the accepted one.Burkhart
C
6

Also, you can read resource metrics directly from cgroups. See example below (I am running on Debian Jessie and docker 1.2)

> docker ps -q
afa03c363af5
> ls /sys/fs/cgroup/memory/system.slice/ | grep docker-afa03c363af5
docker-afa03c363af54815d721d938e01fe4cb2debc4f6c15ebff1851e20f6cde3ae0e.scope
> cd docker-afa03c363af54815d721d938e01fe4cb2debc4f6c15ebff1851e20f6cde3ae0e.scope
> cat memory.usage_in_bytes
4358144
> cat memory.limit_in_bytes
1073741824
Chaldean answered 14/9, 2014 at 10:38 Comment(2)
I'm on Ubuntu, a couple of years after this, and I don't have a subfolder called system.slice - has it changed its name? There is now a docker subfolder at this level...Subspecies
How about for Mac users?Pharynx
E
6

Kindly check out below commands for getting CPU and Memory usages of docker containers:-

docker stats container_ID #to check single container resources

enter image description here

for i in $(docker ps -q); do docker stats $i --no-trunc --no-stream ; echo "--------";done #to check/list all container resources enter image description here

docker stats --all #to check all container resources live enter image description here

docker system df -v #to check storage related information enter image description here

Euraeurasia answered 14/11, 2022 at 16:31 Comment(2)
Please prefer text, not images/screenshotsEpps
please solve mistake in "docker status container_ID" -> "docker stats container_ID"Fob
M
3

This is the command I use:

 docker stats CONTAINER_ID --no-stream --format "{{.Container}}: {{.MemUsage}}"

replace the CONTAINER_ID with the container id you find by using this command:

docker container ps
Marilyn answered 4/2, 2024 at 10:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.