# Stage 1 - Build wheels FROM python:3.11-slim as builder # Set working directory WORKDIR /app # Install build dependencies RUN apt-get update && apt-get install -y \ gcc \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies into wheel format COPY requirements.txt . RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt # Stage 2 - Final image FROM python:3.11-slim # Create non-root user RUN useradd -m appuser # Set working directory WORKDIR /app # Install system runtime dependencies RUN apt-get update && apt-get install -y \ libpq5 \ && rm -rf /var/lib/apt/lists/* # Copy built wheels COPY --from=builder /app/wheels /wheels COPY --from=builder /app/requirements.txt . # Install packages from wheels RUN pip install --no-cache /wheels/* # Copy app source code COPY ./app app/ COPY ./alembic.ini . COPY ./alembic alembic/ # Create runtime directories and set permissions RUN mkdir -p /app/logs /app/uploads/images /app/uploads/documents /app/backups && \ chown -R appuser:appuser /app # Switch to non-root user USER appuser # Set environment variables ENV PYTHONPATH=/app \ PYTHONUNBUFFERED=1 \ REDIS_URL=redis://redis:6379/0 # Expose FastAPI port EXPOSE 7860 # Start the FastAPI server CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]