使用 pip 安装诗歌,配置 virtualenv,安装依赖项,运行您的应用程序。
FROM python:3.10
# Configure Poetry
ENV POETRY_VERSION=1.2.0
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VENV=/opt/poetry-venv
ENV POETRY_CACHE_DIR=/opt/.cache
# Install poetry separated from system interpreter
RUN python3 -m venv $POETRY_VENV \
&& $POETRY_VENV/bin/pip install -U pip setuptools \
&& $POETRY_VENV/bin/pip install poetry==${POETRY_VERSION}
# Add `poetry` to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"
WORKDIR /app
# Install dependencies
COPY poetry.lock pyproject.toml ./
RUN poetry install
# Run your app
COPY . /app
CMD [ "poetry", "run", "python", "-c", "print('Hello, World!')" ]
如何在我的图像中添加诗歌? (我应该使用
吗?)pip
pip
您应该使用 pip 安装诗歌。但你需要将它与系统解释器和项目的虚拟环境隔离。
为了最大程度地控制 CI 环境,完全支持使用 pip 安装...提供最佳的调试体验,并让您使用最少的外部工具。
ENV POETRY_VERSION=1.2.0
ENV POETRY_VENV=/opt/poetry-venv
# Install poetry separated from system interpreter
RUN python3 -m venv $POETRY_VENV \
&& $POETRY_VENV/bin/pip install -U pip setuptools \
&& $POETRY_VENV/bin/pip install poetry==${POETRY_VERSION}
# Add `poetry` to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"
我应该使用哪个版本的诗歌?
在安装中明确指定最新的稳定版本。
忘记指定
POETRY_VERSION
将导致不确定的构建,因为安装程序将始终安装最新版本 - 这可能会引入重大更改
我需要虚拟环境吗?
是的,你需要稍微配置一下。
ENV POETRY_CACHE_DIR=/opt/.cache
其原因有点偏离主题:
默认情况下,poetry 在 $HOME/.cache/pypoetry/virtualenvs 中创建一个虚拟环境,以将系统解释器与您的应用程序隔离。这是大多数开发场景所期望的行为。使用容器时,$HOME 变量可能会被某些运行时更改,因此在独立目录中创建虚拟环境可以解决可能出现的任何再现性问题。
要在 docker 镜像中使用诗歌,您需要:
这是一个用诗歌管理的最小烧瓶项目。
您可以将这些内容复制到您的机器上进行测试(预计
poerty.lock
)
python-poetry-docker/
|- Dockerfile
|- app.py
|- pyproject.toml
|- poetry.lock
Dockerfile
FROM python:3.10 as python-base
# https://python-poetry.org/docs#ci-recommendations
ENV POETRY_VERSION=1.2.0
ENV POETRY_HOME=/opt/poetry
ENV POETRY_VENV=/opt/poetry-venv
# Tell Poetry where to place its cache and virtual environment
ENV POETRY_CACHE_DIR=/opt/.cache
# Create stage for Poetry installation
FROM python-base as poetry-base
# Creating a virtual environment just for poetry and install it with pip
RUN python3 -m venv $POETRY_VENV \
&& $POETRY_VENV/bin/pip install -U pip setuptools \
&& $POETRY_VENV/bin/pip install poetry==${POETRY_VERSION}
# Create a new stage from the base python image
FROM python-base as example-app
# Copy Poetry to app image
COPY --from=poetry-base ${POETRY_VENV} ${POETRY_VENV}
# Add Poetry to PATH
ENV PATH="${PATH}:${POETRY_VENV}/bin"
WORKDIR /app
# Copy Dependencies
COPY poetry.lock pyproject.toml ./
# [OPTIONAL] Validate the project is properly configured
RUN poetry check
# Install Dependencies
RUN poetry install --no-interaction --no-cache --without dev
# Copy Application
COPY . /app
# Run Application
EXPOSE 5000
CMD [ "poetry", "run", "python", "-m", "flask", "run", "--host=0.0.0.0" ]
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker!'
pyproject.toml
[tool.poetry]
name = "python-poetry-docker-example"
version = "0.1.0"
description = ""
authors = ["Someone <[email protected]>"]
[tool.poetry.dependencies]
python = "^3.10"
Flask = "^2.1.2"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
poetry.lock
[[package]]
name = "click"
version = "8.1.3"
description = "Composable command line interface toolkit"
category = "main"
optional = false
python-versions = ">=3.7"
[package.dependencies]
... more lines ommitted
完整内容在要点。
我更喜欢使用多阶段构建,这样我就可以摆脱实际发布图像中的诗意并保持这些图像的苗条。
FROM python:3.10-slim AS builder
ENV POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_NO_INTERACTION=1
# to run poetry directly as soon as it's installed
ENV PATH="$POETRY_HOME/bin:$PATH"
# install poetry
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& curl -sSL https://install.python-poetry.org | python3 -
WORKDIR /app
# copy only pyproject.toml and poetry.lock file nothing else here
COPY poetry.lock pyproject.toml ./
# this will create the folder /app/.venv (might need adjustment depending on which poetry version you are using)
RUN poetry install --no-root --no-ansi --without dev
# ---------------------------------------------------------------------
FROM python:3.10-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PATH="/app/.venv/bin:$PATH"
WORKDIR /app
# copy the venv folder from builder image
COPY --from=builder /app/.venv ./.venv
我提供了一个安装了 Poetry 的 Python Docker 镜像,它可以用作您自己的镜像的构建器基础。最新三个 Poetry 和 Python 版本的图像可用:https://github.com/max-pfeiffer/python-poetry
我还提供了一些其他图像,其中包含已经提到的实践(虚拟环境、多阶段构建)。在那里您还可以找到一些有关如何利用 Poetry 和为 Web 应用程序构建图像的其他示例:
只是上述任何想法的补充。
您可以将诗歌安装分为两个步骤。
为了避免源文件更改破坏第 3 方依赖项的 RUN 层缓存。
COPY pyproject.toml poetry.lock /app
RUN poetry install --only main --no-root --no-directory
COPY . /app
RUN poetry install --only main