linux - 为什么 docker 容器提示 "Permission denied"?

标签 linux docker permissions permission-denied user-permissions

我使用以下命令运行 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/

相关文章:

linux - 在 Linux 上,如何在不同的登录名下从终端 shell 运行 GUI 应用程序?

linux - 在 bash 中扩展一个可能的相对路径

c - 在 python 中使用 keras+tensorflow 进行训练并在 C/C++ 中进行预测

docker - 为什么我的 docker-compose 以我的 dockerfile 中的 prod 阶段为目标也运行 dev 目标?

sql - 授予 SQL 作业权限,以便能够访问/修改不同服务器上的文件夹和文件

postgresql - 如何授予用户对 Postgres 中所有新表的权限?

r - 无法在没有 root 权限的集群上安装 nloptr/nlopt

c - 是否可以删除/proc/<pid>/fd中的fd链接?

node.js - 获取https://gcp.io/v2/:x509:未知授权机构签名的证书

docker-compose 健康检查重试频率 != 间隔