From 5a4260ed42725cc0aa0fd8d97072cdedc5fe7948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 24 Oct 2024 00:16:43 +0200 Subject: [PATCH 1/2] Update Dockerfile to use uv --- Dockerfile | 85 ++++++++++++++++++++++++++++++++++++++------ docker/entrypoint.sh | 8 +++++ docker/uwsgi.ini | 10 ++++++ 3 files changed, 92 insertions(+), 11 deletions(-) create mode 100755 docker/entrypoint.sh create mode 100644 docker/uwsgi.ini diff --git a/Dockerfile b/Dockerfile index 983d0ab..2032b7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,79 @@ -FROM python:3.11-slim +# Stage 1: General debian environment +FROM debian:stable-slim AS linux-base -WORKDIR /srv/app +# Assure UTF-8 encoding is used. +ENV LC_CTYPE=C.utf8 +# Location of the virtual environment +ENV UV_PROJECT_ENVIRONMENT="/venv" +# Location of the python installation via uv +ENV UV_PYTHON_INSTALL_DIR="/python" +# Byte compile the python files on installation +ENV UV_COMPILE_BYTECODE=1 +# Python verision to use +ENV UV_PYTHON=python3.12 +# Tweaking the PATH variable for easier use +ENV PATH="$UV_PROJECT_ENVIRONMENT/bin:$PATH" -RUN apt update && \ - apt install --no-install-recommends -y libpq-dev build-essential curl gcc sassc libpango-1.0-0 libpangoft2-1.0-0 libjpeg-dev libopenjp2-7-dev libffi-dev libpangoft2-1.0-0 libcairo2 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 shared-mime-info gettext &&\ - rm -rf /var/lib/apt/lists/* +# Update debian +RUN apt-get update +RUN apt-get upgrade -y -COPY --chown=www-data:www-data . . +# Install general required dependencies +RUN apt-get install --no-install-recommends -y tzdata \ + libpango-1.0-0 \ + libpangoft2-1.0-0 \ + libjpeg-dev \ + libopenjp2-7-dev \ + libffi-dev \ + libpangoft2-1.0-0 \ + libcairo2 \ + libpangocairo-1.0-0 \ + libgdk-pixbuf2.0-0 \ + shared-mime-info \ + gettext \ + tini \ + uwsgi -run python -m pip install --upgrade pip \ - && python -m pip install uwsgi \ - && python -m pip install -r requirements.txt \ - && python -m pip install . +# Stage 2: Python environment +FROM linux-base AS python-base -ENTRYPOINT [ "uwsgi", "-i", "uwsgi.ini" ] +# Install debian dependencies +RUN apt-get install --no-install-recommends -y \ + libpq-dev \ + build-essential \ + curl \ + gcc \ + sassc + +# Install uv +COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv + +# Create virtual environment and install dependencies +COPY pyproject.toml ./ +COPY uv.lock ./ +RUN uv sync --frozen --no-dev --no-install-project + +# Stage 3: Building environment +FROM python-base AS builder-base + +WORKDIR /app +COPY . /app + +# Build static files +RUN python manage.py collectstatic --no-input + +# Compile translation files +RUN python manage.py compilemessages + +# Stage 4: Webapp environment +FROM linux-base AS webapp + +# Copy python, virtual env and static assets +COPY --from=builder-base $UV_PYTHON_INSTALL_DIR $UV_PYTHON_INSTALL_DIR +COPY --from=builder-base $UV_PROJECT_ENVIRONMENT $UV_PROJECT_ENVIRONMENT +COPY --from=builder-base /app /app + +# Start the application server +WORKDIR /app +EXPOSE 8000 +CMD ["docker/entrypoint.sh"] diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..3d6d97c --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euxo pipefail + +echo "Migrate database..." +python manage.py migrate + +echo "Start uwsgi..." +exec uwsgi --ini docker/uwsgi.ini diff --git a/docker/uwsgi.ini b/docker/uwsgi.ini new file mode 100644 index 0000000..92dd00e --- /dev/null +++ b/docker/uwsgi.ini @@ -0,0 +1,10 @@ +[uwsgi] +http = :$(PORT) +home = /venv +module = la_chariotte.wsgi:application +master = True +vacuum = True +max-requests = 5000 +processes = 4 +enable-threads = true +buffer-size = 32768 From 9f8be6d52d1e4e8f43338c77014c0c03085703c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 24 Oct 2024 01:18:41 +0200 Subject: [PATCH 2/2] Use granian as a WSGI server --- Dockerfile | 8 +++++--- docker/entrypoint.sh | 12 +++++++++--- docker/uwsgi.ini | 10 ---------- 3 files changed, 14 insertions(+), 16 deletions(-) delete mode 100644 docker/uwsgi.ini diff --git a/Dockerfile b/Dockerfile index 2032b7e..8487530 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,8 +31,7 @@ RUN apt-get install --no-install-recommends -y tzdata \ libgdk-pixbuf2.0-0 \ shared-mime-info \ gettext \ - tini \ - uwsgi + tini # Stage 2: Python environment FROM linux-base AS python-base @@ -52,6 +51,7 @@ COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv COPY pyproject.toml ./ COPY uv.lock ./ RUN uv sync --frozen --no-dev --no-install-project +RUN uv pip install granian # Stage 3: Building environment FROM python-base AS builder-base @@ -60,10 +60,12 @@ WORKDIR /app COPY . /app # Build static files +RUN python manage.py compilescss RUN python manage.py collectstatic --no-input # Compile translation files -RUN python manage.py compilemessages +# RUN python manage.py compilemessages + # Stage 4: Webapp environment FROM linux-base AS webapp diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 3d6d97c..0b9ea03 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -2,7 +2,13 @@ set -euxo pipefail echo "Migrate database..." -python manage.py migrate +python manage.py migrate --settings local_settings -echo "Start uwsgi..." -exec uwsgi --ini docker/uwsgi.ini +echo "Start server..." + +granian la_chariotte.wsgi:application \ + --host 127.0.0.1 \ + --port 8000 \ + --interface wsgi \ + --no-ws \ + --loop uvloop diff --git a/docker/uwsgi.ini b/docker/uwsgi.ini deleted file mode 100644 index 92dd00e..0000000 --- a/docker/uwsgi.ini +++ /dev/null @@ -1,10 +0,0 @@ -[uwsgi] -http = :$(PORT) -home = /venv -module = la_chariotte.wsgi:application -master = True -vacuum = True -max-requests = 5000 -processes = 4 -enable-threads = true -buffer-size = 32768