61

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.

Paris
  • 6,323
  • 7
  • 31
  • 49
  • 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 ; done – staticx Nov 19 '15 at 15:17

4 Answers4

90

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
Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • 3
    is there any way to measure max resource used by container at any particular time during its complete lifecycle? – Khatri Sep 02 '18 at 18:10
  • 1
    @Khatri 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. – Adrian Mouat Sep 04 '18 at 18:22
16

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.

johncosta
  • 3,737
  • 1
  • 25
  • 25
  • 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. – Adrian Mouat Jan 16 '17 at 16:15
5

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
Viacheslav Kovalev
  • 1,745
  • 12
  • 17
  • 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... – halfer Oct 04 '16 at 23:12
  • How about for Mac users? – Joe Mar 29 '18 at 03:37
3

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

docker status 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

linux.cnf
  • 519
  • 6
  • 7
  • Please [prefer text, not images/screenshots](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – ggorlen Apr 22 '23 at 22:49