AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 642981
Accepted
Thomas V.
Thomas V.
Asked: 2014-11-09 10:26:39 +0800 CST2014-11-09 10:26:39 +0800 CST 2014-11-09 10:26:39 +0800 CST

Docker 容器无法解析 Ubuntu 14.04 桌面主机上的 DNS

  • 772

我在 Ubuntu 14.04 LTS 上遇到了我的 Docker 容器的问题。Docker 工作了两天,然后突然我失去了容器内的所有网络连接。下面的错误输出最初让我相信这是因为 apt-get 试图通过 IPv6 解析 DNS。

我在我的主机上禁用了 IPv6,但仍然删除了所有图像,提取了基础 ubuntu,但仍然遇到了问题。

我将 /etc/resolve.conf 名称服务器从本地 DNS 服务器更改为 Google 的公共 DNS 服务器(8.8.8.8 和 8.8.4.4),但仍然没有运气。我还在 /etc/default/docker 的 DOCKER_OPTS 中将 DNS 设置为 Google 并重新启动了 docker。

我也试过拉coreos,yum也无法解析DNS。

这很奇怪,因为虽然 DNS 不起作用,但当我 ping apt-get 无法解析的相同更新服务器时,我仍然得到响应。

我不在代理后面,我在一个非常标准的本地网络上,这个版本的 Ubuntu 是最新的和新鲜的(我两天前安装了更接近 docker)。

我已经通过关于 stackoverflow 和 github 问题的其他帖子彻底研究了这一点,但没有找到任何解决方案。我不知道如何解决这个问题,有人可以帮忙吗?

错误信息

➜  arthouse git:(docker) ✗ docker build --no-cache .
Sending build context to Docker daemon 51.03 MB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:14.04
 ---> 5506de2b643b
Step 1 : RUN apt-get update
 ---> Running in 845ae6abd1e0
Err http://archive.ubuntu.com trusty InRelease
Err http://archive.ubuntu.com trusty-updates InRelease
Err http://archive.ubuntu.com trusty-security InRelease   
Err http://archive.ubuntu.com trusty-proposed InRelease  
Err http://archive.ubuntu.com trusty Release.gpg
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
Err http://archive.ubuntu.com trusty-updates Release.gpg
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
Err http://archive.ubuntu.com trusty-security Release.gpg
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
Err http://archive.ubuntu.com trusty-proposed Release.gpg
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease  
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/InRelease  
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/InRelease  
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-proposed/InRelease  
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/Release.gpg  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/Release.gpg  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/Release.gpg  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-proposed/Release.gpg  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.

容器 IFCONFIG/PING

➜  code  docker run -it ubuntu /bin/bash
root@7bc182bf87bb:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:04  
          inet addr:172.17.0.4  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:4/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:738 (738.0 B)  TX bytes:648 (648.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

root@7bc182bf87bb:/# ping google.com
PING google.com (74.125.226.0) 56(84) bytes of data.
64 bytes from lga15s42-in-f0.1e100.net (74.125.226.0): icmp_seq=1 ttl=56 time=12.3 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 12.367/12.367/12.367/0.000 ms
root@7bc182bf87bb:/# 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=44 time=21.8 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=44 time=21.7 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=44 time=21.7 ms

此外,当我强制 IPv4 时,apt-get update 失败:

root@6d925cdf84ad:/# sudo apt-get update -o Acquire::ForceIPv4=true
Err http://archive.ubuntu.com trusty InRelease

Err http://archive.ubuntu.com trusty-updates InRelease

Err http://archive.ubuntu.com trusty-security InRelease

Err http://archive.ubuntu.com trusty-proposed InRelease

Err http://archive.ubuntu.com trusty Release.gpg
  Unable to connect to archive.ubuntu.com:http: [IP: 91.189.88.153 80]
Err http://archive.ubuntu.com trusty-updates Release.gpg
  Unable to connect to archive.ubuntu.com:http: [IP: 91.189.88.153 80]
Err http://archive.ubuntu.com trusty-security Release.gpg
  Unable to connect to archive.ubuntu.com:http: [IP: 91.189.88.153 80]
Err http://archive.ubuntu.com trusty-proposed Release.gpg
  Unable to connect to archive.ubuntu.com:http: [IP: 91.189.88.153 80]
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/InRelease  
ubuntu
  • 13 13 个回答
  • 193348 Views

13 个回答

  • Voted
  1. Best Answer
    Thomas V.
    2014-11-09T11:09:56+08:002014-11-09T11:09:56+08:00

    哇,我在 github 上找到了一个帖子,解决了我的问题。

    在 Steve K. 指出这实际上不是 DNS 问题而是连接问题之后,我能够在 github 上找到一篇描述如何解决此问题的帖子。

    显然 docker0 网桥被挂了。安装 bridge-utils 并运行以下命令使我的 Docker 正常工作:

    apt-get install bridge-utils
    pkill docker
    iptables -t nat -F
    ifconfig docker0 down
    brctl delbr docker0
    service docker restart
    
    • 86
  2. wisbucky
    2018-06-28T15:06:07+08:002018-06-28T15:06:07+08:00

    如果是 DNS 解析器问题,解决方法如下:

    首先要检查的是cat /etc/resolv.conf在docker 容器中运行。如果它有一个无效的 DNS 服务器,例如nameserver 127.0.x.x,那么容器将无法将域名解析为 ip 地址,因此ping google.com会失败。

    要检查的第二件事是cat /etc/resolv.conf在主机上运行。Docker 基本上/etc/resolv.conf每次启动容器时都会将主机复制到容器中。因此,如果主机/etc/resolv.conf错了,那么 docker 容器也会错。

    如果您发现主机/etc/resolv.conf错误,那么您有 2 个选择:

    1. 在 daemon.json 中硬编码 DNS 服务器。如果您希望 DNS 服务器发生变化,这很容易,但并不理想。

    2. 修复主机的/etc/resolv.conf. 这有点棘手,但它是动态生成的,并且您没有对 DNS 服务器进行硬编码。


    1. docker daemon.json 中硬编码 DNS 服务器

    • 编辑/etc/docker/daemon.json

      {
          "dns": ["10.1.2.3", "8.8.8.8"]
      }
      
    • 重新启动 docker 守护程序以使这些更改生效:
      sudo systemctl restart docker

    • 现在,当您运行/启动容器时,docker 将/etc/resolv.conf使用来自daemon.json.


    2.修复主机的/etc/resolv.conf

    A. Ubuntu 16.04 及更早版本

    • 对于 Ubuntu 16.04 及更早版本,/etc/resolv.conf由 NetworkManager 动态生成。

    • 注释掉中的行dns=dnsmasq(带有#) /etc/NetworkManager/NetworkManager.conf

    • 重新启动 NetworkManager 以重新生成/etc/resolv.conf:
      sudo systemctl restart network-manager

    • 在主机上验证:cat /etc/resolv.conf

    B. Ubuntu 18.04 及更高版本

    • Ubuntu 18.04 改为使用systemd-resolved生成/etc/resolv.conf. 现在默认情况下它使用本地 DNS 缓存 127.0.0.53。这在容器中不起作用,因此 Docker 将默认使用 Google 的 8.8.8.8 DNS 服务器,这可能会破坏防火墙后面的人。

    • /etc/resolv.conf实际上是一个符号链接 ( ls -l /etc/resolv.conf),它在 Ubuntu 18.04 中默认指向/run/systemd/resolve/stub-resolv.conf(127.0.0.53)。

    • 只需将符号链接更改为指向/run/systemd/resolve/resolv.conf,其中列出了真实的 DNS 服务器:
      sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

    • 在主机上验证:cat /etc/resolv.conf

    现在你应该/etc/resolv.conf在主机上有一个有效的 docker 复制到容器中。

    • 28
  3. David 'the bald ginger'
    2016-07-27T01:43:38+08:002016-07-27T01:43:38+08:00

    试图为我也遇到的问题增加额外的价值;有一个替代答案:

    我的网络与办公室相关,并且 Google DNS 设置被阻止,因此容器可以 ping IP 地址但不能 ping 域名。

    我的主人/etc/resolv.conf原本的样子;

    #Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 127.0.1.1
    search companyDomain.co.za
    

    这是由于网络管理器对 DNS 服务器详细信息进行了某种屏蔽。

    不幸的是,根据docker 手册, docker 在构建容器的 resolv.conf 时会过滤掉任何 localhost IP 地址,并将其替换为 Google 的 DNS IP。在我的情况下,这导致域名被禁止使用。

    我不得不:

    • 将my 重置/etc/default/docker为默认值,以便容器使用我主机的 resolv.conf 内容。
    • 编辑/etc/NetworkManager/NetworManager.conf并注释掉该行dns=dnsmasq。这样 NM 可以指定实际的 DNS IP 地址而不是 127.0.0.1。
    • 用 重新启动 NM sudo service network-manager restart。
    • 重新启动 docker 服务sudo service docker restart。

    例如,运行一个容器将允许它执行apt-get update/upgrade。

    • 13
  4. tryer3000
    2015-05-27T21:48:11+08:002015-05-27T21:48:11+08:00

    Docker 官方文档提供了配置 DNS 服务器以供 Docker 使用的工具

    1. 打开/etc/default/docker文件进行编辑:

      sudo nano /etc/default/docker
      
    2. 为 Docker 添加设置:

      DOCKER_OPTS="--dns 8.8.8.8"
      
    3. 替换8.8.8.8为本地 DNS 服务器,例如192.168.1.1. 您还可以指定多个 DNS 服务器。用空格分隔它们,例如:

      --dns 8.8.8.8 --dns 192.168.1.1
      

      警告:如果您在连接到各种网络的笔记本电脑上执行此操作,请确保选择公共 DNS 服务器。

      PS:nm-tool可以用来查看本地主机DNS服务器

    4. 保存并关闭文件。

    5. 重新启动 Docker 守护程序。

      sudo service docker restart
      
    • 9
  5. user9565
    2014-11-09T10:38:26+08:002014-11-09T10:38:26+08:00

    你的错误在这里:

     Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8c01::19).
     connect (101: Network is unreachable) [IP: 2001:67c:1360:8c01::19 80]
    

    这不是 DNS 的错误,而是您的系统正在尝试连接到 IPv6 主机并失败。大概是因为您的主机上没有 IPv6 访问权限。IPv6 地址的实际查找成功。(ubuntu 镜像/存档可在 IPv6 和 IPv4 上使用。您只是不幸遇到了 IPv6,因为您的系统认为它应该可以工作。)

    您应该通过安装 miredo来解决这个问题,或者重试直到您遇到 IPv4 镜像。

    在这里要再次认识到的重要一点是 DNS 不是罪魁祸首,正如您可以通过自己的 ping 测试看到的那样。

    • 8
  6. esengineer
    2015-06-23T17:42:49+08:002015-06-23T17:42:49+08:00

    对于在使用 boot2docker 时来到这里的其他读者,这是我修复的方法。事实上,上面的答案为我指明了正确的方向。

    基本上,由于某种原因,boot2docker 中的容器无法解析主机名。

    所以我只是重新启动了 boot2docker 并启动了容器。现在主机名可以再次正确解析。

    我想问题是在连接主机上的网络时启动 boot2docker,这导致 boot2docker 启动并进入非工作状态。

    • 1
  7. rgov
    2020-07-11T18:42:48+08:002020-07-11T18:42:48+08:00

    当我允许 Ubuntu 安装程序安装 Docker snap 包时,我遇到了这个问题。当我放弃它并切换到官方 Docker 包时,问题自行解决了。

    sudo snap remove docker
    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    
    • 1
  8. speedplane
    2016-08-12T14:16:34+08:002016-08-12T14:16:34+08:00

    我在 Windows 上遇到了同样的问题。这个命令让它为我工作:docker-machine restart

    • 0
  9. michi.0x5d
    2017-08-11T02:54:57+08:002017-08-11T02:54:57+08:00

    有一个类似的问题,但在用户定义的网络中的容器之间的名称解析似乎有点不稳定。有些人无法解决像你这样的问题。

    问题是移动的 /var/lib/docker。由于空间原因,它是通过 nfs 安装的。添加本地文件系统并将文件移动到那里可以解决问题。

    • 0
  10. Nolwennig
    2018-06-08T07:30:45+08:002018-06-08T07:30:45+08:00

    重启 Debian9 上的 Docker 守护进程

    service docker restart

    连接和网络工作正常

    • 0

相关问题

  • 无法通过 Ubuntu VPN 访问外部网络

  • ubuntu apt-get upgrade - 如何在 shell 中单击确定?

  • VirtualBox 上 Ubuntu 的访客优化技巧 [关闭]

  • 外部硬盘上的 virtualbox 虚拟硬盘驱动器(Vista 主机上的 ubuntu 客户机)

  • 如何在 Ubuntu 上挂载 LVM 分区?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve