Skip to content

Instantly share code, notes, and snippets.

@NerdPraise
Created August 2, 2023 11:13
Show Gist options
  • Save NerdPraise/213fbf4e3b9a95565496cec5a7563881 to your computer and use it in GitHub Desktop.
Save NerdPraise/213fbf4e3b9a95565496cec5a7563881 to your computer and use it in GitHub Desktop.
DockerFile for poetry
FROM python:3.10.12-slim-buster as python-base
# python
ENV PYTHONUNBUFFERED=1 \
# prevents python creating .pyc files
PYTHONDONTWRITEBYTECODE=1 \
# poetry
# make poetry install to this location
POETRY_HOME="/opt/poetry" \
# make poetry create the virtual environment in the project's root
# it gets named `.venv`
POETRY_VIRTUALENVS_IN_PROJECT=true \
# do not ask any interactive question
POETRY_NO_INTERACTION=1 \
# paths
# this is where our requirements + virtual environment will live
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"
# prepend poetry and venv to path
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
# `builder-base` stage is used to build deps + create our virtual environment
FROM python-base as builder-base
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
build-essential \
# psycopg2 dependencies
&& apt-get install -y libpq-dev \
# Translations dependencies
&& apt-get install -y gettext \
# Install poetry
&& curl -sSL https://install.python-poetry.org | python3 - \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
# Switch workdir and copy our Python requirements here to cache them
# and install only runtime deps using poetry
WORKDIR $PYSETUP_PATH
COPY ./poetry.lock ./pyproject.toml ./
# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --no-dev
FROM python-base as development
WORKDIR $PYSETUP_PATH
# copy the built poetry + venv into the development image
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
# quicker install as runtime deps are already installed above
RUN poetry install
WORKDIR /app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment