0

这不是常见问题解答:“如何挂载主机目录,使其在容器中可见”,答案是docker run -v /host/path:/container/path.

相反,专用容器可以在挂载点挂载一些有趣的数据。如何使该挂载点内的数据在主机中可见?

例如,当运行gluster/gluster-centos glusterfs映像时,容器具有显示和挂载 glusterfs 数据的功能。我可以让这些数据在主机中可见,这样我就不必在我的主机中安装 glusterfs 代码了吗?

我试图docker run -v /host/path:/container/path然后,从容器内部:

mkdir /container/path/mountpoint
mount -t glusterfs server:volume /container/path/mountpoint

在容器中,我可以看到 glusterfs 卷中的数据/container/path/mountpoint,但在主机上,/host/path/mountpoint存在但为空。如何将容器中的数据公开/container/path/mountpoint给主机?

编辑:我发现我可以看到挂载点内的数据:

containerPid=$(docker inspect -f '{{.State.Pid}}' containername)
sudo ls -l /proc/$containerPid/root/container/path/mountpoint

但我希望有更优雅的东西......(并且不需要root来查看数据)

4

1 回答 1

0

您正在寻找shared挂载标志(有关更多信息,请阅读绑定挂载。以下内容应该可以满足您的要求:

docker run -v /host/path:/container/path:shared ...

例如,如果我在我的主机上运行这个(我需要--privileged才能mount运行):

# mkdir /tmp/foo
# docker run -it --rm --privileged -v /tmp/foo:/foo:shared alpine sh

然后在容器内:

container# mkdir /foo/mnt
container# mount -o bind /etc /foo/mnt

在主机上,我看到:

# ls /tmp/foo
alpine-release  group     issue           motd        passwd     resolv.conf  ssl
apk             hostname  logrotate.d     mtab        periodic   securetty    sysctl.conf
conf.d          hosts     modprobe.d      network     profile    services     sysctl.d
crontabs        init.d    modules         opt         profile.d  shadow       udhcpd.conf
fstab           inittab   modules-load.d  os-release  protocols  shells

我认为这正是您所问的问题。

于 2020-09-10T02:19:41.343 回答