# ============================================ # Multi-stage build for production backend # ============================================ # Stage 1: Build dependencies FROM python:3.11-slim AS builder ARG PYTHON_VERSION=3.11 WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ gcc \ g++ \ libsndfile1 \ ffmpeg \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir --user -r requirements.txt # Stage 2: Production runtime FROM python:3.11-slim AS production WORKDIR /app # Install runtime dependencies only RUN apt-get update && apt-get install -y \ libsndfile1 \ ffmpeg \ curl \ && rm -rf /var/lib/apt/lists/* # Copy Python dependencies from builder COPY --from=builder /root/.local /root/.local # Make sure scripts in .local are usable ENV PATH=/root/.local/bin:$PATH # Copy application code COPY . . # Create non-root user RUN useradd -m -u 1000 appuser && \ chown -R appuser:appuser /app # Switch to non-root user USER appuser # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 # Run application with gunicorn CMD ["gunicorn", "app.main:app", \ "--workers", "4", \ "--worker-class", "uvicorn.workers.UvicornWorker", \ "--bind", "0.0.0.0:8000", \ "--timeout", "120", \ "--keep-alive", "5", \ "--access-logfile", "-", \ "--error-logfile", "-", \ "--log-level", "info"]