Docker容器中无法使用pip install

4

我在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

我尝试使用docker-build和--network=host选项或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))

我不知道为什么会出现这个错误。有人能帮我吗?我正在Python虚拟环境中运行docker-compose build命令。


对于Python3,它被称为pip3。 - Garr Godfrey
代理服务器的作用是什么?您能否从容器和主机内部连接到代理服务器? - Andy Shinn
@AndyShinn,一开始我没有它们,但是它还是给了我同样的错误。然后我在网上搜索并尝试了一下,看到有人建议使用它。因此我使用了它。但它仍然显示相同的错误。 - nzy
2个回答

3

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

{
  "dns": ["myDNS"]
}

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

nmcli dev show | grep 'DNS'

之后我重启了Docker并成功安装了依赖项。


0

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


起初我没有它们,但仍然出现了相同的错误。然后我在互联网上搜索并尝试了一下,看到有人建议使用它们。因此我使用了它们。但即使我已经将它们删除了,仍然会出现相同的错误。 - nzy
我之前无法编辑我的评论。我只是想澄清问题不在于安装pip,而是在Dockerfile中使用pip安装依赖项的问题。 - nzy
1
请从您的配置中删除代理并添加适当的错误,因为现在我看到“无法连接到代理”,这显然是代理不起作用的原因。 - os11k
我已经移除了代理配置并更新了错误。 - nzy
你的容器仍然没有互联网连接。这个容器的主机是什么?很可能你缺少iptables规则。试着在Digital Ocean上获取一个新的droplet,然后只需在那里安装docker,它就应该可以工作了。问题很可能出在你的docker主机上。 - os11k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接