Ant*_*n.P 7 linux ubuntu mount hosts docker
我运行一个简单的 docker 容器 sudo docker run -it ubuntu:latest /bin/bash
当我检查已安装的文件系统时,使用: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
....
Run Code Online (Sandbox Code Playgroud)
我无法理解最后一行,即/dev/sda1 -> /etc/hosts当我df -h在主机上运行时,我得到了 mount /dev/sda1 -> /。
所以/dev/sda1实际上是我的硬盘驱动器,为什么安装/etc/hosts在容器上,以及如何来/etc/hosts在容器上是正确的内容的文件。
对这里发生的事情有什么解释吗?这是如何运作的?
您的帖子中有两个问题,让我依次介绍它们。
1)为什么/etc/{hosts,hostname,resolv.conf}文件是从外部挂载的?
我至少看到了一个原因。
想象一下,如果容器引擎只是将这些文件写入容器的文件系统,并且用户决定将其挂载/etc为一个卷(这是完全合法且非常有用的 - 挂载/etc将允许用户为容器提供多个配置文件)会发生什么) 的一个-v论据docker run:
/etc目录;/etc)。启动此容器后,用户尝试使用相同的/etc卷再启动一个(同样,这是完全合法且有用的 - 例如,用户扩展某些服务并/etc在实例之间共享配置文件),并且...第二个容器覆盖hostname,hosts和resolv.conf卷上的文件,影响第一个容器。
现在考虑使用绑定安装而不是直接写入时会发生什么:
/etc目录;/etc/{hosts,hostname,resolv.conf}从主机上的某个地方绑定挂载到容器的文件系统;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]
Run Code Online (Sandbox Code Playgroud)
实际上,这里的每一行输出都显示了三个字段( mount target /etc/hosts、 mount source/dev/sda1和 FS root /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".
Run Code Online (Sandbox Code Playgroud)
对于大多数挂载,FS root 是/(因为您挂载了整个文件系统),因此在查看df(1)输出时不会丢失太多信息。但是,对于特定文件的绑定安装,情况并非如此。
| 归档时间: |
|
| 查看次数: |
3374 次 |
| 最近记录: |