【问题标题】:why am I getting permission denied on file with read permissions inside Docker container?为什么我在 Docker 容器内获得具有读取权限的文件的权限被拒绝?
【发布时间】:2016-06-02 22:40:22
【问题描述】:

我有一个 docker 容器,它在容器内的用户帐户“go”下运行 GOCD 服务器(java 应用程序)。

此容器将 /etc/hosts 挂载为 644(所有用户均可读取),但“go”帐户似乎无法读取此文件。

这是证据:

[~] # docker -v
Docker version 1.10.2, build 0762ca4

# first enter the container as root and read the contents of 
# /etc/hosts
~] # docker exec  -it 0dac9bf0eab5 bash
  root@gocd:/# ls -la /etc/hosts
  -rw-r--r--+ 1 root root 164 Jun  2 22:03 /etc/hosts

    #no problem - file is readable
   root@gocd:/# cat /etc/hosts
   127.0.0.1    localhost
   ::1  localhost ip6-localhost ip6-loopback
   10.0.3.2 gocd
   root@gocd:/#

  # now change user to 'go'
   root@gocd:/# su - go -c bash
   go@gocd:/$ id
   uid=999(go) gid=999(go) groups=999(go)

   # check permissions - still 644
   go@gocd:/$ ls -la /etc/hosts
    -rw-r--r--+ 1 root root 164 Jun  2 22:03 /etc/hosts

   # but trying to read the file - causes error:
     go@gocd:/$ cat /etc/hosts
     cat: /etc/hosts: Permission denied

任何想法为什么会发生这种情况?

【问题讨论】:

  • 我也试过了。它向我展示了 /etc/hosts。相同的权限。唯一的区别:-rw-r--r--。而不是+。 '+' 表示这是一个 ACL。检查它是否是一个 ACL: 'ls -le filename' 。摆脱 ACL 运行 echo | sudo chmod -E 文件名或 echo | sudo chmod -R -E 目录的目录名称。让我知道它是否有帮助/有效。 (如果可行,我会将其作为答案发布)。
  • 你是绝对正确的 - 这是 ACL 的事情。我已经使用 'root@gocd:/# setfacl -b /etc/hosts' 删除了 ACL,并且容器内的应用程序开始工作。所以一切都很好 - 唯一的事情 - 我想避免对我创建的每个容器都这样做。我使用 QNAP 和 ContainerStation - 它是 docker 的包装器,我怀疑它是 ContainerStation,它在 /etc/hosts 上创建了一个带有 ACL 的容器。非常感谢您的想法!
  • 这里是威联通论坛的相关讨论:forum.qnap.com/viewtopic.php?t=117600
  • 发布您的步骤作为答案

标签: docker


【解决方案1】:

问题与 ACL 权限有关,该权限限制了对 /etc/hosts 和 /etc/resolv.conf 的 READ 访问权限仅限于 root。

结果,在容器内的任何其他帐户下运行的应用程序无法读取这些文件,这会导致网络堆栈出现问题。例如,需要将主机解析为 ip 的 java 应用程序会得到 UnknownHost Exception。

如果容器是使用 ContainerStation 创建的,QNAP 系统会出现此问题。 ContainerStation 中显然没有设置来更改此行为,但可以通过添加到容器中的启动脚本中的以下命令来修复:

# modify ACL so go user would have read access
# to /etc/hosts and /etc/resolv.conf
# this is to avoid HostUnknown exception which happends
# when the gocd container is used on QNAP with ContainerStation
setfacl -m user:${USER_ID}:r /etc/resolv.conf
setfacl -m user:${USER_ID}:r /etc/hosts

对于想要运行 GOCD 服务器的 QNAP 用户,我在 docker hub 上创建了一个 docker 容器,其中已经包含此修复:

https://hub.docker.com/r/rshestakov/docker-gocd-server/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多