简体   繁体   English

Dockerfile Multistage Python Poetry - 安装

[英]Dockerfile Multistage Python Poetry - install

I'm trying to dockerize a simple script (a command line utility).我正在尝试 dockerize 一个简单的脚本(一个命令行实用程序)。 Here is a working Dockerfile:这是一个有效的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"]

Curiously, this is enough: the target utility somehow gets installed into .local/bin ;奇怪的是,这就足够了:目标实用程序以某种方式安装到.local/bin I can't see why.我不明白为什么。

python:slim image is 115MB; python:slim图像为 115MB; the resulting image is 174MB.生成的图像为 174MB。 Not unacceptable, but relatively bloated.不是不能接受,而是比较臃肿。 Multistage build is in order, u suppose.你想,多阶段构建是有序的。 Unfortunately, I can't see how I should move the essentials into the second stage.不幸的是,我看不出应该如何将必需品移至第二阶段。 The idea of selective copying of the .local/bin and .local/lib does not look particularly safe or inviting.选择性复制.local/bin.local/lib的想法看起来并不特别安全或吸引人。 Or maybe this is the way?或者这就是方法?

Or is it possible/advisable to pip install <target>.whl into the second stage?或者是否可以/建议将pip install <target>.whl导入第二阶段?

Here is an example build that you can reference for multi-stage with Poetry:这是一个示例构建,您可以参考 Poetry 的多阶段构建:

https://stackoverflow.com/a/64642121/14305096 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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM