Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining Linux host metrics from within a docker container

Looking for some recommendations for how to report linux host metrics such as cpu and memory utilization and disk usage stats from within a docker container. The host will contain a number of docker containers. One thought was to run Top and other basic linux commands from the outside the container and push them into a container folder that has the appropriate authorization so that they can be consumed. Another thought was to use the docker api to run docker stats for the containers but not sure this is the best as it may not report on other processes running on the host that are not containerized. A third option would be to somehow execute something like TOP and other commands on the host from within the container, this option being the most ideal for my situation. I was just looking for some proven design patterns that others have used. Also, I don’t have the ability to install a bunch of tools on the host as this would be a customer host which I don’t have control as to what is already installed.

like image 397
jim stone Avatar asked Nov 24 '25 11:11

jim stone


1 Answers

You may run your container in privileged mode, but be aware that it this could compromise the host security as your container will no longer be in a sandboxed environment.

docker run -d --privileged --pid=host alpine:3.8 sh

When the operator executes docker run --privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor or SELinux to allow the container nearly all the same access to the host as processes running outside containers on the host. Additional information about running with --privileged is available on the Docker Blog.

https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities

Good reference: https://security.stackexchange.com/a/218379

like image 140
Rakesh Gupta Avatar answered Nov 26 '25 17:11

Rakesh Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!