/python-poetry

Python Docker images that use poetry for dependency management.

Primary LanguageDockerfileMIT LicenseMIT

python-poetry

What is this?

A series of Python Docker images that use poetry for dependency management. Because this is what I tend to do, these images also:

Usage

Simple

FROM duffn/python-poetry:3.9-slim

COPY pyproject.toml poetry.lock ./
# Poetry is installed with `pip`, so active our virtual environmentn and install projects dependecies there, so they don't conflict with poetry's dependencies.
RUN . $VENV_PATH/bin/activate && $POETRY_HOME/poetry install --no-root

WORKDIR /app
COPY . .

# Our user has an ID of 10000 and the group an ID of 10001.
RUN chown 10000:10001 -R /app

# Our non-root username.
USER nonroot

# Use `tini` to start our container.
ENTRYPOINT ["tini", "--"]
CMD ["./my-start-command.sh"]

Multi-stage Build

FROM duffn/python-poetry:3.9-slim as base

COPY ./poetry.lock ./pyproject.toml ./
# Only install the production dependencies in our base multi-stage build.
RUN . $VENV_PATH/bin/activate && $POETRY_HOME/poetry install --no-root --no-dev

# Development image
FROM base as development

# Install the development dependencies as well in our development target.
RUN . $VENV_PATH/bin/activate && $POETRY_HOME/poetry install --no-root

WORKDIR /app
COPY . .

RUN chown 10000:10001 -R /app

USER nonroot

ENTRYPOINT ["tini", "--"]
CMD ["./my-start-command.sh"]

# Production image
# Build only a single target with the --target production flag.
# `docker build --target production -t my-image:latest .`
FROM base as production

# No need to install dependenices here as we have the production dependencies in our base image.
WORKDIR /app
COPY . .

RUN chown 10000:10001 -R /app

USER nonroot

ENTRYPOINT ["tini", "--"]
CMD ["./my-start-command.sh"]

my-start-command.sh could look something like:

#!/bin/bash

set -e

. /venv/bin/activate

flask run --host=0.0.0.0

Tags

All images use the official Python images as their base. All images are build when code is merged to the main branch.

  • The X.X-<name> tags, for example 3.9-slim should use the most recent version of poetry.
    • If you'd like, you can also pin to a specific version of poetry using the X.X-<name>-X.X.X tags, where X.X.X is a version of poetry starting with the minimum version of 1.1.4. For example, 3.9-slim-1.1.4.
    • Furthermore, you can pin to a build to a specific date by using the X.X-<name>-<date> tags.
  • You can find all available tags on Docker Hub or the GitHub Container Registry.

License

MIT