From 0a02f1db14039ff83a239f5255525cff7b0ae822 Mon Sep 17 00:00:00 2001 From: saani Date: Sun, 23 Nov 2025 18:36:23 +0000 Subject: [PATCH] feat: migrate to UV package manager and enhance production config - Replace pip with UV for faster dependency installation - Switch base image from python:3.11-slim to debian:bookworm-slim - Add SECURE_PROXY_SSL_HEADER for production SSL proxy support - Optimize Docker layer caching by copying dependency files first - Move database migration to build time for faster container startup - Enhance Gunicorn logging with access and error log streams - Update dependency management from requirements.txt to pyproject.toml These changes improve build performance, production security, and container startup time while modernizing the dependency management workflow. --- booking_system/settings.py | 3 +++ dockerfile | 33 ++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/booking_system/settings.py b/booking_system/settings.py index 1403f67..e3fd12b 100644 --- a/booking_system/settings.py +++ b/booking_system/settings.py @@ -67,6 +67,9 @@ TEMPLATES = [ WSGI_APPLICATION = 'booking_system.wsgi.application' +if not DEBUG: + SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") + # DATABASES = { # 'default': { diff --git a/dockerfile b/dockerfile index 6ffeffd..82fddd4 100644 --- a/dockerfile +++ b/dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-slim +FROM debian:bookworm-slim # Set environment variables ENV PYTHONDONTWRITEBYTECODE 1 @@ -7,24 +7,31 @@ ENV PYTHONUNBUFFERED 1 # Set work directory WORKDIR /app -# Install system dependencies RUN apt-get update && apt-get install -y \ - gcc \ + build-essential \ + curl wget \ && rm -rf /var/lib/apt/lists/* -# Copy requirements and install Python dependencies -COPY requirements.txt . -RUN pip install --no-cache-dir --upgrade pip -RUN pip install --no-cache-dir -r requirements.txt +RUN curl -LsSf https://astral.sh/uv/install.sh | sh +ENV PATH="/root/.local/bin:${PATH}" -# Copy project +# Copy only the dependency definitions first to leverage Docker's layer caching +COPY pyproject.toml uv.lock .python-version ./ + +# Install Python dependencies for production +RUN uv sync --no-group dev --group prod + +# Copy the rest of the application code into the container COPY . . -# Collect static files -RUN python manage.py collectstatic --noinput +# Collect the static files +RUN uv run --no-sync ./manage.py collectstatic --noinput -# Expose port +# Migrate the database +RUN uv run --no-sync ./manage.py migrate + +# Expose the port Gunicorn will run on EXPOSE 8000 -# Run migrations and start Gunicorn -CMD sh -c "python manage.py migrate && gunicorn booking_system.wsgi:application --bind 0.0.0.0:8000 --workers 3" \ No newline at end of file +# Run with gunicorn +CMD ["uv", "run", "--no-sync", "gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "info", "config.wsgi:application"] \ No newline at end of file -- 2.39.5