我在 CentOS 7 主机上使用 Docker 1.6,并使用 CentOS 7 容器。 在我的大多数容器中,DNS 不起作用,因为即使 root 也无法读取/etc/resolv.conf:
[root@7ba55011e7ab etc]# ls -l /etc/resolv.conf
ls: cannot access /etc/resolv.conf: Permission denied
这种情况发生在我的大多数容器中,甚至是直接从标准 Docker centos:latest 镜像创建的容器。 (当我使用标准 Docker debian 镜像时,也会出现此问题。) resolv.conf 唯一可读的容器是我从库存 centos 镜像创建的第一个容器。
不用说,我已经多次跳出 Docker,并重新启动了主机。我还尝试在/etc/sysconfig/docker 的选项中使用 --dns hostname 。但这当然没有帮助,因为问题不是 resolv.conf 的内容,而是我无法读取它(即使是 root)。
我知道/etc/resolv.conf 是从主机的/etc/resolv.conf“绑定(bind)安装”的。该文件的主机副本看起来不错,并且权限看起来很合理:
[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的comment , issue 11396提到您可以通过以下任意一种方式授予容器写入权限(也意味着至少具有读取权限):
- Disable SELinux for the entire host:
setenforce 0
参见issue 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
- Set the directory SELinux policy to allow any container access:
chcon -Rt svirt_sandbox_file_t /var/db
- Make the container
--privileged
.
This disables not only SELinux constraints but also the default cgroups restrictions:
docker run --privileged -v /var/db:/data1 -i -t fedora
- Disable SELinux policy constraints for this container only:
docker run --security-opt label:disable -v /var/db:/data1 -i -t fedora
- Run the container processes as SELinux process type
unconfined_t
:
docker run --security-opt label:type:unconfined_t -v /var/db:/data1 -i -t fedora
关于dns - Docker:为什么/etc/resolv.conf不可读?破坏 DNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29979656/