如何在docker中使用诗歌?



如何在我的图像中安装诗歌?(我应该使用pip吗?)

我应该用哪个版本的诗?

我需要虚拟环境吗?

有很多例子和观点提供了不同的解决方案。

TL;DR

使用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将导致不确定的构建,因为安装程序将始终安装最新版本-这可能会引入破坏性更改

虚拟环境(virtualenv)

我需要虚拟环境吗?

是的,你需要稍微配置一下。

ENV POETRY_CACHE_DIR=/opt/.cache

这样做的原因有点离题:

默认情况下,poetry在$HOME/中创建一个虚拟环境。缓存/pypoetry/virtualenvs以将系统解释器与应用程序隔离。这是大多数开发场景所期望的行为。当使用容器时,$HOME变量可能会被某些运行时更改,因此在独立目录中创建虚拟环境可以解决可能出现的任何可再现性问题。

让一切都在一起

要在docker映像中使用诗歌,您需要:

  1. 安装你想要的诗歌版本
  2. 配置虚拟环境位置
  3. 安装依赖项
  4. 使用poetry run python ...运行应用程序

工作示例:

这是一个用诗歌管理的最小的flask项目。

您可以将这些内容复制到您的机器上进行测试(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 <someone@example.com>"]
[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

我提供了一个Python Docker Image with Poetry安装,可以用作您自己的图像的构建器基础。最近三个诗歌和Python版本的图片可用:https://github.com/max-pfeiffer/python-poetry

我还提供了其他一些包含前面提到的实践(虚拟环境、多阶段构建)的映像。在这里,你还可以找到一些关于如何使用诗歌和为web应用程序构建图像的其他示例:

  • https://github.com/max-pfeiffer/uvicorn-poetry
  • https://github.com/max-pfeiffer/uvicorn-gunicorn-poetry

相关内容

  • 没有找到相关文章

最新更新