AberSheeran
Aber Sheeran

使用多阶段构建优化 Python 依赖安装

起笔自
所属文集: Docker学习笔记
共计 858 个字符
落笔于

在最终镜像里包含 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

# ......
如果你觉得本文值得,不妨赏杯茶
Python Docker IMAGE 的挑选
没有下一篇