简体   繁体   English

无法在docker容器中编辑/etc/resolv.conf

[英]unable to edit /etc/resolv.conf in docker container

I want to add a domain entry to /etc/resolv.conf of my docker container. 我想在我的docker容器的/etc/resolv.conf中添加一个域条目。

Here is my dockerFile 这是我的dockerFile

FROM tomcat:8.0.20-jre7
VOLUME /tmp
#RUN sed -i "s|search local|domain com.example.com|g;" /etc/resolv.conf
RUN echo "domain com.example.com" >> /etc/resolv.conf
# Expose ports.
EXPOSE 8080

I tried both echo and sed . 我试过了echosed with sed , I get error during build. 使用sed ,我在构建期间遇到错误。

sed: cannot rename /etc/sed6LcoES: Device or resource busy

but with echo container build and run successfully. 但使用echo容器构建并成功运行。 however, when I get into container, I do not see my domain added in /etc/resolv.conf . 但是,当我进入容器时,我没有在/etc/resolv.conf看到我的域名添加。

why is it not working? 为什么不工作?

NOTE: I have got dns-search working by passing during run argument 注意:我通过在运行参数期间传递来进行dns-search工作

docker run -p 8080:8080 --dns-search=com.example.com -d --name myawesome my/myawesome:latest

but I am interested in getting dockerFile working. 但我有兴趣让dockerFile工作。

This is by design. 这是设计的。 /etc/resolv.conf is used by docker engine to handle service discovery. /etc/resolv.conf由docker引擎用于处理服务发现。 Documentation states the following: 文档说明如下:

How can Docker supply each container with a hostname and DNS configuration, without having to build a custom image with the hostname written inside? Docker如何为每个容器提供主机名和DNS配置,而无需使用内部编写的主机名构建自定义映像? Its trick is to overlay three crucial /etc files inside the container with virtual files where it can write fresh information … This arrangement allows Docker to do clever things like keep resolv.conf up to date across all containers when the host machine receives new configuration over DHCP later. 它的诀窍是用虚拟文件覆盖容器内的三个关键/ etc文件,它可以写出新的信息......这种安排允许Docker做一些聪明的事情,例如当主机接收到新配置时,保持resolv.conf在所有容器中保持最新状态DHCP以后。 The exact details of how Docker maintains these files inside the container can change from one Docker version to the next, so you should leave the files themselves alone and use the following Docker options instead. Docker如何在容器中维护这些文件的确切细节可以从一个Docker版本更改为下一个版本,因此您应该单独保留文件并使用以下Docker选项。

If you want to override/reconfigure some dns settings, use --dns parameters during container starting. 如果要覆盖/重新配置某些dns设置, --dns在容器启动期间使用--dns参数。 See more details: 查看更多详情:

Using the standard output stream can be solved by trying the following code, the namespace section is corrected to its own DNS server IP, but this is only valid while the container is running: 使用标准输出流可以通过尝试以下代码来解决,命名空间部分更正为其自己的DNS服务器IP,但这仅在容器运行时有效:

echo "$(sed '2,$c nameserver 223.5.5.5\nnameserver 223.6.6.6' /etc/resolv.conf)" > /etc/resolv.conf

The sed command does not actually modify a file, but instead creates a new file to replace the original file.(so edit it by vim is ok). sed命令实际上并不修改文件,而是创建一个新文件来替换原始文件。(所以通过vim编辑它就可以了)。 Because /etc/resolve.conf is mounted, replacing the original file will inform the device that it is busy. 由于安装了/etc/resolve.conf ,因此替换原始文件将通知设备它正忙。

You can observe the files being mounted by command df -ah : 您可以通过命令df -ah观察正在挂载的文件:

Filesystem      Size  Used Avail Use% Mounted on
overlay          59G  7.5G   49G  14% /
...
mqueue             0     0     0    - /dev/mqueue
shm              64M     0   64M   0% /dev/shm
/dev/sda1        59G  7.5G   49G  14% /etc/resolv.conf
....

The right way, you can specify the container's DNS server IP when running docker images: 正确的方法是,您可以在运行docker镜像时指定容器的DNS服务器IP:

$ docker run --help|grep dns
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains

$ docker run -it --rm --dns=223.5.5.5 --dns=223.6.6.6 centos:perf-tools /bin/bash
[root@ea0ac0fcd834 /]# cat /etc/resolv.conf
nameserver 223.5.5.5
nameserver 223.6.6.6

In addition, DNS server IP can be modified based on the docker-composer.yml configuration file: 此外,可以根据docker-composer.yml配置文件修改DNS服务器IP:

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9

more see: https://docs.docker.com/v17.09/compose/compose-file/#dns 更多信息请参见: https//docs.docker.com/v17.09/compose/compose-file/#dns

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 如何使用 /etc/resolv.conf 中定义的名称服务器启动容器 - Docker how to start container with defined nameservers in /etc/resolv.conf 如何从 docker-compose 文件覆盖 /etc/resolv.conf 中 Docker 的嵌入式 DNS 服务器 - How to override embedded DNS server for Docker in /etc/resolv.conf from a docker-compose file 如何在docker文件或脚本中以非root用户身份修改/etc/resolv.conf - how to modify /etc/resolv.conf as non root user in docker file or as script 在 Ubuntu 16.04 docker 映像中安装 apt-get:'/etc/resolv.conf':设备或资源繁忙 - apt-get install in Ubuntu 16.04 docker image: '/etc/resolv.conf': Device or resource busy Docker 使用主机的 DNS 代替 /etc/resolv.conf 中提到的 DNS - Docker uses the DNS of the host instead of the DNS mentioned in /etc/resolv.conf 无法为服务 xxx 创建容器:打开 /var/lib/docker/overlay2/969rf5...fdf-init/merged/etc/resolv.conf:不允许操作 - Cannot create container for service xxx: open /var/lib/docker/overlay2/969rf5...fdf-init/merged/etc/resolv.conf: Operation not permitted chown: /etc/resolv.conf: 只读文件系统 - chown: /etc/resolv.conf: Read-only file system 如何更改 OpenShift pod 中的 /etc/resolv.conf 文件 - How to change /etc/resolv.conf file in an OpenShift pod 如何更改现有 docker 容器的 resolv.conf - How to change resolv.conf for existing docker containers docker-compose不会更新resolv.conf - docker-compose does not update resolv.conf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM