# ── Build stage: compile C extensions ────────────────────────────────── FROM python:3.12.9-slim-bookworm AS builder WORKDIR /build RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ && rm -rf /var/lib/apt/lists/* COPY requirements.txt . RUN pip install --no-cache-dir --prefix=/install -r requirements.txt # ── Runtime stage: lean production image ─────────────────────────────── FROM python:3.12.9-slim-bookworm # Create non-root user first, then copy with correct ownership (DW-2) RUN useradd -m -u 1000 appuser WORKDIR /app # Copy pre-built Python packages from builder COPY --from=builder /install /usr/local # Copy application code with correct ownership — avoids redundant chown layer COPY --chown=appuser:appuser . . # Make entrypoint executable RUN chmod +x entrypoint.sh USER appuser EXPOSE 8000 # Use entrypoint with exec so uvicorn runs as PID 1 and receives signals (DW-5) ENTRYPOINT ["./entrypoint.sh"]