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.

https://hackernoon.com/docker-containerd-standalone-runtimes-heres-what-you-should-know-b834ef155426

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.

Image for post
Image for post
ctop grafana and top and pstree view

Techlogs

Programming, Software and Related

Alex Punnen

Written by

SW Architect/programmer /designer - in various languages and technologies from 2001 to now. That’s the story so far…

Techlogs

Techlogs

Programming, Software and Related

Alex Punnen

Written by

SW Architect/programmer /designer - in various languages and technologies from 2001 to now. That’s the story so far…

Techlogs

Techlogs

Programming, Software and Related

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store