[Index]

A simple Poetry Dockerfile

Published Thursday, March 13, 2025

Poetry can be a bit hard to set up a Dockerfile for; there are many variants lying around the internet.

This version keeps things simple, it uses pip to install poetry, which is in my opinion better than doing a curl on Poetry's website [1]. Docker only reruns the install step when dependencies have changed, by first COPY'ing the pyproject.toml Docker after copying the remaining the app code. [2].

This will install the packages "globally". I think that is fine, or even desired, in a container. Bump the Python and/or Poetry versions as needed.

FROM python:3.12

ENV POETRY_NO_INTERACTION=1 \
    POETRY_VIRTUALENVS_CREATE=false \
    POETRY_CACHE_DIR='/var/cache/pypoetry' \
    POETRY_HOME='/usr/local' \
    POETRY_VERSION=2.1

WORKDIR /app

COPY pyproject.toml poetry.lock ./

RUN pip install poetry==$POETRY_VERSION && poetry install --only main --no-root --no-directory

COPY . /app

RUN poetry install --only main

... your CMD
[1] https://python-poetry.org/docs/#installing-with-the-official-installer
[2] https://python-poetry.org/docs/faq#poetry-busts-my-docker-cache-because-it-requires-me-to-copy-my-source-files-in-before-installing-3rd-party-dependencies