Docker-访问主机/ proc [英] Docker - Access host /proc

查看:520
本文介绍了Docker-访问主机/ proc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在其中创建的帖子的副本。码头工人论坛。因此,一旦解决此问题,我将关闭此/另一个。但是,由于在docker论坛中没有人回答并且我的问题仍然存在,因此我将其再次发布,希望得到答案。

This is a duplicate of a post I have created in the docker forum. Thus I am going to close this / the other one once this problem is solved. But since no one answers in the docker forum and my problem persists, I'll post it again, looking forward to get an answer.

我想公开一台服务器监视应用程序作为Docker容器。我编写的应用程序依靠/ proc读取系统信息,例如CPU利用率或磁盘统计信息。因此,我必须将hosts / proc虚拟文件系统中提供的信息转发到我的docker容器。

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.

因此,我做了一个简单的镜像(使用docker的第一个或第二个简介)网站:链接)并启动它:

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

假定正在运行的容器可以从/ host / proc中读取以获取有关主机系统的信息。

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

并检查/ host / proc的内容。

And checked the content of /host/proc.

检查它的最简单方法是获取/ host / proc / sys / kernel / hostname的内容-这应该产生我正在工作的vm的主机名。
但是我得到了容器的主机名,而/ host / proc / uptime获得了正确的虚拟机正常运行时间。

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

我在这里错过了吗?

Docker版本17.05.0-ce,内部版本89658be运行在Linux 4.4.0-97-generic(VM)

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

更新:

我发现了几篇文章,描述了如何使用与上述相同的方法在容器中运行特定的监视应用程序。

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

更新:

只是尝试使用现有的Ubuntu映像-相同的行为。使用pid = host特权运行图像无济于事。

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

问候
偷看

推荐答案

原因问题是 / proc 不是正常的文件系统。根据 procfs ,它就像一个访问某些内核数据和系统信息的接口。该接口提供了类似文件的结构,因此可能使人们误解它是普通目录。 / proc 中的
文件也不是普通文件。它们为空(大小= 0)。您可以自己检查。

The reason of this problem is that /proc is not a normal filesystem. According to procfs, it is like an interface to access some kernel data and system information. This interface provides a file-like structure, so it can make people misunderstand that it is a normal directory. Files in /proc are also not normal files. They are empty (size = 0). You can check by yourself.

$ stat /proc/sys/kernel/hostname 
File: /proc/sys/kernel/hostname
Size: 0             Blocks: 0          IO Block: 1024   regular empty file

因此文件不保留任何数据,但是当您读取文件时,内核将动态地向您返回相应的系统信息。

So the file doesn't hold any data, but when you read the file, the kernel will dynamically return to you a corresponding system information.

要回答您的问题, / proc / sys / kernel / hostname 只是访问主机名的接口。根据访问该接口的位置,主机还是容器,您将获得相应的主机名。当您使用绑定安装 -v / proc:/ hosts / proc:ro 时也会应用此方法,因为绑定安装将提供 /的替代视图proc 。如果调用接口 / hosts / proc / sys / kernel / hostname ,则内核将返回您所在容器(容器)的主机名。

To answer your question, /proc/sys/kernel/hostname is just an interface to access the hostname. And depending on where you access that interface, on the host or on the container, you will get the corresponding hostname. This is also applied when you use bind mount -v /proc:/hosts/proc:ro, since bind mount will provide an alternative view of /proc. If you call the interface /hosts/proc/sys/kernel/hostname, the kernel will return the hostname of the box where you are in (the container).

简而言之,如果您的主机位于,请考虑将 / proc / sys / kernel / hostname 作为镜像在它的前面,它将反映主机。如果是容器,它将反映容器。

In short, think about/proc/sys/kernel/hostname as a mirror, if your host stands in front of it, it will reflect the host. If it is the container, it will reflect the container.

这篇关于Docker-访问主机/ proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆