【发布时间】: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