linux - 无法访问 Docker 容器中已安装的主机目录

标签 linux docker mount

我正在尝试让我的 Docker 容器读取和写入主机目录。

我运行容器:

docker run -it -v $(pwd):/file logstash-5.1.2

在容器内,我可以看到/file 具有我在主机上的(非 root)用户的 uid,以及与主机上相同的权限:

drwxrwxrwx.   2 1156 1156 4096 Jul 21 05:00 file

并且 root 无法访问/file。

root@c642b0c37e09:~# ls /file
ls: cannot open directory /file: Permission denied

我读过有关在容器中创建与主机具有相同 uid 的用户的文章,但这似乎不受欢迎。

  1. 为什么 root 无法访问该目录?我认为它可以做所有事情。
  2. 在 Docker 中让容器读取和写入不属于 root 的已安装目录的最佳方式是什么?
  3. 我们也在使用 Rancher。这会让事情变得更容易吗?我还没有遇到不同的东西,主要是因为我想看看是否可以纯粹在 Docker 中完成它。

最佳答案

您应该将上下文更改为 svirt_sandbox_file_t 以让容器在此上下文中访问此文件夹。

如果您确定文件夹权限,那么只需尝试即可;

chcon  -R -t svirt_sandbox_file_t /your/host/path

如果不确定,请尝试;

chown  -R groupId:userId /your/host/path
chcon  -R -t svirt_sandbox_file_t /your/host/path

这里 chcon 命令应用 SELinux 上下文,将“/your/host/path”的上下文更改为 svirt_sandbox_file_t。

关于linux - 无法访问 Docker 容器中已安装的主机目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232009/

相关文章:

linux - docker (撰写): connection refused without sudo

html - 如果更改屏幕 DPI,Linux 上的 chrome 浏览器会弄乱以像素为单位的字体大小

linux - docker -libudev : udev_has_devtmpfs: name_to_handle_at on/dev: Operation not permitted

python - 如何在用 python 编写的 tail 函数中附加一些数据

linux - Libc的静态链接

docker - 是否可以将 Kubernetes 用于 Gitlab 构建运行器以及如何使用?

docker - 如何覆盖/发现基本图像的 CMD/ENTRYPOINT

linux - Chef - 如何使用 UUID 挂载 LVM

linux - 将 s3fs 作为卷提供给 docker

linux - Samba 导致 "mount:/is busy"