# Use Python 3.11 slim as base image FROM python:3.11-slim as builder # Set working directory WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ gcc \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY requirements.txt . RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt # Final stage FROM python:3.11-slim # Create non-root user RUN useradd -m appuser # Set working directory WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ libpq5 \ && rm -rf /var/lib/apt/lists/* # Copy wheels from builder stage COPY --from=builder /app/wheels /wheels COPY --from=builder /app/requirements.txt . # Install Python packages RUN pip install --no-cache /wheels/* # Copy application code COPY ./app app/ COPY ./alembic.ini . COPY ./alembic alembic/ # Create necessary directories with proper 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 # Expose port required by Hugging Face EXPOSE 7860 # Start the application with uvicorn on port 7860 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]