Skip to content

Instantly share code, notes, and snippets.

@wshayes
Last active October 29, 2024 19:53
Show Gist options
  • Save wshayes/62c8d029ce277476983461ce0afaf751 to your computer and use it in GitHub Desktop.
Save wshayes/62c8d029ce277476983461ce0afaf751 to your computer and use it in GitHub Desktop.
Poetry docker file that can support private git repository packages
# syntax=docker/dockerfile:experimental
FROM python:3.7-slim AS base
# ENV LANG=C.UTF-8 # Sets utf-8 encoding for Python et al
# ENV PYTHONDONTWRITEBYTECODE=1 # Turns off writing .pyc files; superfluous on an ephemeral container.
# ENV PYTHONUNBUFFERED=1 # Seems to speed things up
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
LANG=C.UTF-8 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_PATH=/opt/poetry \
VENV_PATH=/opt/venv \
POETRY_VERSION=1.0.0
# Ensures that the python and pip executables used
# in the image will be those from our virtualenv.
ENV PATH="$POETRY_PATH/bin:$VENV_PATH/bin:$PATH" \
PYTHONPATH=/app
RUN apt-get -qy update && apt-get install --no-install-recommends -y git libev-dev
FROM base as poetrydev
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
# deps for installing poetry
curl \
# deps for building python deps
build-essential \
# deps for using ssh
openssh-client \
\
# install poetry - uses $POETRY_VERSION internally
&& curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python \
&& mv /root/.poetry $POETRY_PATH \
&& poetry --version \
\
# configure poetry & make a virtualenv ahead of time since we only need one
&& python -m venv $VENV_PATH \
&& poetry config virtualenvs.create false \
\
# cleanup
&& rm -rf /var/lib/apt/lists/*
WORKDIR /venv_build
COPY poetry.lock pyproject.toml ./
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh poetry install --no-interaction --no-ansi -vvv
# Dev version of Docker image
FROM base AS dev
WORKDIR /app
EXPOSE 80
# Install OS package dependencies.
# Do all of this in one RUN to limit final image size.
RUN rm -rf /var/cache/apt/* /var/lib/apt/lists/*
COPY --from=poetrydev $VENV_PATH $VENV_PATH
COPY ./app /app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--debug", "--port", "80", "--reload-dir", "CHANGE_ME_DIR_NAME"]
@Janphr
Copy link

Janphr commented Nov 21, 2022

Yes.
I have not tried further to solve the problem, as it is not too important for me at the moment.
If I get to it and find a solution, I will post it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment