简体   繁体   English

Docker 构建“无法解析‘archive.ubuntu.com’”apt-get 无法安装任何东西

[英]Docker build "Could not resolve 'archive.ubuntu.com'" apt-get fails to install anything

I've been trying to run Docker build on various files which previously worked before, which are now no longer working.我一直在尝试在以前工作过的各种文件上运行 Docker 构建,这些文件现在不再工作。

As soon as the Docker file included any line that was to install software it would fail with a message saying that the package was not found.只要 Docker 文件包含要安装软件的任何行,它就会失败并显示一条消息,指出未找到该软件包。

RUN apt-get -y install supervisor nodejs npm

The common message which showed up in the logs was日志中显示的常见消息是

Could not resolve 'archive.ubuntu.com'

Any idea why any software will not install?知道为什么任何软件都不会安装吗?

Uncommenting DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" in /etc/default/docker as Matt Carrier suggested did NOT work for me.取消注释DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"/etc/default/docker马特运营商建议并没有为我工作。 Nor did putting my corporation's DNS servers in that file.也没有将我公司的 DNS 服务器放在该文件中。 But, there's another way (read on).但是,还有另一种方式(继续阅读)。

First, let's verify the problem:首先,让我们验证一下问题:

$ docker run --rm busybox nslookup google.com   # takes a long time
nslookup: can't resolve 'google.com'   # <--- appears after a long time
Server:    8.8.8.8
Address 1: 8.8.8.8

If the command appears to hang, but eventually spits out the error "can't resolve 'google.com'", then you have the same problem as me.如果该命令似乎挂起,但最终会吐出错误“无法解析 'google.com'”,那么您遇到了与我相同的问题。

The nslookup command queries the DNS server 8.8.8.8 in order to turn the text address of 'google.com' into an IP address. nslookup命令查询 DNS 服务器 8.8.8.8 以将“google.com”的文本地址转换为 IP 地址。 Ironically, 8.8.8.8 is Google's public DNS server .具有讽刺意味的是,8.8.8.8 是Google 的公共 DNS 服务器 If nslookup fails, public DNS servers like 8.8.8.8 might be blocked by your company (which I assume is for security reasons).如果nslookup失败,您的公司可能会阻止像 8.8.8.8 这样的公共 DNS 服务器(我认为这是出于安全原因)。

You'd think that adding your company's DNS servers to DOCKER_OPTS in /etc/default/docker should do the trick, but for whatever reason, it didn't work for me.您会认为将您公司的 DNS 服务器添加到/etc/default/docker DOCKER_OPTS中的DOCKER_OPTS应该可以解决问题,但无论出于何种原因,它对我不起作用。 I describe what worked for me below.我在下面描述了对我有用的东西。

SOLUTION :解决方案

On the host (I'm using Ubuntu 16.04), find out the primary and secondary DNS server addresses:在主机上(我使用的是 Ubuntu 16.04),找出主要和辅助 DNS 服务器地址:

$ nmcli dev show | grep 'IP4.DNS'
IP4.DNS[1]:              10.0.0.2
IP4.DNS[2]:              10.0.0.3

Using these addresses, create a file /etc/docker/daemon.json :使用这些地址,创建一个文件/etc/docker/daemon.json

$ sudo su root
# cd /etc/docker
# touch daemon.json

Put this in /etc/docker/daemon.json :把它放在/etc/docker/daemon.json

{                                                                          
    "dns": ["10.0.0.2", "10.0.0.3"]                                                                           
}     

Exit from root:从 root 退出:

# exit

Now restart docker:现在重启docker:

$ sudo service docker restart

VERIFICATION :验证

Now check that adding the /etc/docker/daemon.json file allows you to resolve 'google.com' into an IP address:现在检查添加/etc/docker/daemon.json文件是否允许您将“google.com”解析为 IP 地址:

$ docker run --rm busybox nslookup google.com
Server:    10.0.0.2
Address 1: 10.0.0.2
Name:      google.com
Address 1: 2a00:1450:4009:811::200e lhr26s02-in-x200e.1e100.net
Address 2: 216.58.198.174 lhr25s10-in-f14.1e100.net

REFERENCES :参考资料

I based my solution on an article by Robin Winslow, who deserves all of the credit for the solution.我的解决方案基于 Robin Winslow 的一篇文章,他的解决方案值得所有人称赞。 Thanks, Robin!谢谢,罗宾!

"Fix Docker's networking DNS config." “修复 Docker 的网络 DNS 配置。” Robin Winslow.罗宾·温斯洛。 Retrieved 2016-11-09.检索 2016-11-09。 https://robinwinslow.uk/2016/06/23/fix-docker-networking-dns/ https://robinwinslow.uk/2016/06/23/fix-docker-networking-dns/

After much headache I found the answer.经过一番头痛,我找到了答案。 Could not resolve 'archive.ubuntu.com' can be fixed by making the following changes: Could not resolve 'archive.ubuntu.com'可以通过进行以下更改来修复:

  1. Uncomment the following line in /etc/default/docker取消注释/etc/default/docker的以下行
    DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

  2. Restart the Docker service sudo service docker restart重启 Docker 服务sudo service docker restart

  3. Delete any images which have cached the invalid DNS settings.删除所有缓存了无效 DNS 设置的图像。

  4. Build again and the problem should be solved.再次构建,应该可以解决问题。

Credit goes to Andrew SB归功于Andrew SB

I run into the same problem, but neiter uncommenting /etc/default/docker dns entries nor editing the /etc/resolv.conf in the build container or the /etc/docker/daemon.json helps for me.我遇到了同样的问题,但是没有取消注释/etc/default/ docker dns 条目,也没有编辑构建容器中的/etc/resolv.conf/etc/docker/daemon.json对我有帮助。

But after I build with the option --network=host the resolving was fine again.但是在我使用选项 --network=host 构建之后,解析又好了。

docker build --network=host -t my-own-ubuntu-like-image .

Maybe this will help someone again.也许这会再次帮助某人。

I believe that Matt Carrier's answer is the correct solution for this problem.我相信Matt Carrier 的回答是这个问题的正确解决方案。 However, after implementing it, I still observed the same behavior: could not resolve 'archive.ubuntu.com' .但是,在实施之后,我仍然观察到相同的行为: could not resolve 'archive.ubuntu.com'

This led me to eventually find that the network I was connected to was blocking public DNS.这让我最终发现我所连接的网络阻止了公共 DNS。 The solution to this problem was to configure my Docker container to use the same name server that my host (the machine from which I was running Docker) was using.此问题的解决方案是将我的 Docker 容器配置为使用与我的主机(运行 Docker 的机器)使用的名称服务器相同的名称服务器。

How I triaged:我如何分类:

  1. Since I was working through the Docker documentation, I already had an example image installed on my machine.由于我正在阅读 Docker 文档,因此我已经在我的机器上安装了一个示例映像。 I was able to start a new container to run that image and create a new bash session in that container: docker run -it docker/whalesay bash我能够启动一个新容器来运行该映像并在该容器中创建一个新的 bash 会话: docker run -it docker/whalesay bash
  2. Does the container have an Internet connection?: ping 172.217.4.238 (google.com)容器是否有互联网连接?: ping 172.217.4.238 (google.com)
  3. Can the container resolve hostnames?容器可以解析主机名吗? ping google.com

In my case, the first ping resulted in responses, the second did not.在我的情况下,第一个ping导致响应,第二个没有。

How I fixed:我是如何修复的:

Once I discovered that DNS was not working inside the container, I verified that I could duplicate the same behavior on the host.一旦我发现 DNS 在容器内不起作用,我就确认我可以在主机上复制相同的行为。 nslookup google.com resolved just fine on the host. nslookup google.com在主机上解决得很好。 But, nslookup google.com 8.8.8.8 or nsloookup google.com 8.8.4.4 timed out.但是, nslookup google.com 8.8.8.8nsloookup google.com 8.8.4.4超时。

Next, I found the name server(s) that my host was using by running nm-tool (on Ubuntu 14.04).接下来,我通过运行nm-tool (在 Ubuntu 14.04 上)找到了我的主机正在使用的名称服务器。 In the vein of fast feedback, I started up the example image again, and added the IP address of the name server to the container's resolv.conf file: sudo vi /etc/resolv.conf .为了快速反馈,我再次启动了示例镜像,并将名称服务器的 IP 地址添加到容器的 resolv.conf 文件中: sudo vi /etc/resolv.conf Once saved, I attempted the ping again ( ping google.com ) and this time it worked!保存后,我再次尝试 ping ( ping google.com ),这次成功了!

Please note that the changes made to the container's resolv.conf are not persistent and will be lost across container restarts.请注意,对容器的 resolv.conf 所做的更改不是持久的,并且会在容器重新启动时丢失。 In my case, the more appropriate solution was to add the IP address of my network's name server to the host's /etc/default/docker file.就我而言,更合适的解决方案是将我的网络名称服务器的 IP 地址添加到主机的/etc/default/docker文件中。

After adding local dns ip to default docker file it started working for me... please find the below steps...将本地 dns ip 添加到默认 docker 文件后,它开始为我工作...请找到以下步骤...

$ nm-tool # (will give you the dns IP)

DNS : 172.168.7.2 DNS : 172.168.7.2

$ vim /etc/default/docker # (uncomment the DOCKER_OPTS and add DNS IP)
DOCKER_OPTS="--dns 172.168.7.2 --dns 8.8.8.8 --dns 8.8.4.4"

$ rm `docker ps --no-trunc -aq` # (remove all the containers to avoid DNS cache)

$ docker rmi $(docker images -q) # (remove all the images)

$ service docker restart #(restart the docker to pick up dns setting)

Now go ahead and build the docker... :)现在继续构建 docker... :)

For anyone who is also having this problem, I solved my problem by editing the /etc/default/docker file, as suggested by other answers and questions.对于也遇到此问题的任何人,我按照其他答案和问题的建议,通过编辑/etc/default/docker文件解决了我的问题。 However I had no idea what IP to use as the DNS.但是我不知道使用什么 IP 作为 DNS。

It was only after a while I figured out I had to run ifconfig docker on the host to show the IP for the docker network interface.过了一会儿,我才发现我必须在主机上运行ifconfig docker才能显示 docker 网络接口的 IP。

docker0   Link encap:Ethernet  Endereço de HW 02:42:69:ba:b4:07  
          inet end.: 172.17.0.1  Bcast:0.0.0.0  Masc:255.255.0.0
          endereço inet6: fe80::42:69ff:feba:b407/64 Escopo:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
          pacotes RX:8433 erros:0 descartados:0 excesso:0 quadro:0
          Pacotes TX:9876 erros:0 descartados:0 excesso:0 portadora:0
          colisões:0 txqueuelen:0 
          RX bytes:484195 (484.1 KB) TX bytes:24564528 (24.5 MB)

It was 172.17.0.1 in my case.就我而言,它是172.17.0.1 Hope this helps anyone who is also having this issue.希望这可以帮助任何也遇到此问题的人。

I found this answer after some Googleing.我在谷歌搜索后找到了这个答案 I'm using Windows, so some of the above answers did not apply to my file system.我使用的是 Windows,所以上面的一些答案不适用于我的文件系统。

Basically run:基本上运行:

docker-machine ssh default
echo "nameserver 8.8.8.8" > /etc/resolv.conf

Which just overwrites the existing nameserver used with 8.8.8.8 I believe.我相信这只会覆盖与8.8.8.8使用的现有名称服务器。 It worked for me!它对我有用!

Based on some comments, you may have to be root.根据一些评论,您可能必须是 root。 To do that, issue sudo -i .为此,请发出sudo -i

I just wanted to add a late response for anyone coming across this issue from search engines.我只是想为从搜索引擎遇到此问题的任何人添加一个迟到的回复。

Do NOT do this: I used to have an option in /etc/default/docker to set iptables=false .不要这样做:我曾经在 /etc/default/docker 中有一个选项来设置iptables=false This was because ufw didn't work (everything was opened even though only 3 ports were allowed) so I blindly followed the answer to this question: Uncomplicated Firewall (UFW) is not blocking anything when using Docker and this, which was linked in the comments这是因为 ufw 不起作用(即使只允许 3 个端口,所有东西都被打开)所以我盲目地遵循了这个问题的答案: Uncomplicated Firewall (UFW) 在使用 Docker 时没有阻止任何东西,这在注释

I have a very low understanding of iptables rules / nat / routing in general, hence why I might have done something irrational.总的来说,我对 iptables 规则/nat/路由的理解非常低,因此我可能会做一些不合理的事情。

Turns out that I've probably misconfigured it and killed DNS resolution inside my containers.事实证明,我可能对它进行了错误配置并在我的容器内终止了 DNS 解析。 When I ran an interactive container terminal: docker run -i -t ubuntu:14.04 /bin/bash当我运行交互式容器终端时: docker run -i -t ubuntu:14.04 /bin/bash

I had these results:我有这些结果:

root@6b0d832700db:/# ping google.com
ping: unknown host google.com

root@6b0d832700db:/# cat /etc/resolv.conf
search online.net
nameserver 8.8.8.8
nameserver 8.8.4.4

root@6b0d832700db:/# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=56 time=1.76 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=56 time=1.72 ms

Reverting all of my ufw configuration (before.rules), disabling ufw and removing iptables=false from /etc/default/docker restored the DNS resolution functionality of the containers.恢复我的所有 ufw 配置(before.rules),禁用 ufw 并从 /etc/default/docker 中删除 iptables=false 恢复了容器的 DNS 解析功能。

I'm now looking forward to re-enable ufw functionality by following these instructions instead.我现在期待按照这些说明重新启用 ufw 功能。

I have the same issue, and tried the steps mentioned, but seems none works until refresh the network settings.我有同样的问题,并尝试了提到的步骤,但在刷新网络设置之前似乎没有任何效果。

The steps:步骤:

  1. As mentioned, add DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true" to /etc/default/docker .如前所述,将DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --ip-masq=true"/etc/default/docker
  2. Manually flush the PREROUTING table contents using the iptables -t nat -F POSTROUTING .使用iptables -t nat -F POSTROUTING手动刷新 PREROUTING 表内容。 After running this, restart docker and it will initialize the nat table with the new IP range.运行此命令后,重新启动 docker,它将使用新的 IP 范围初始化 nat 表。

I have struggled for some time with this now as well, but here it is what solved it for me on Ubuntu 16.04 x64 .我现在也为此苦苦挣扎了一段时间,但这就是在Ubuntu 16.04 x64上为我解决的方法。 I hope it saves someone's time, too.我希望它也可以节省某人的时间。

  1. In /etc/NetworkManager/NetworkManager.conf : comment out #dns=dnsmasq/etc/NetworkManager/NetworkManager.conf :注释掉#dns=dnsmasq

  2. Create (or modify) /etc/docker/daemon.json :创建(或修改) /etc/docker/daemon.json

{
    "dns": ["8.8.8.8"]
}
  1. Restart docker with: sudo service docker restart使用以下sudo service docker restart重新启动 docker: sudo service docker restart

Same issue for me (on Ubuntu Xenial).对我来说同样的问题(在 Ubuntu Xenial 上)。

  • docker run --dns ... for containers worked. docker run --dns ...用于容器工作。
  • Updating docker daemon options for docker build (docker-compose etc.) did not work.更新 docker docker build (docker-compose 等)的 docker 守护进程选项不起作用。

After analyzing the docker logs ( journalctl -u docker.service ) if found some warning about bad resolvconf applied.在分析journalctl -u docker.service日志( journalctl -u docker.service )后,如果发现一些关于应用了错误 resolvconf 的警告。

Following that i found that our corporate nameservers were added to the network interfaces but not in resolvconf.之后,我发现我们的公司名称服务器已添加到网络接口中,但未添加到 resolvconf 中。

Applied this solution How do I configure my static DNS in interfaces?应用此解决方案如何在接口中配置静态 DNS? (askubuntu) , ie adding nameservers to /etc/resolvconf/resolv.conf.d/tail (askubuntu)即将名称服务器添加到/etc/resolvconf/resolv.conf.d/tail

After updating resolvconf (or reboot).更新resolvconf(或重新启动)后。

bash docker run --rm busybox nslookup google.com

worked instantly.立即工作。

All my docker-compose builds are working now.我所有的 docker-compose 构建现在都在工作。

I got same issue today, I just added line below to /etc/default/docker我今天遇到了同样的问题,我只是在 /etc/default/docker 下面添加了一行

DOCKER_OPTS="--dns 172.18.20.13 --dns 172.20.100.29 --dns 8.8.8.8"

and then I restarted my Laptop.然后我重新启动了我的笔记本电脑。

In my case restarting docker daemon is not enough for me, I have to restart my Laptop to make it work.在我的情况下,重新启动 docker 守护进程对我来说是不够的,我必须重新启动我的笔记本电脑才能使其工作。

Before spending too much time on any of the other solutions, simply restart Docker and try again.在任何其他解决方案上花费太多时间之前,只需重新启动 Docker 并重试。

Solved the problem for me, using Docker Desktop for Windows on Windows 10.在 Windows 10 上使用适用于 Windows 的 Docker 桌面为我解决了这个问题。

In my case, firewall was the issue.就我而言,防火墙是问题所在。 Disabling it for the moment solved the issue.暂时禁用它解决了这个问题。 I use nftables .我使用nftables Stopping the service did the trick.停止服务成功了。

sudo systemctl stop nftables.service 

On my system ( macOS High Sierra 10.13.6 with Docker 2.1.0.1 ) this was due to a corporate proxy.在我的系统(带有Docker 2.1.0.1 macOS High Sierra 10.13.6 )上,这是由于公司代理造成的。

I solved this by two steps:我通过两个步骤解决了这个问题:

  1. Manually configure proxy settings in Preferences>ProxiesPreferences>Proxies手动配置代理设置
  2. Add the same settings to your config.json inside ~/.docker/config.json like:将相同的设置添加到~/.docker/config.json例如:

     "proxies": { "default": { "httpProxy": "MYPROXY", "httpsProxy": "MYPROXY", "noProxy": "MYPROXYWHITELIST" } }

In my case, since my containers were in a cloud environment the MTU of the interfaces were not usual 1500 and was like 1450, so I had to configure my docker daemon to set the MTU to 1450 for containers.就我而言,由于我的容器在云环境中,因此接口的 MTU 不是通常的 1500,而是 1450,因此我必须配置我的 docker 守护程序以将容器的 MTU 设置为 1450。

{  
"mtu": 1454 
}

look at this : https://mlohr.com/docker-mtu/看看这个: https : //mlohr.com/docker-mtu/

I have dnsmasq in my system for dns resolution that had the nameservers to resolve the URL.我的系统中有dnsmasq用于 dns 解析,它具有解析 URL 的名称服务器。 Docker copies /etc/resolv.conf of the host system as it is into the container's /etc/resolv.conf and thus didn't have the right nameservers. Docker 将主机系统的/etc/resolv.conf复制到容器的/etc/resolv.conf ,因此没有正确的名称服务器。 From docs :文档

By default, a container inherits the DNS settings of the host, as defined in the /etc/resolv.conf configuration file.默认情况下,容器继承主机的 DNS 设置,如/etc/resolv.conf配置文件中所定义。

Adding the nameservers in /etc/resolv.conf of the host fixed the issue.在主机的/etc/resolv.conf中添加名称服务器解决了该问题。

With the recent updates, the following line in ( /etc/docker/daemon.json ) was the cause of the issue:随着最近的更新,( /etc/docker/daemon.json )中的以下行是问题的原因:

{
    "bridge": "none"
}

Remove it, and restart the docker service with: sudo systemctl restart docker删除它,并使用以下sudo systemctl restart docker服务: sudo systemctl restart docker

OS ( Ubuntu 20.04.3 LTS ) and Docker ( version 20.10.11, build dea9396 )操作系统( Ubuntu 20.04.3 LTS )和 Docker( version 20.10.11, build dea9396

暂无
暂无

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

相关问题 docker build失败,无法解析&#39;archive.ubuntu.com&#39; - docker build failing with Could not resolve 'archive.ubuntu.com' "<i>In Docker, apt-get install fails with &quot;Failed to fetch http:\/\/archive.ubuntu.com\/ ... 404 Not Found&quot; errors.<\/i>在 Docker 中,apt-get install 失败并出现“无法获取 http:\/\/archive.ubuntu.com\/ ... 404 Not Found”错误。<\/b> <i>Why?<\/i>为什么?<\/b> <i>How can we get past it?<\/i>我们怎样才能过去呢?<\/b>" - In Docker, apt-get install fails with "Failed to fetch http://archive.ubuntu.com/ ... 404 Not Found" errors. Why? How can we get past it? Docker ubuntu容器可以ping archive.ubuntu.com但不能apt-get - Docker ubuntu container can ping archive.ubuntu.com but not apt-get Ubuntu 16.04: Error "Could not connect to archive.ubuntu.com:80 (91.189.88.152)" while running "apt-get update " command in a dockerfile - Ubuntu 16.04 : Error "Could not connect to archive.ubuntu.com:80 (91.189.88.152)" while running "apt-get update " command in a dockerfile Docker build fails to fetch packages from archive.ubuntu.com inside bash script used in Dockerfile - Docker build fails to fetch packages from archive.ubuntu.com inside bash script used in Dockerfile Docker构建在apt-get中失败 - Docker build fails in apt-get apt-get install apt-transport-https在Docker中失败 - apt-get install apt-transport-https fails in Docker 我如何在docker ubuntu contains上执行&#39;apt-get install&#39;? - How could I execute 'apt-get install' on docker ubuntu contain? Ubuntu 22 基础 docker 映像上的 apt-get 更新失败 - apt-get update fails on Ubuntu 22 base docker image apt-get在基于Debian和Ubuntu的Docker映像中失败 - apt-get fails in Debian and Ubuntu based docker images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM