Docker - Access host /proc

2019-07-20 03:07发布

问题:

This is a duplicate of a post i have made in the docker forum. Thus iam going to close this / the other one once this problem is solved. But since no one answers in the docker forum and my problems persists, ill post it again looking forward to get an answer.

I would like to expose a server monitoring app as a docker container. The app i have written relies on /proc to read system information like CPU utilization or disk stats. Thus i have to forward the information provided in hosts /proc virtual file system to my docker container.

So i made a simple image (using the first or second intro on docker website: Link) and started it:

docker run -v=/proc:/host/proc:ro -d hostfiletest

docker run -v=/proc:/host/proc:ro -d hostfiletest Assuming the running container could read from /host/proc to obtain information about the host system.

I fired up a console inside the container to check:

docker exec -it {one of the funny names the container get} bash

And checked the content of /host/proc.

Easiest way to check it was getting the content of /host/proc/sys/kernel/hostname - that should yield the hostname of the vm iam working on. But i get the hostname of the container, while /host/proc/uptime gets me the correct uptime of the vm.

Do i miss something here? Maybe something conceptual?

Docker version 17.05.0-ce, build 89658be running on Linux 4.4.0-97-generic (VM)

Update:

I found several articles describing how to run a specific monitoring app inside a containing using the same approach i mentioned above.

Update:

Just tried using an existing Ubuntu image - same behavior. Running the image privileged and with pid=host doesn't help.

Greetings Peepe

回答1:

I know its a few months later no but I came across the same problem today.

In my case I was using psutil in Python to read disk stats of the hosts from inside a docker container.

The solution was to mount the whole host files system as read only into the docker container with -v /:/rootfs:ro and specify the path to proc as psutil.PROCFS_PATH = '/rootfs/proc'.

Now the psutil.disk_partitions() lists all partitions from the host files system. As the hostname is also contained within the proc hierarchy, I guess this also works for other host system information as long the the retrieving command points to /rootsfs/proc.