如何使用诗歌与 Docker ?

0ve6wy6x  于 2023-01-04  发布在  Docker
关注(0)|答案(2)|浏览(231)

如何在我的映像中安装诗歌?(我应该使用pip吗?)
我应该用哪种版本的诗?
我是否需要虚拟环境?
wild中有manyexamples和opinions,它们提供了不同的解决方案。

pftdvrlh

pftdvrlh1#

TL; DR

使用pip安装poetry,配置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.安装poetry,但是你需要把它从系统解释器和项目的虚拟环境中隔离出来。
为了最大限度地控制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/. cache/pypoetry/virtualenvs中创建一个虚拟环境,以将系统解释器与应用程序隔离。这是大多数开发场景所需的行为。当使用容器时,$HOME变量可能会在某些运行时被更改,因此在独立目录中创建虚拟环境可以解决任何可能出现的可重复性问题。

将一切整合在一起

要在 Docker 图像中使用诗歌,您需要:
1.安装您想要的诗歌版本
1.配置虚拟环境位置
1.安装依赖项
1.使用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

gist中的完整内容。

gg0vcinb

gg0vcinb2#

我更喜欢使用多阶段构建,这样我就可以在我的实际发布图像中摆脱诗意,并保持这些图像的苗条。

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-dev --no-root --no-ansi

# ---------------------------------------------------------------------

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

相关问题