Skip to main content
 首页 » 编程设计

python之无法将python包下载到Docker镜像中

2024年10月01日26lovecherry

我一直在关注官方文档here .

我的Dockerfile、app.py和requirements.txt都和教程里给的一样。
当我尝试使用 docker build -t friendlyhello 构建我的 Docker 镜像时。 当 docker 运行 RUN pip install -r requirements.txt 部分时出现以下错误:

Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/flask/

我已经 checkout 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

请您参考如下方法:

如果您需要更新守护进程以使用不同的 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