python-3.x Docker的文档:找不到满足要求apturl==0.5.2(&其他)的版本

2w3rbyxf  于 2023-05-19  发布在  Python
关注(0)|答案(2)|浏览(277)

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

$ 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就不能工作。
谢谢终结者的编辑!

yiytaume

yiytaume1#

在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
(Or在多行中):

$ 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

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

$ 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,以确保只有适当的包显示在pip freeze
显然,第一条路,虽然最终对我来说不是正确的,但也不是错误的,只是不适合我的具体情况。

bvjxkvbb

bvjxkvbb2#

如果有人遇到这个问题,请尝试使用另一个Python映像。最好是在引擎盖下使用Debian的那个。
在Dockerfile中,考虑将第一行更改为:

# FROM python:3.8-slim-buster
FROM python:3.8

在我的例子中,我试图使用图像python:3.9-alpine3.17安装TensorFlow,但失败了,直到我将其更改为python:3.9

相关问题