我用sudo docker run -it ubuntu:latest /bin/bash
运行了一个简单的docker容器
当我使用df -h
检查已挂载的文件系统时,
Filesystem Size Used Avail Use% Mounted on
overlay 63G 4.3G 56G 8% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1000M 0 1000M 0% /sys/fs/cgroup
/dev/sda1 63G 4.3G 56G 8% /etc/hosts
....
我无法理解最后一行,即/dev/sda1 -> /etc/hosts
,当我在主机上运行df -h
时,我得到了挂载/dev/sda1 -> /
。
所以/dev/sda1
实际上是我的硬盘,为什么它安装在容器上的/etc/hosts
上,为什么容器上的/etc/hosts
是一个包含正确内容的文件。
对这里发生的事情有什么解释吗?这是怎么回事?
发布于 2019-06-14 03:14:34
您的帖子中有两个问题,让我按顺序进行讨论。
1)为什么从外部挂载 /etc/{hosts,hostname,resolv.conf}
文件?
我至少看到了一个原因。
想象一下,如果容器引擎简单地将这些文件写入容器的文件系统和,用户将决定将/etc
挂载为卷(这是完全合法且非常有用的-挂载/etc
将允许用户在docker run
的一个-v
参数中为容器提供多个配置文件):
/etc
文件中,其内容由容器引擎更改(写入/etc
).中的特定文件
在启动这个容器之后,用户尝试用相同的/etc
卷启动另一个容器(同样,这是完全合法和有用的--例如,用户扩展一些服务并在实例之间共享/etc
中的配置文件),然后...第二个容器覆盖卷上的hostname
、hosts
和resolv.conf
文件,影响第一个容器。
现在考虑当使用绑定挂载而不是直接写入时会发生什么:
directory;
/etc/{hosts,hostname,resolv.conf}
从主机上的某个位置绑定装载到容器的filesystem;2)为什么我将 /dev/sda1
视为这些装载的来源?
检查findmnt(8)
而不是df(1)
$ docker run -it ubuntu
root@5a8ab4d6e716:/# findmnt
TARGET SOURCE
...
|-/etc/resolv.conf /dev/sda1[/var/lib/docker/containers/5a8ab4d6e71691f279cbbcf5a295b5fa90fd138f10418c996ad7ea4440452816/resolv.conf]
|-/etc/hostname /dev/sda1[/var/lib/docker/containers/5a8ab4d6e71691f279cbbcf5a295b5fa90fd138f10418c996ad7ea4440452816/hostname]
`-/etc/hosts /dev/sda1[/var/lib/docker/containers/5a8ab4d6e71691f279cbbcf5a295b5fa90fd138f10418c996ad7ea4440452816/hosts]
实际上,这里的每一行输出都显示了三个字段(挂载目标/etc/hosts
、挂载源/dev/sda1
和FS根/var/lib/<...>/hosts
),而df(1)
没有显示第三个字段。
根据man procfs
关于/proc/PID/mountinfo
文件的一段话(它是实用程序挂载信息的来源):
(4) root: the pathname of the directory in the filesystem which forms the root of this mount.
(5) mount point: the pathname of the mount point relative to the process's root directory.
...
(10) mount source: filesystem-specific information or "none".
对于大多数挂载,FS根目录是/
(因为您挂载的是整个文件系统),因此在查看df(1)
输出时不会丢失太多信息。但是,对于特定文件的绑定挂载,情况并非如此。
https://stackoverflow.com/questions/56578998
复制相似问题