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

标签 dns docker

我在 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 tcomment , 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/

相关文章:

docker - 使用外部的实际环境覆盖 docker-compose env_file 中的变量

python - Docker tensorflow-gpu镜像仅适用于cpu

android - 对于移动应用程序来说,使用域名连接服务器还是使用 IP 地址哪个更好?

DNS 两域一台服务器

azure - 我可以将 (CNAME) 自定义域映射到属于 Azure Web Apps 通配符一部分的子域吗?

linux - 如果 docker 使用底层 linux 操作系统,为什么要在 Dockerfile 的 FROM 行中指定操作系统

Docker Alpine Latext texmf-dist 样式未找到

amazon-web-services - 我如何使用 ecr 与来自其他注册表的正常 docker 登录

apache - HTTP/2 和 CNAME 如何协同工作?

python - 在 dnspython 中返回 'A' DNS 记录