FROM python:3.9-slim AS builder # Install system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* # Set working directory WORKDIR /app # Copy requirements COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt FROM python:3.9-slim # Set environment variables ENV TRANSFORMERS_CACHE=/app/cache ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 # Install minimal dependencies for runtime RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ && rm -rf /var/lib/apt/lists/* # Create app directory and cache directory RUN mkdir -p /app ${TRANSFORMERS_CACHE} && \ # Create a non-root user to run the application useradd -m appuser && \ chown -R appuser:appuser /app ${TRANSFORMERS_CACHE} WORKDIR /app # Copy requirements and install directly in the final stage COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY --chown=appuser:appuser . . # Switch to non-root user USER appuser # Pre-download models to avoid cold starts in production RUN python -c "from transformers import pipeline; \ pipeline('summarization', model='facebook/bart-large-cnn'); \ pipeline('image-to-text', model='nlpconnect/vit-gpt2-image-captioning')" # Expose the port the app runs on EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ CMD curl -f http://localhost:8000/ || exit 1 # Command to run the application CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]