Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UV to replace Pipenv; multi-stage Dockerfile #612

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 73 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,85 @@
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.10-slim
# Use a multi-stage build to first get uv
FROM ghcr.io/astral-sh/uv:0.5.7 AS uv

# Comment to trigger an image rebuild
FROM ubuntu:noble AS build

# Optional build argument for different environments
ARG ENV
RUN apt-get update -qy && \
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
ca-certificates \
clang \
gcc \
git \
libgdal-dev \
libpq-dev \
make

RUN apt-get update -y \
&& apt-get install --no-install-recommends -y gcc g++ libc-dev \
postgresql-client libpq-dev make git jq libgdal-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# We need to set this environment variable so that uv knows where
# the virtual environment is to install packages
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PROJECT_ENVIRONMENT=/app/.venv \
VIRTUAL_ENV=/app/.venv

RUN pip install --upgrade pip && pip install pipenv==2024.0.1
#TODO move to pipfile when operational
RUN pip install newrelic
# Create a virtual environment with uv inside the container
RUN --mount=type=cache,target=/app/.cache \
--mount=from=uv,source=/uv,target=./uv \
/uv python install 3.10 && \
/uv venv $VIRTUAL_ENV

# Install python dependencies
# Install everything for dev and test otherwise just core dependencies
COPY Pipfile Pipfile
COPY Pipfile.lock Pipfile.lock
# Make sure that the virtual environment is in the PATH so
# we can use the binaries of packages that we install such as pip
# without needing to activate the virtual environment explicitly
ENV PATH=$VIRTUAL_ENV/bin:/usr/local/bin:$PATH

RUN if [ "$ENV" = "dev" ] || [ "$ENV" = "test" ]; then \
echo "Install all dependencies" \
&& pipenv install --system --deploy --ignore-pipfile --dev; \
else \
echo "Install production dependencies only" \
&& pipenv install --system --deploy; \
fi
RUN --mount=type=cache,target=/app/.cache \
--mount=from=uv,source=/uv,target=./uv \
/uv pip install setuptools wheel

COPY ./app /app/app
# Copy pyproject.toml and uv.lock to a temporary directory
COPY pyproject.toml /_lock/
COPY uv.lock /_lock/

COPY alembic.ini /app/alembic.ini
# Install the packages with uv using --mount=type=cache to cache the downloaded packages
RUN --mount=type=cache,target=/app/.cache \
--mount=from=uv,source=/uv,target=./uv \
cd /_lock && \
/uv sync --locked --no-install-project

# Start the runtime stage
FROM ubuntu:noble
SHELL ["sh", "-exc"]

ENV PATH=$VIRTUAL_ENV/bin:/usr/local/bin:$PATH

ENV TZ UTC
RUN echo $TZ > /etc/timezone

RUN apt-get update -qy && \
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
expat \
libgdal-dev \
postgresql-client

COPY --chmod=777 wait_for_postgres.sh /usr/local/bin/wait_for_postgres.sh

# Set the entry point and signal handling
ENTRYPOINT [ "/app/start.sh" ]
STOPSIGNAL SIGINT

# Copy the pre-built `/app` directory from the build stage
COPY --from=build --chmod=777 /app /app
COPY --from=build --chmod=777 /root /root

COPY app/settings/prestart.sh /app/prestart.sh
COPY app/settings/start.sh /app/start.sh
COPY newrelic.ini /app/newrelic.ini
COPY alembic.ini /app/alembic.ini

COPY --chmod=777 app/settings/prestart.sh /app/prestart.sh
COPY --chmod=777 app/settings/start.sh /app/start.sh

COPY wait_for_postgres.sh /usr/local/bin/wait_for_postgres.sh
RUN chmod +x /usr/local/bin/wait_for_postgres.sh
RUN chmod +x /app/start.sh
COPY ./app /app/app

ENTRYPOINT [ "/app/start.sh" ]
WORKDIR /app
69 changes: 0 additions & 69 deletions Pipfile

This file was deleted.

Loading
Loading