Docker — How to check your Containers CPU Usage

Alex Punnen
Mar 7, 2018 · 2 min read

I have used ctop which gives a more graphical way than docker_stats. But I found that it was showing CPU percentage way higher than what top was showing for the system. Basically it is showing relative the percentage to the root process. Docker containers run as child process

To illustrate with an example

1.First find the root process under which all the containers run this is called docker-containerd-shim

The Docker architecture is broken into four components: Docker engine, containerd, containerd-shm and runC. The binaries are respectively called docker, docker-containerd, docker-containerd-shim, and docker-runc.

root 1843 1918 0 Aug31 ? 00:00:00 docker-containerd-shim 611bd9... /var/run/docker/libcontainerd/611bd92.... docker-runc

2.You can see all the containers that are running using the command

pstree -p 1918

Now say that we are interested in seeing the CPU consumption of say fluentdb.Easy way to get the child pid of this is

pstree -p 1918 |grep fluentd

Which gives 21670

Now you can run top -p 21670 to see the CPU share of this child process also top -p 1918 to see the overall CPU of the parent process.

With cadvisor collecting to Prometheus and view in Grafana, this was the closest and most accurate representation of the actual CPU percentage used by the container; in relation to the host machine.

This diagram illustrates this. ctop and docker stats give 23% as the CPU percentage. Actual CPU percentage of the docker parent process is around 2% and cadvisor output from Grafana shows the most ‘accurate’ value of the container CPU percentage related to host.

ctop grafana and top and pstree view