【问题标题】:pip install not working inside docker containerpip install 在 docker 容器中不起作用
【发布时间】:2020-05-01 23:21:41
【问题描述】:

我在烧瓶中创建了一个小项目,我正在尝试使用 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))

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

【问题讨论】:

  • 对于python3它被称为pip3
  • 代理是干什么用的?你能从容器和主机内部连接到代理吗?
  • @AndyShinn 我一开始没有它们,但它给了我同样的错误。然后我搜索了互联网并尝试了它,并看到了使用它的建议。因此我使用了它。它仍然显示相同的错误

标签: python docker pip ubuntu-16.04


【解决方案1】:

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

{
  "dns": ["myDNS"]
}

myDNS 的值是通过以下方式获得的:

nmcli dev show | grep 'DNS'

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 一开始我没有它们,但它给了我同样的错误。然后我搜索了互联网并尝试了它,并看到了使用它的建议。因此我使用了它。然而,即使我删除了它们,我也会遇到同样的错误
    • 我无法编辑我之前的评论。我只是想澄清一下,问题不在于安装 pip,而是在 Dockerfile 中使用 pip 安装依赖项。
    • 请从您的配置中删除代理并添加正确的错误,因为现在我看到“无法连接到代理”,这很明显代理不起作用
    • 我已删除代理配置并更新了错误。
    • 再一次,您的容器没有互联网连接。这个容器的主机是什么?可能您缺少 iptables 规则。尝试在数字海洋上获得新鲜的液滴,然后在那里安装 docker,然后它就可以工作了。问题很可能出在您的 docker 主机上
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 2017-01-31
    • 2013-10-25
    • 2017-05-23
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多