我使用以下命令运行 docker 容器,并将目录从主机(/root/database
)映射到容器(/tmp/install/database
):
# docker run -it --name oracle_install -v /root/database:/tmp/install/database bofm/oracle12c:preinstall bash
但在容器中,我发现我无法使用 ls
列出 /tmp/install/database/
中的内容,尽管我是 root
并拥有所有特权:
[root@77eb235aceac /]# cd /tmp/install/database/
[root@77eb235aceac database]# ls
ls: cannot open directory .: Permission denied
[root@77eb235aceac database]# id
uid=0(root) gid=0(root) groups=0(root)
[root@77eb235aceac database]# cd ..
[root@77eb235aceac install]# ls -alt
......
drwxr-xr-x. 7 root root 4096 Jul 7 2014 database
我在主机中检查了 /root/database
,一切似乎都正常:
[root@localhost ~]# ls -lt
......
drwxr-xr-x. 7 root root 4096 Jul 7 2014 database
为什么docker容器提示“Permission denied”?
更新:
根本原因与 SELinux
有关。其实我也遇到过类似的issue去年。
最佳答案
在容器内拒绝共享目录的权限可能是因为该共享目录存储在设备上。默认情况下,容器不能访问任何设备。添加选项 $docker run --privileged
允许容器访问 所有 设备并执行内核调用。 这不被认为是安全的。
共享设备的更简洁的方法是使用选项 docker run --device=/dev/sdb
(如果 /dev/sdb
是您想要共享的设备分享)。
来自手册页:
--device=[] Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc:rwm) --privileged=true|false Give extended privileges to this container. The default is false. By default, Docker containers are “unprivileged” (=false) and cannot, for example, run a Docker daemon inside the Docker container. This is because by default a container is not allowed to access any devices. A “privileged” container is given access to all devices. When the operator executes docker run --privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor to allow the container nearly all the same access to the host as processes running outside of a container on the host.
关于linux - 为什么 docker 容器提示 "Permission denied"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617912/