I'm making a system-performance-monitor-tool, which needs cpu-usage, memory-usage, disk-usage and currnet-processes-info.
I already/kinda finished it out of docker, but the installation and uninstallation is too complicated, so I'm trying to finish it in docker.
I have already mount /proc/stat and /proc/meminfo in container so I can directly read the host's cpu-usage and memory-usage now.
Now I have trouble getting disk-usage and currnet processes. Normal in host I will run df and ps auxf to get them, but it can't be done in a container. I read the document of procps but still confused.
(Also I knew how but I really don't want to use remote-command with ssh because I just don't want to do key-based authentication. Please don't recommend this way).
Is there a way like mount something into container that can make it possible to read host's "processes info" and "disk space usage info" in a docker container?
Is there a way like mount something into container that can make it possible to read host's "processes info" and "disk space usage info" in a docker container?
Sure. You can mount the host's root filesystem and the /proc filesystem into the container like this:
docker run -v /proc:/host/proc -v /:/host/root ...
Then inside the container you can discover host process information by inspecting the /host/proc directory, and you can see filesystem utilization by running df /host/root.
This exact technique is used when running node-exporter inside a container; this document shows a relatively common configuration.
Note that you won't be able to use ps in this case, but you can read entries from /host/proc directly.
Alternately, you can run your container in the host pid namespace:
docker run --pid=host -v /:/host/root ...
This allows you to run ps in the container to see host processes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With