Python with poetry for running python packages managed by poetry
10K+
Docker images with the python dependency management tool poetry.
Inside the containers, poetry can be used as usual per CLI.
To reduce image size, it is recommended to use a multi-layer build to avoid having poetry inside the final container. The below example shows how to install a package into a virtual environment in a building layer and then copy it to the executing layer to reach the minimal image size.
# Allow passing of Python and poetry version to the build
ARG PYTHON_VERSION="3.11"
ARG POETRY_VERSION="1.2.2"
# Building layer
FROM fabianemmi/python-poetry:${PYTHON_VERSION}-${POETRY_VERSION}-slim as builder
# Copy package: ideally only `.py` files, `poetry.lock`, and `pyproject.toml`
ADD <package> /opt/<package>
COPY poetry.lock pyproject.toml /opt/
WORKDIR /opt
# Install with poetry
RUN python -m venv /venv && \
. /venv/bin/activate && \
pip install --upgrade pip && \
poetry build && \
pip install /opt/dist/*.whl
# Delete Python cache files
RUN find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf && \
cd /venv && \
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -r
# Execution layer
FROM python:${PYTHON_VERSION}-alpine
# Copy the required files: vritual environment
COPY --from=builder /venv /venv
# Add venv to PATH variable to make installed packages available
ENV PATH=/venv/bin:$PATH
Notes:
/app/<package> from the builder layer. This is only necessary when executing a source code file of the project.. /venv/bin/activate but can be made redundant by using
ENV PATH=/venv/bin:$PATH
Creation of virtual environments inside Docker containers is not required and, hence, turned off (POETRY_VIRTUALENVS_CREATE=false).
To correctly cache dependencies in a CI job, you need to enable creation of virtual environments in a pipeline. Here is an example for GitLab Pipelines:
variables:
POETRY_CACHE_DIR: ${CI_PROJECT_DIR}/.venv
PIP_CACHE_DIR: ${CI_PROJECT_DIR}/.cache/pip
.poetry-cache: &poetry-cache
cache:
paths:
- $POETRY_CACHE_DIR
- $PIP_CACHE_DIR
key:
files:
- poetry.lock
- .gitlab-ci.yml
prefix: ${CI_COMMIT_REF_SLUG}-${CI_JOB_NAME}
.python-poetry: &python-poetry
<<: *poetry-cache
variables:
POETRY_CACHE_DIR: ${POETRY_CACHE_DIR}
image: fabianemmi/python-poetry:latest
...
example job:
<<: *python-poetry
before_script:
- export POETRY_VIRTUALENVS_CREATE=true
- poetry config virtualenvs.in-project true
- poetry install ...
...
This repository is managed from GitLab. Feel free to open merge requests.
Content type
Image
Digest
sha256:4dcc81191…
Size
399.3 MB
Last updated
about 1 year ago
docker pull fabianemmi/python-poetry:3.12-2.1.4