在最终镜像里包含 Python 的包管理器是不明智的。
Python-Poetry
FROM python:3.10 as requirements
WORKDIR /src
RUN python -m pip install -U poetry
COPY pyproject.toml pyproject.toml
COPY poetry.lock poetry.lock
RUN poetry export -f requirements.txt --without-hashes -o /src/requirements.txt
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE 1
COPY --from=requirements /src/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ......
PDM
FROM python:3.10 as requirements
WORKDIR /src
RUN python -m pip install -U pdm
RUN pdm config python.use_venv False
COPY pyproject.toml pyproject.toml
COPY pdm.lock pdm.lock
RUN pdm export --production -f requirements -o requirements.txt --without-hashes
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE 1
COPY --from=requirements /src/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ......