无法将基于保险丝的卷公开给 Docker 容器
Can't expose a fuse based volume to a Docker container
我正在尝试为我的 docker 容器提供大量加密文件系统供内部使用。
这个想法是容器将像往常一样写入卷,但实际上主机会在将数据写入文件系统之前对其进行加密。
我正在尝试使用 EncFS - 它在主机上运行良好,例如:
encfs /encrypted /visible
我可以将文件写入 /visible,并且这些文件会被加密。
但是,当尝试 运行 一个以 /visible 为体积的容器时,例如:
docker run -i -t --privileged -v /visible:/myvolume imagename bash
我确实在容器中得到了一个卷,但它位于原始 /encrypted
文件夹中,未通过 EncFS。如果我从 /visible
卸载 EncFS,我可以看到容器写入的文件。不用说/encrypted
是空的。
有没有办法让 docker 通过 EncFS 挂载卷,而不是直接写入文件夹?
相反,当我使用 NFS 挂载作为卷时,docker 工作正常。它写入网络设备,而不是我安装设备的本地文件夹。
谢谢
我无法在本地复制您的问题。如果我尝试将 encfs 文件系统公开为 Docker 卷,我会在尝试启动容器时遇到错误:
FATA[0003] Error response from daemon: Cannot start container <cid>:
setup mount namespace stat /visible: permission denied
所以您可能发生了一些不同的事情。无论如何,这就是解决我的问题的方法:
默认情况下,FUSE 只允许安装文件系统的用户访问该文件系统。当您 运行 是一个 Docker 容器时,该容器最初是 运行 作为 root
。
您可以在挂载 FUSE 文件系统时使用 allow_root
或 allow_other
挂载选项。例如:
$ encfs -o allow_root /encrypted /other
这里,allow_root
将允许root用户访问挂载点,而allow_other
将允许任何人访问挂载点(前提是目录的Unix权限允许他们访问)。
如果我使用 allow_root
通过 encfs 文件系统挂载,那么我可以将该文件系统公开为 Docker 卷 并且 该文件系统的内容是正确的从容器内部可见。
这肯定是因为您在主机安装挂载点之前启动了 docker 守护程序。在这种情况下,目录名称的索引节点仍指向主机本地磁盘:
ls -i /mounts/
1048579 s3-data-mnt
然后,如果您使用像 s3fs 这样的保险丝守护进程进行挂载:
/usr/local/bin/s3fs -o rw -o allow_other -o iam_role=ecsInstanceRole /mounts/s3-data-mnt
ls -i
1 s3-data-mnt
我的猜测是 docker 对目录名称进行了一些 bootstrap 缓存到 inode(对此了解更多的人可以填写此空白)。
您的评论是正确的。如果您在安装完成后简单地重新启动 docker,您的卷将正确地从主机共享到您的容器。 (或者你可以简单地延迟启动 docker 直到你的所有坐骑都完成安装)
有趣的是(但现在对我来说是完整的)是在退出容器并卸载主机上的挂载点时,我从容器内到共享卷的所有写入都神奇地出现了(它们正在存储在主机本地磁盘上的 inode 中):
[root@host s3-data-mnt]# echo foo > bar
[root@host s3-data-mnt]# ls /mounts/s3-data-mnt
total 6
1 drwxrwxrwx 1 root root 0 Jan 1 1970 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
[root@host s3-data-mnt]# docker run -ti -v /mounts/s3-data-mnt:/s3-data busybox /bin/bash
root@5592454f9f4d:/mounts/s3-data# ls -als
total 8
4 drwxr-xr-x 3 root root 4096 Sep 16 16:05 .
4 drwxr-xr-x 12 root root 4096 Sep 16 16:45 ..
root@5592454f9f4d:/s3-data# echo baz > beef
root@5592454f9f4d:/s3-data# ls -als
total 9
4 drwxr-xr-x 3 root root 4096 Sep 16 16:05 .
4 drwxr-xr-x 12 root root 4096 Sep 16 16:45 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 beef
root@5592454f9f4d:/s3-data# exit
exit
[root@host s3-data-mnt]# ls /mounts/s3-data-mnt
total 6
1 drwxrwxrwx 1 root root 0 Jan 1 1970 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
[root@host /]# umount -l s3-data-mnt
[root@host /]# ls -als
[root@ip-10-0-3-233 /]# ls -als /s3-stn-jira-data-mnt/
total 8
4 drwxr-xr-x 2 root root 4096 Sep 16 17:28 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
您可以通过将挂载调用包装在 nsenter
中以将其挂载在与 docker 守护程序相同的 Linux 挂载命名空间中来解决此问题,例如。
nsenter -t "$PID_OF_DOCKER_DAEMON" encfs ...
问题是这种方法是否会在守护进程自身重新启动后继续存在。 ;-)
我正在尝试为我的 docker 容器提供大量加密文件系统供内部使用。 这个想法是容器将像往常一样写入卷,但实际上主机会在将数据写入文件系统之前对其进行加密。
我正在尝试使用 EncFS - 它在主机上运行良好,例如:
encfs /encrypted /visible
我可以将文件写入 /visible,并且这些文件会被加密。 但是,当尝试 运行 一个以 /visible 为体积的容器时,例如:
docker run -i -t --privileged -v /visible:/myvolume imagename bash
我确实在容器中得到了一个卷,但它位于原始 /encrypted
文件夹中,未通过 EncFS。如果我从 /visible
卸载 EncFS,我可以看到容器写入的文件。不用说/encrypted
是空的。
有没有办法让 docker 通过 EncFS 挂载卷,而不是直接写入文件夹? 相反,当我使用 NFS 挂载作为卷时,docker 工作正常。它写入网络设备,而不是我安装设备的本地文件夹。
谢谢
我无法在本地复制您的问题。如果我尝试将 encfs 文件系统公开为 Docker 卷,我会在尝试启动容器时遇到错误:
FATA[0003] Error response from daemon: Cannot start container <cid>:
setup mount namespace stat /visible: permission denied
所以您可能发生了一些不同的事情。无论如何,这就是解决我的问题的方法:
默认情况下,FUSE 只允许安装文件系统的用户访问该文件系统。当您 运行 是一个 Docker 容器时,该容器最初是 运行 作为 root
。
您可以在挂载 FUSE 文件系统时使用 allow_root
或 allow_other
挂载选项。例如:
$ encfs -o allow_root /encrypted /other
这里,allow_root
将允许root用户访问挂载点,而allow_other
将允许任何人访问挂载点(前提是目录的Unix权限允许他们访问)。
如果我使用 allow_root
通过 encfs 文件系统挂载,那么我可以将该文件系统公开为 Docker 卷 并且 该文件系统的内容是正确的从容器内部可见。
这肯定是因为您在主机安装挂载点之前启动了 docker 守护程序。在这种情况下,目录名称的索引节点仍指向主机本地磁盘:
ls -i /mounts/
1048579 s3-data-mnt
然后,如果您使用像 s3fs 这样的保险丝守护进程进行挂载:
/usr/local/bin/s3fs -o rw -o allow_other -o iam_role=ecsInstanceRole /mounts/s3-data-mnt
ls -i
1 s3-data-mnt
我的猜测是 docker 对目录名称进行了一些 bootstrap 缓存到 inode(对此了解更多的人可以填写此空白)。
您的评论是正确的。如果您在安装完成后简单地重新启动 docker,您的卷将正确地从主机共享到您的容器。 (或者你可以简单地延迟启动 docker 直到你的所有坐骑都完成安装)
有趣的是(但现在对我来说是完整的)是在退出容器并卸载主机上的挂载点时,我从容器内到共享卷的所有写入都神奇地出现了(它们正在存储在主机本地磁盘上的 inode 中):
[root@host s3-data-mnt]# echo foo > bar
[root@host s3-data-mnt]# ls /mounts/s3-data-mnt
total 6
1 drwxrwxrwx 1 root root 0 Jan 1 1970 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
[root@host s3-data-mnt]# docker run -ti -v /mounts/s3-data-mnt:/s3-data busybox /bin/bash
root@5592454f9f4d:/mounts/s3-data# ls -als
total 8
4 drwxr-xr-x 3 root root 4096 Sep 16 16:05 .
4 drwxr-xr-x 12 root root 4096 Sep 16 16:45 ..
root@5592454f9f4d:/s3-data# echo baz > beef
root@5592454f9f4d:/s3-data# ls -als
total 9
4 drwxr-xr-x 3 root root 4096 Sep 16 16:05 .
4 drwxr-xr-x 12 root root 4096 Sep 16 16:45 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 beef
root@5592454f9f4d:/s3-data# exit
exit
[root@host s3-data-mnt]# ls /mounts/s3-data-mnt
total 6
1 drwxrwxrwx 1 root root 0 Jan 1 1970 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
[root@host /]# umount -l s3-data-mnt
[root@host /]# ls -als
[root@ip-10-0-3-233 /]# ls -als /s3-stn-jira-data-mnt/
total 8
4 drwxr-xr-x 2 root root 4096 Sep 16 17:28 .
4 dr-xr-xr-x 28 root root 4096 Sep 16 17:06 ..
1 -rw-r--r-- 1 root root 4 Sep 16 17:11 bar
您可以通过将挂载调用包装在 nsenter
中以将其挂载在与 docker 守护程序相同的 Linux 挂载命名空间中来解决此问题,例如。
nsenter -t "$PID_OF_DOCKER_DAEMON" encfs ...
问题是这种方法是否会在守护进程自身重新启动后继续存在。 ;-)