Docker:为什么/etc/resolv.conf不可读? 打破DNS

我使用CentOS 7容器在CentOS 7主机上使用Docker 1.6。 在我的大部分容器中,DNS不起作用,因为/etc/resolv.conf不能被读取,甚至是root:

[root@7ba55011e7ab etc]# ls -l /etc/resolv.conf ls: cannot access /etc/resolv.conf: Permission denied 

这发生在我的大部分容器中,甚至是直接从标准Docker centos创build的容器:最新的图像。 (当我使用标准的Docker debian镜像时,也出现了这个问题。)resolv.conf中唯一可读的容器是我从stock centos镜像创build的第一个容器。

不用说,我已经多次反弹了Docker,并重新启动了主机。 我也尝试在/ etc / sysconfig / docker中的OPTIONS中使用–dns 主机名 。 但是,这当然不会有帮助,因为resolv.conf的内容不是问题,而是我无法读取(甚至是root)的事实。

我知道/etc/resolv.conf是从主机的/etc/resolv.conf中“绑定挂载”的。 主机的这个文件的副本看起来很好,权限看起来是合理的:

 [root@co7mars2 etc]# ls -l /etc/resolv.conf -rw-r--r--. 1 root root 106 Apr 30 18:08 /etc/resolv.conf 

我无法从容器内卸载/etc/resolv.conf:

 umount -f -r /etc/resolv.conf umount: /etc/resolv.conf: must be superuser to umount 

有没有解决这个问题?

我在Docker的github网站上看到了一些相关的问题,比如https://github.com/docker/docker/issues/2267 ,但是他们解决了复杂用例的增强,而不是我的情况,我刚刚死了水。

顺便说一句,我已经在两个单独的和不相关的CentOS 7主机上尝试了这个,结果也是一样的。

谢谢。

要添加到Daniel t的评论中 , 问题11396提到可以通过以下任何一种方式给容器写入权限(意味着至less读取权限):

  • 禁用整个主机的SELinux: setenforce 0

见问题7952 :

 # Example of proper behavior on fresh btrfs system when SELinux is in Permissive mode [~]$ getenforce Enforcing [~]$ sudo setenforce 0 [~]$ getenforce Permissive [~]$ sudo docker run fedora echo "hello world" hello world [~]$ sudo setenforce 1 [~]$ sudo docker run fedora echo "hello world" echo: error while loading shared libraries: libc.so.6: cannot open shared object file: Permission denied 
  • 设置SELinux策略目录以允许任何容器访问:
  chcon -Rt svirt_sandbox_file_t /var/db 
  • 使容器 – --privileged
    这不仅会禁用SELinux约束,还会禁用默认的cgroups限制:
  docker run --privileged -v /var/db:/data1 -i -t fedora 
  • 仅对此容器禁用SELinux策略约束:
  docker run --security-opt label:disable -v /var/db:/data1 -i -t fedora 
  • 以SELinux进程typesunconfined_t运行容器进程:
  docker run --security-opt label:type:unconfined_t -v /var/db:/data1 -i -t fedora