首页 > 解决方案 > 来自 Docker 的文档:找不到满足要求的版本 apturl==0.5.2 (& others)

问题描述

我目前正在关注docker 的关于如何使用以下方法构建图像的文档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不​​是唯一一个有类似错误的,我估计大约 40% 的内容requirements.txt 会产生相同的错误。我和我的同事被难住了,看到在类似帖子中提出的我们尚未尝试过的类似帖子中提出的解决方案谈到了操纵 DNS,我决定提出这个问题,因为我们不喜欢玩这个在工作场所:D

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

感谢终结者的编辑!

标签: pythonpython-3.xdockerpipdockerfile

解决方案


在 Iain Shelvington 在我的问题评论中的大力帮助下,找到了解决方案。问题是$ pip3 install Flask&$ pip3 freeze > requirements.txt 记录了所有安装在本地的包,不一定是那些真正需要的包。正如 Iain Shelvington 所说,myrequirements.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.txtsudo 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 映像,并pip freeze直接在 python 映像中启动,确保只有适当的包与pip freeze

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


推荐阅读