pip install 在 docker 容器内不工作

pip install not working inside docker container

我在 Flask 中创建了一个小项目,我正在尝试使用 docker 部署它。为此,我创建了如下所示的 Dockerfile:

FROM python:3.6-stretch

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app 
ADD . /app

# Install the dependencies
RUN pip install --user -r requirements.txt

# run the command to start uWSGI
CMD ["uwsgi", "app.ini"]

我的 requirements.txt 文件包含以下内容:

#click==7.1.1
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
uWSGI==2.0.18
Werkzeug==1.0.1

我尝试使用带有 --network=host 选项的 docker-build 以及 docker-compose 来构建我的项目,但没有成功。我收到以下错误:

    WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fac273160f0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/flask/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fac27316588>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/flask/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fac27316828>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/flask/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fac273166a0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/flask/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fac273168d0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',)': /simple/flask/
ERROR: Could not find a version that satisfies the requirement Flask==1.1.2 (from -r requirements.txt (line 2)) (from versions: none)
ERROR: No matching distribution found for Flask==1.1.2 (from -r requirements.txt (line 2))

我不知道为什么会收到此错误。有人可以帮我吗?我是 运行 docker-在 python 虚拟环境中编写构建命令。

您的容器在安装 pip 时无法连接到互联网,因此您需要修复此问题。我假设从您的 docker 文件中删除代理应该可以解决这个问题。

我已经解决了这个问题。我必须创建一个文件 /etc/docker/daemon.json 并添加以下部分:

{
  "dns": ["myDNS"]
}

myDNS 的值是使用以下方法获得的:

nmcli dev show | grep 'DNS'

之后我重新启动 docker 并且能够安装依赖项。