无法将python包下载到Docker镜像上

时间:2017-06-15 08:19:14

标签: python docker dockerfile

我一直关注官方文档here

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

  

重新连接(重试(总计= 4,连接=无,读取=无,重定向=无))连接被“NewConnectionError”断开后(':无法建立新连接:[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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

问题在于解决容器中的DNS问题。要解决此问题,请按照有助于解决泊坞窗容器中的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 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
/ #