在 Docker build 中使用 pip install 时出现 ModuleNotFoundError

ModuleNotFoundError when using pip install in Docker build

所以,我遇到了很多讨论这个特定问题的线程,但无法真正找到适合我的修复方法。

我在我的根目录requirements.txt 文件中总结了以下要求

stripe==2.51.0
bottle==0.12.18

我的 src/app.py 开始为

from bottle import route, run, template, get, post, request, response, static_file
import stripe

# rest of the code I assume isn't relevant

我正在使用 Dockerfile 构建镜像,如下所示:

#  build 1
FROM python:3.8.5 AS builder
COPY requirements.txt .
RUN python -m pip install --user -r requirements.txt

# build 2
FROM python:3.8.5-slim
COPY --from=builder /root/.local/bin /root/.local
COPY ./src .

EXPOSE 8080
ENV PATH=/root/.local:$PATH
CMD [ "python", "app.py" ]

即使我在将根路径添加到最终路径的同时使用了 --user,我仍然会遇到以下错误:

ModuleNotFoundError: No module named 'bottle'

我也 - 我认为 - 在安装我的软件包时使用相同的解释器 运行 所以我真的不知道如何解决这个问题。

任何正确方向的指示将不胜感激。

非常感谢大家!

能够下载

stripe==2.51.0
bottle==0.12.18

使用给定的 docker 文件和 requirements.txt。

将 Dockerfile 和 requirements.txt 放在同一个文件夹中,然后在本地计算机上安装 docker-desktop 和 运行“docker 构建。”来自 cmd 中的那个文件夹,它将提供本地构建 docker 图像。 收到日志:

Sending build context to Docker daemon  3.072kB
Step 1/9 : FROM python:3.8.5 AS builder
3.8.5: Pulling from library/python
d6ff36c9ec48: Pull complete
c958d65b3090: Pull complete
edaf0a6b092f: Pull complete
80931cf68816: Pull complete
7dc5581457b1: Pull complete
87013dc371d5: Pull complete
dbb5b2d86fe3: Pull complete
4cb6f1e38c2d: Pull complete
0b3d7b2fc317: Pull complete
Digest: sha256:d21be681213425a9a0b736f62b218dad823789a5c708543fbad284b0c06f62a5
Status: Downloaded newer image for python:3.8.5
 ---> 62aa40094bb1
Step 2/9 : COPY requirements.txt .
 ---> e6ea6fe42122
Step 3/9 : RUN python -m pip install --user -r requirements.txt
 ---> Running in b9ea44bb9f44
Collecting stripe==2.51.0
  Downloading stripe-2.51.0-py2.py3-none-any.whl (203 kB)
Collecting bottle==0.12.18
  Downloading bottle-0.12.18-py3-none-any.whl (89 kB)
Collecting requests>=2.20; python_version >= "3.0"
  Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
Collecting idna<3,>=2.5
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting chardet<4,>=3.0.2
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Downloading certifi-2020.6.20-py2.py3-none-any.whl (156 kB)

也尝试将 docker 文件更新为 Dockerfile

# build 2
FROM python:3.8.5-slim
ADD requirements.txt /
ENV PATH=/root/.local/bin:$PATH
RUN pip install --user -r requirements.txt
COPY ./src .   #### 

EXPOSE 8080
ENV PATH=/root/.local:$PATH ###not sure of the use####
CMD [ "python", "app.py" ]

进行这些更改:

COPY --from=builder /root/.local /root/.local
ENV PATH=/root/.local/bin:$PATH

建议:使用slim-buster,你的Dockerfile可以这样结束:

FROM python:3.8.5-slim-buster AS base

FROM base AS builder
COPY requirements.txt .
RUN python -m pip install --user -r requirements.txt

FROM base AS release
COPY --from=builder /root/.local /root/.local
COPY ./src .
EXPOSE 8080
ENV PATH=/root/.local/bin:$PATH
CMD [ "python", "app.py" ]