# --- Stage 1: Build Dependencies --- FROM python:3.12-slim AS builder WORKDIR /app RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ libpq-dev \ python3-dev \ && rm -rf /var/lib/apt/lists/* COPY requirements.txt . RUN pip install --user --no-cache-dir -r requirements.txt # --- Stage 2: Runtime Environment --- FROM python:3.12-slim # Set up secure non-root user RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH \ PYTHONUNBUFFERED=1 WORKDIR $HOME/app # Install PostgreSQL runtime libraries (must be root) USER root RUN apt-get update && apt-get install -y --no-install-recommends \ libpq-dev \ && rm -rf /var/lib/apt/lists/* USER user # Copy dependencies from builder COPY --chown=user --from=builder /root/.local /home/user/.local COPY --chown=user requirements.txt . # Copy core API codebase COPY --chown=user app/ ./app COPY --chown=user alembic/ ./alembic COPY --chown=user migrations/ ./migrations COPY --chown=user alembic.ini apply_schema_updates.py ./ EXPOSE 7860 # Run schema migrations first, then start FastAPI via uvicorn (exec hands over PID 1) CMD ["bash", "-c", "python3 apply_schema_updates.py && exec python3 -m uvicorn app.main:app --host 0.0.0.0 --port 7860"]