/poetry-docker

Docker image with poetry pre-installed

Primary LanguagePythonMIT LicenseMIT

poetry-docker

1.8.3-py3.12-slim badge 1.8.3-py3.11-slim badge 1.8.3-py3.10-slim badge 1.8.3-py3.9-slim badge 1.8.3-py3.8-slim badge Docker Publish Badge

a Docker image that includes Poetry for CI/CD pipelines.

Supported tags

  • 1.8.3-py3.12-slim
  • 1.8.3-py3.11-slim
  • 1.8.3-py3.10-slim
  • 1.8.3-py3.9-slim
  • 1.8.3-py3.8-slim

See versions.json for further information.

Usage

In your pipeline / actions, replace docker image from python to biggates/poetry, for example:

# FROM python:3.10-slim
FROM biggates/poetry:1.8.2-py3.10-slim

details

Poetry is installed to /root/.local/bin. In case the path fails, use the following line in Dockerfile:

ENV PATH="/root/.local/bin:$PATH"

Usage in multistage builds

A typical usage is use poetry to install all the dependencies in one stage, and copy the whole venv to another stage and use it.

# first stage
FROM biggates/poetry:1.8.2-py3.10-slim as venv-creator

ENV POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_IN_PROJECT=1 \
    POETRY_VIRTUALENVS_CREATE=1 \
    POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

# add poetry managed dependencies
ADD poetry.lock poetry.toml pyproject.toml README.md ./

# let poetry create the venv
RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install

# second stage
FROM python:3.10-slim as runtime

# activate the venv
ENV VIRTUAL_ENV=/app/.venv \
    PATH="/app/.venv/bin:$PATH"

WORKDIR /app

# copy the previously created venv
COPY --from=venv-creator ${VIRTUAL_ENV} ${VIRTUAL_ENV}

# add current project files
COPY . /app

# rest of your dockerfile
CMD ["python", "myscript.py"]

reference: Blazing fast Python Docker builds with Poetry