【问题标题】:Why does Docker build struggle to locate so many Python modules?为什么 Docker build 很难找到这么多 Python 模块?
【发布时间】:2021-10-20 09:54:55
【问题描述】:

在我的 docker 构建过程中,我不断看到一条错误消息,在 docker 尝试安装我的 requirements.txt 文件的部分,上面写着 Module Not Found。但是,它说找不到的模块版本有一半在可用版本列表中。

我一直在尝试旧版本,最终我会找到一个可行的,但同样的事情会在下一个包中发生。尝试构建一个肯定不可能正确的容器需要花费数小时。我假设这可能与包可能不会在容器的不同层之间传递有关,但我是 Docker 新手,所以无法判断这是否是问题或如何解决。

我正在使用 WSL2 (Ubuntu 20.04) 在 Python 中构建一个烧瓶应用程序(从技术上讲,它是一个 Plotly Dash 应用程序,但我读过它的工作原理与烧瓶相同)。我有一台 Windows 10 机器,并且已确保将 Docker 设置为与 WSL2 和 Ubuntu 兼容。我已经粘贴了我在下面遇到的许多错误中的一些,以及我当前的 requirements.txt、Dockerfile 和文件结构。

我想知道如何防止此消息不断出现以及如何知道哪个软件包与 Docker 兼容?任何帮助将不胜感激。

我看到的一些错误消息是:

ERROR: Could not find a version that satisfies the requirement pandas==1.0.5 (from versions: 0.1, 0.2, 0.3.0, 0.4.0, 
0.4.1, 0.4.2, 0.4.3, 0.5.0, 0.6.0, 0.6.1, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8.0, 0.8.1, 0.9.0, 0.9.1, 0.10.0, 0.10.1, 
0.11.0, 0.12.0, 0.13.0, 0.13.1, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.15.2, 0.16.0, 0.16.1, 0.16.2, 0.17.0, 0.17.1, 
0.18.0, 0.18.1, 0.19.0, 0.19.1, 0.19.2, 0.20.0, 0.20.1, 0.20.2, 0.20.3, 0.21.0, 0.21.1, 0.22.0, 0.23.0, 0.23.1, 
0.23.2, 0.23.3, 0.23.4, 0.24.0, 0.24.1, 0.24.2, 0.25.0, 0.25.1, 0.25.2, 0.25.3, 1.0.0, 1.0.1, 1.0.2, 1.0.3, 
1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5)
#12 50.80 ERROR: No matching distribution found for pandas==1.0.5

或者:

#12 17.32 ERROR: Could not find a version that satisfies the requirement pandas==1.3.3 (from versions: 0.1, 0.2, 0.3.0, 
0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.5.0, 0.6.0, 0.6.1, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8.0, 0.8.1, 0.9.0, 0.9.1, 0.10.0, 
0.10.1, 0.11.0, 0.12.0, 0.13.0, 0.13.1, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.15.2, 0.16.0, 0.16.1, 0.16.2, 0.17.0, 
0.17.1, 0.18.0, 0.18.1, 0.19.0, 0.19.1, 0.19.2, 0.20.0, 0.20.1, 0.20.2, 0.20.3, 0.21.0, 0.21.1, 0.22.0, 0.23.0, 
0.23.1, 0.23.2, 0.23.3, 0.23.4, 0.24.0, 0.24.1, 0.24.2, 0.25.0, 0.25.1, 0.25.2, 0.25.3, 1.0.0, 1.0.1, 1.0.2, 1.0.3, 
1.0.4, 1.0.5, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5)

或者:

#12 108.3 ERROR: Could not find a version that satisfies the requirement numpy==1.18.5 (from versions: 1.3.0, 1.4.1, 
1.5.0, 1.5.1, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.1, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0.post2, 
1.10.1, 1.10.2, 1.10.4, 1.11.0, 1.11.1, 1.11.2, 1.11.3, 1.12.0, 1.12.1, 1.13.0rc1, 1.13.0rc2, 1.13.0, 1.13.1, 1.13.3, 
1.14.0rc1, 1.14.0, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.14.5, 1.14.6, 1.15.0rc1, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 
1.15.4, 1.16.0rc1, 1.16.0rc2, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.16.6, 1.17.0rc1, 1.17.0rc2, 1.17.0, 
1.17.1, 1.17.2, 1.17.3, 1.17.4, 1.17.5, 1.18.0rc1, 1.18.0, 1.18.1, 1.18.2, 1.18.3, 1.18.4, 1.18.5, 1.19.0rc1, 
1.19.0rc2, 1.19.0, 1.19.1, 1.19.2, 1.19.3, 1.19.4, 1.19.5)
#12 108.3 ERROR: No matching distribution found for numpy==1.18.5

回滚一些包后的当前 requirements.txt,但仍然没有找到一些模块:

Brotli==1.0.9
dash==2.0.0
dash-bootstrap-components==0.13.1
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-table==5.0.0
Flask==2.0.1
Flask-Compress==1.10.1
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==1.1.1
numpy==1.17.2
pandas==1.0.5
plotly==5.3.1
python-dateutil==2.8.2
pytz==2021.3
six==1.16.0
tenacity==8.0.1
Werkzeug==2.0.2

Dockerfile:

ARG APP_IMAGE=python:3.6.1-alpine

FROM $APP_IMAGE AS base

FROM base as builder

##test these out

RUN mkdir /install
WORKDIR /install

COPY requirements.txt /requirements.txt

# RUN pip install flask==2.0.0 ###I've tried running pip install of the packages here, which worked but I still kept hitting the same error for each package
# RUN pip install MarkupSafe==2.0.1
RUN pip install --upgrade pip
RUN python3 -m pip install -r /requirements.txt

FROM base
ENV FLASK_APP index.py
WORKDIR /project
COPY --from=builder /install /usr/local
ADD . /project


ENTRYPOINT ["python3", "flask", "run", "--host=0.0.0.0"]

文件结构:

-apps
   -main.py
-assets
-env
-app.py
-data.csv
-Dockerfile
-index.py #entry point to the application
-requirements.txt

【问题讨论】:

  • 有什么理由使用 python 3.6?是否所有包都使用相同的 python 版本在本地安装?
  • 使用默认的 python 图像而不是 alpine 图像可以获得更好的结果吗?
  • @h4z3 @iain-shelvington 谢谢你们,我结合了你们的答案。通过将代码放入ARG APP_IMAGE=python:3.8,它已经成功运行。如果你们中的一个人想把它作为答案,那么我会将其标记为正确

标签: python docker pip


【解决方案1】:

尝试使用另一个python实例(这是python版本问题,python 3.6不兼容此要求)

我认为python3.8会满足这个要求

【讨论】:

    【解决方案2】:

    Alpine 版本的 Docker 映像是您尝试使用的软件的最紧凑的映像。它是一个 Docker 映像,没有包所依赖的多个 Linux 库和模块。

    如果您担心 Docker 映像的大小并且有存储要求,请尝试使用 Python slim 映像。它比标准版本小,但包含几个 Alpine 没有的依赖项。

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      相关资源
      最近更新 更多