# Multi-stage Docker build for Image Search API FROM python:3.11-slim AS base # Set environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 # Install system dependencies # libpq-dev is required for psycopg2 compilation RUN apt-get update && apt-get install -y \ build-essential \ curl \ libpq-dev \ && rm -rf /var/lib/apt/lists/* # Create app directory WORKDIR /app # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy application code (API only) COPY app/ ./app/ # Create data directory RUN mkdir -p /app/data # Expose port (Hugging Face Spaces uses 7860 by default) EXPOSE 7860 # Health check - use PORT env var if set, otherwise 7860 (Hugging Face Spaces default) HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD sh -c "curl -f http://localhost:${PORT:-7860}/health || exit 1" # Default command - use PORT env var if set, otherwise 7860 (Hugging Face Spaces default) CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-7860}"] # Production stage FROM base AS production # Install additional production dependencies RUN pip install --no-cache-dir gunicorn # Create non-root user RUN useradd --create-home --shell /bin/bash appuser && \ chown -R appuser:appuser /app USER appuser # Use gunicorn for production - use PORT env var if set, otherwise 7860 (Hugging Face Spaces default) CMD ["sh", "-c", "gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:${PORT:-7860}"] # Development stage FROM base AS development # Install development dependencies RUN pip install --no-cache-dir \ pytest \ pytest-cov \ black \ flake8 \ mypy # Use uvicorn with reload for development - use PORT env var if set, otherwise 7860 (Hugging Face Spaces default) CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-7860} --reload"]