在构建 docker 镜像时,我有一个 curl 命令,它可以像这样拉下一个文件:RUN curl -L http://files.mycompany.com/ -o file.war
files.mycompany.com 是只能从公司网络内访问的服务器。我可以从我的主机访问此服务器,但如果我使用该名称(IP 工作正常),则不能从 docker 容器内访问。
这有效:RUN ping google.com
这有效:RUN ping 10.3.2.1
(files.mycompany.com 的 IP)
这不起作用:RUN ping files.mycompany.com
(将名称转换为另一个 IP,而不是从主机 ping 同一服务器)
由于从同一网络上的另一台开发计算机构建容器工作正常,因此在我的机器上设置不正确。就像docker接口(interface)没有收到本地网络的DNS记录一样?
我正在运行 Ubuntu 17.04。
最佳答案
在您的主机中执行此操作:
cat /etc/resolv.conf
如果您看到类似
127.0.0...
的内容,这意味着您的主机使用的 DNS 配置是一个监听 localhost 的守护进程。 Docker 无法告诉您的容器使用相同的 DNS,因为容器有自己的 localhost,因此 docker 默认使用 Google DNS (8.8.8.8)。您可以确认这样做 里面 容器:cat /etc/resolv.conf
我建议您按照步骤 here ,所以编辑你的
/etc/docker/daemon.json
,并把这个:{"dns": ["your_dns_server_ip"]}
关于
/etc/default/docker
的注意事项: 这个文件在最新的 Ubuntu 版本中不再使用。相反,创建我已经指出的 json 文件。见 the docs : /etc/docker/daemon.json
关于Docker 容器无法访问本地网络 DNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410029/