首页 > 解决方案 > Dockerfile 多阶段 Python 诗歌 - 安装

问题描述

我正在尝试对一个简单的脚本(命令行实用程序)进行 dockerize。这是一个有效的Dockerfile:

FROM python:3.9-slim

ARG user=tweaker project=tex-tweak src=tex_tweak

# Non-root user.
RUN useradd -ms /bin/bash "$user"
USER $user
WORKDIR /home/$user
ENV PATH=/home/$user/.local/bin:$PATH

# Source and project files.
RUN mkdir /home/$user/$project
WORKDIR /home/$user/$project
COPY $src ./$src/
COPY pyproject.toml ./
#COPY poetry.lock ./

# Build.
RUN pip install poetry --user && \
    poetry config virtualenvs.create false && \
    poetry install --no-dev && \
    poetry build

CMD ["bash"]

奇怪的是,这就足够了:目标实用程序以某种方式安装到.local/bin; 我不明白为什么。

python:slim图像为 115MB;生成的图像为 174MB。不是不能接受,而是比较臃肿。你想,多阶段构建是有序的。不幸的是,我看不出我应该如何将要领转移到第二阶段。选择性复制.local/binand的想法.local/lib看起来并不特别安全或吸引人。或者也许这就是方式?

或者是否有可能/建议pip install <target>.whl进入第二阶段?

标签: pythondockerfilepython-poetry

解决方案


这是一个示例构建,您可以参考它用于 Poetry 的多阶段:

https://stackoverflow.com/a/64642121/14305096

FROM python:3.9-slim as base

ENV PYTHONFAULTHANDLER=1 \
    PYTHONHASHSEED=random \
    PYTHONUNBUFFERED=1

RUN apt-get update && apt-get install -y gcc libffi-dev g++
WORKDIR /app

FROM base as builder

ENV PIP_DEFAULT_TIMEOUT=100 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1 \
    POETRY_VERSION=1.1.3

RUN pip install "poetry==$POETRY_VERSION"
RUN python -m venv /venv

COPY pyproject.toml poetry.lock ./
RUN . /venv/bin/activate && poetry install --no-dev --no-root

COPY . .
RUN . /venv/bin/activate && poetry build

FROM base as final

COPY --from=builder /venv /venv
COPY --from=builder /app/dist .
COPY docker-entrypoint.sh ./

RUN . /venv/bin/activate && pip install *.whl
CMD ["./docker-entrypoint.sh"]

推荐阅读