【问题标题】:Can't download python packages onto Docker image无法将 python 包下载到 Docker 映像中
【发布时间】:2017-06-15 08:19:14
【问题描述】:

我一直在关注官方文档here

我的Dockerfileapp.pyrequirements.txt与教程中给出的相同。
当我尝试使用 docker build -t friendlyhello . 构建我的 Docker 映像时,当 docker 运行 RUN pip install -r requirements.txt 部分时出现以下错误:

连接被“NewConnectionError(”中断后重试(Retry(total=4, connect=None, read=None, redirect=None)):无法建立新连接:[Errno -2] 名称或服务未知',)': /simple/flask/

我已经查看了this 并尝试了所有建议的解决方案,但似乎都不起作用。

提前致谢。

编辑 1:我的 requirements.txt

Flask Redis

完整的输出

Sending build context to Docker daemon 4.608 kB
Step 1/7 : FROM python:2.7-slim
---> 4ba53c70eb04
Step 2/7 : WORKDIR /app
---> Using cache
---> bebf675fc3bd
Step 3/7 : ADD . /app
---> Using cache
---> 435299812b68
Step 4/7 : RUN pip install -r requirements.txt
---> Running in 97465239272f
Collecting Flask (from -r requirements.txt (line 1))
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after       connection broken by'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f29c8b33910>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/
Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f29c8bb9f90>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/
Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f29c9aa69d0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/
Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f29c9aa6190>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/
Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f29c9aa6510>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/
Could not find a version that satisfies the requirement Flask (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for Flask (from -r requirements.txt (line 1))
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

编辑 2:我发现我的公司使用了一个特定的 DNS,我可以使用它来解析 docker run --dns [DNS] busybox nslookup google.com

【问题讨论】:

  • 请添加您的requirements.txt和完整的输出。
  • 我在我的服务器上尝试过同样的东西,它正在工作。试图了解您的设置,您可以从您的容器 ping 到 google.com 吗?运行这个命令“docker run busybox nslookup google.com”
  • @Here_2_learn 似乎没有按预期工作。我最终收到了nslookup: can't resolve 'google.com' 的消息。
  • @Here_2_learn 请参阅编辑 2。
  • 确保您的公司 dns 位于 /etc/resolv.conf 或配置 docker 守护程序以指向它!

标签: python docker dockerfile


【解决方案1】:

如果您需要更新您的守护程序以使用不同的 DNS 地址,您可以使用以下内容创建(或修改)/etc/docker/daemon.json 文件:

{
  "dns": ["8.8.8.8", "8.8.2.2"]
}

只需将上述 IP 替换为您自己的要求,完成后,您可以在您的守护程序上运行重新加载或重新启动以重新读取文件。

sudo systemctl reload docker

这应该会更改所有新容器的默认 DNS,包括构建时使用的容器。

有关可以在此文件中设置的更多详细信息,请参阅以下链接:https://docs.docker.com/engine/reference/commandline/dockerd/#linux-configuration-file

【讨论】:

  • 我执行了您的指示,但即使我更新了daemon.json 文件,nslookup 也无法解析 google.com。但是,如果我使用--dns &lt;DNS&gt; 手动添加 dns,它可以工作。
【解决方案2】:

问题在于解析容器中的 DNS。要解决这个问题,请按照有助于解析 docker 容器中的 DNS 的过程:

找出您机器中使用的 DNS 服务器:

# nm-tool  |grep DNS
    DNS:             172.24.100.50
    DNS:             10.1.100.50

使用在上述步骤中找到的 DNS IP 再次运行它来解决 DNS 问题:

# docker run --dns 172.24.100.50 busybox nslookup google.com
Server:    172.24.100.50
Address 1: 172.24.100.50 indc01.radisys.com
Name:      google.com
Address 1: 2607:f8b0:4009:80c::200e ord36s01-in-x0e.1e100.net
Address 2: 172.217.4.110 ord36s04-in-f14.1e100.net

要永久解决它,请将以下内容添加到新文件中:

# cat /etc/docker/daemon.json
{
    "dns" : ["172.24.100.50", "8.8.8.8"]
}

有关 Docker DNS 配置的更多信息:https://docs.docker.com/engine/userguide/networking/configure-dns/

重启docker服务并再次检查:

# docker run busybox nslookup google.com
Server:    172.24.100.50
Address 1: 172.24.100.50 indc01.radisys.com
Name:      google.com
Address 1: 2607:f8b0:4009:801::200e ord30s31-in-x0e.1e100.net
Address 2: 172.217.4.238 ord30s31-in-f14.1e100.net

通过运行容器检查:

root@labadmin-VirtualBox:/home/labadmin# docker run -it e02e811dd08f
/ # ping google.com
PING google.com (172.217.4.238): 56 data bytes
64 bytes from 172.217.4.238: seq=0 ttl=47 time=251.506 ms
64 bytes from 172.217.4.238: seq=1 ttl=47 time=245.621 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 245.621/257.113/272.586 ms
/ #

【讨论】:

  • 我执行了您的指示,但即使我更新了daemon.json 文件,nslookup 也无法解析 google.com。
  • 但是,如果我使用--dns &lt;DNS&gt; 手动添加 dns,它就可以正常工作。任何解决此问题的方法,因为我无法将 --dns &lt;DNS&gt; 附加到 docker build 命令。
  • 你重启docker服务了吗??
  • 是的,我做到了..但事实证明这是徒劳的。你能解释一下接受的答案(我的答案)吗?
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 2020-03-23
  • 2014-05-31
  • 2015-11-02
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2022-11-15
相关资源
最近更新 更多