【问题标题】:From Docker's doc: Could not find a version that satisfies the requirement apturl==0.5.2 (& others)来自 Docker 的文档:找不到满足要求的版本 apturl==0.5.2 (& others)
【发布时间】:2021-05-12 00:12:00
【问题描述】:

我目前正在关注 docker's doc 如何使用 python 构建图像: 因此,我的“错误”可以通过遵循文档来复制。 我已经检查并三重检查了我的行为,一切都与文档显示的完全一样。 另外,我在这里查看了很多类似的帖子,但没有一个可以帮助我解决我的问题。 对于那些不想查看链接的人,这就是我所做的: 使用的命令:

$ cd /path/to/python-docker
$ pip3 install Flask
$ pip3 freeze > requirements.txt
$ touch app.py

之后,我们创建dockfile(注意:这是我的dockfile,不是文档):

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
# RUN apt update && apt install -y python3-pip
# RUN pip install --upgrade setuptools
# NOTE: the 2 RUN above are the 2 latest attempts to find a solution by using a RUN line.
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

然后:

$ docker build --tag python-docker .
[+] Building 2.7s (10/10) FINISHED
 => [internal] load build definition from Dockerfile
 => => transferring dockerfile: 203B
 => [internal] load .dockerignore
 => => transferring context: 2B
 => [internal] load metadata for docker.io/library/python:3.8-slim-buster
 => [1/6] FROM docker.io/library/python:3.8-slim-buster
 => [internal] load build context
 => => transferring context: 953B
 => CACHED [2/6] WORKDIR /app
 => [3/6] COPY requirements.txt requirements.txt
 => [4/6] RUN pip3 install -r requirements.txt
 => [5/6] COPY . .
 => [6/6] CMD [ "python3", "-m", "flask", "run", "--host=0.0.0.0"]
 => exporting to image
 => => exporting layers
 => => writing image sha256:8cae92a8fbd6d091ce687b71b31252056944b09760438905b726625831564c4c
 => => naming to docker.io/library/python-docker

以上是假定会发生的结果。这就是得到的:

$ sudo docker build --tag python-docker .
Sending build context to Docker daemon  16.38kB
Step 1/6 : FROM python:3.8-slim-buster
 ---> b426ee0e7642
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> 32f83359c4ca
Step 3/6 : COPY requirements.txt requirements.txt
 ---> Using cache
 ---> 2d84b2a8013a
Step 4/6 : RUN pip3 install -r requirements.txt
 ---> Running in de270828b1b5
ERROR: Could not find a version that satisfies the requirement apturl==0.5.2
ERROR: No matching distribution found for apturl==0.5.2
The command '/bin/sh -c pip3 install -r requirements.txt' returned a non-zero code: 1

虽然我可以从requirements.txt 中删除这一行,但事实是我没有手动编写文件,因此没有理由出错。不管怎样,apturl==0.5.2 并不是唯一一个有类似错误的人,我估计requirements.txt 的内容中大约 40% 会产生同样的错误。 我和我的同事被难住了,看到我们已经看到的类似帖子中提出的解决方案,我们还没有尝试过谈到操纵 DNS,我决定提出这个问题,因为我们不喜欢玩这个在工作场所:D

注意:sudo 只是因为没有 docker 就无法工作。

感谢 TERMINATOR 的编辑!

【问题讨论】:

  • 没有名为 apturl 的 Python 包,有一个具有该名称的 Ubuntu 包。您应该查看重新生成您的 requirements.txt 或手动删除无效的包
  • 你好,这个requirements.txt的生成直接来自Docker的doc。你的意思是docker的文档有问题吗?
  • 不一定是错的,可能只是一些问题,因为您没有从全新安装 Python 开始...尝试相同的步骤,但在全新的虚拟环境中:python3 -m venv foo && . ./foo/bin/activate && pip install Flask && pip freeze > requirements.txt
  • 感谢您的回答@IainShelvington,在使用python3 -m venv python-docker 时,我想这是我应该提出的,因为我对这个“foo”的东西很陌生,我得到一个错误,直接我用这个:apt-get install python3-venv。我 sudoed 了,得到了这个:E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.。恐怕,因为我在工作场所,我不放心触摸任何配置:/
  • 既然你已经安装了 docker,你可以在基础镜像中运行命令,然后手动将 freeze 命令的输出复制到你的 requirements.txt:docker run python:3.8-slim-buster bash -c "pip install Flask && pip freeze"

标签: python python-3.x docker pip dockerfile


【解决方案1】:

在我的问题的 cmets 中 Iain Shelvington 的大力帮助下,找到了解决方案。 问题是$ pip3 install Flask & $ pip3 freeze > requirements.txt 记录了本地安装的所有软件包,不一定是真正需要的软件包。 正如 Iain Shelvington 所说,我requirements.txt 中的包不仅是 pip 包,还有 Ubuntu 包。 然后可能有两条路径: python3 -m venv foo && . ./foo/bin/activate && pip install Flask && pip freeze > requirements.txt
(或多行):

$ python3 -m venv foo
$ . ./foo/bin/activate
$ pip install Flask
$ pip freeze > requirements.txt

以及我之后介绍的另一个 :)

然而,在做路径的第一行时:

$ python3 -m venv python-docker
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

我尝试不使用 sudo。使用 sudo 的尝试如下所示:

$ sudo apt-get install python3-venv
E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem. 

可以理解,由于我在工作场所,我不想更改配置。

谢天谢地,Iain Shelvington 提供了第二条路径。事实上,在 Dockfile 中我有 FROM python:3.8-slim-buster,它允许我这样做:

$ sudo docker run python:3.8-slim-buster bash -c "pip install Flask && pip freeze"
Collecting Flask
  Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting click>=5.1
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1-cp38-cp38-manylinux2010_x86_64.whl (32 kB)
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Werkzeug==1.0.1

最后 6 行是我的“真正”要求。通过将requirements.txt 的内容替换为sudo docker run python:3.8-slim-buster bash -c "pip install Flask && pip freeze" 获得的内容,我可以继续并再次尝试构建我的图像:

$ sudo docker build --tag python-docker .
Sending build context to Docker daemon  15.36kB
Step 1/6 : FROM python:3.8-slim-buster
 ---> b426ee0e7642
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> 32f83359c4ca
Step 3/6 : COPY requirements.txt requirements.txt
 ---> b4c880e0fc47
Step 4/6 : RUN pip3 install -r requirements.txt
 ---> Running in 55b1a05658ba
Collecting click==7.1.2
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting Flask==1.1.2
  Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting itsdangerous==1.1.0
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2==2.11.3
  Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting MarkupSafe==1.1.1
  Downloading MarkupSafe-1.1.1-cp38-cp38-manylinux2010_x86_64.whl (32 kB)
Collecting Werkzeug==1.0.1
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
Removing intermediate container 55b1a05658ba
 ---> a7be3cfb1fbb
Step 5/6 : COPY . .
 ---> acade9a05de9
Step 6/6 : CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
 ---> Running in b1dfdf99b8aa
Removing intermediate container b1dfdf99b8aa
 ---> f6198e9d28f4
Successfully built f6198e9d28f4
Successfully tagged python-docker:latest

最后,成功!

这两条路径是围绕相同的原则构建的:创建或进入一个不受本地包影响的环境,以便pip freeze 可以正常工作并只为我们提供构建映像所需的要求,以及不只是本地安装的所有包。
虽然第一个路径通过虚拟环境执行此操作,但第二个路径使用之前在 Dockfile 中调用的 python 映像,并直接在 python 映像中启动 pip freeze,确保只有适当的包与 @ 一起显示987654335@

显然,第一条道路虽然最终对我来说不是正确的,但也不是错误的,只是不适合我的具体情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2020-02-12
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多