Skip to content

Instantly share code, notes, and snippets.

@Alex-Kopylov
Created June 17, 2024 13:05
Show Gist options
  • Save Alex-Kopylov/10f72d36fa9d641220425c94ae0b7322 to your computer and use it in GitHub Desktop.
Save Alex-Kopylov/10f72d36fa9d641220425c94ae0b7322 to your computer and use it in GitHub Desktop.
Dockerfile poetry template
ARG PYTHON_VERSION=3.11.8
ARG PYTHON_DISTRO=slim-bullseye
FROM python:${PYTHON_VERSION}-${PYTHON_DISTRO} as base-image
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
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 Image
###############################################
FROM base-image as builder-base
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
build-essential
# install poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://install.python-poetry.org | POETRY_VERSION=$POETRY_VERSION POETRY_HOME=$POETRY_HOME python3 -
# copy project requirement files here to ensure they will be cached.
WORKDIR $PYSETUP_PATH
COPY pyproject.toml ./
# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --no-root
###############################################
# Production Image
###############################################
FROM base-image as production
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
ARG PYTHONPATH=/service
ENV PYTHONPATH=$PYTHONPATH \
POETRY_PROJECT_PATH=$PYTHONPATH/pyproject.toml
WORKDIR $PYTHONPATH
COPY . .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment