# --- Step 1: Base Build Stage --- # Use an official Python slim image for a lightweight foundation FROM python:3.11-slim as builder # Set build-time environment variables ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 # Set the working directory inside the container WORKDIR /app # Install minimal system-level dependencies required for building/running certain wheels # (Even with headless OpenCV, some underlying OS libraries are helpful) RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ curl \ && rm -rf /var/lib/apt/lists/* # Copy only the requirements file first to maximize Docker layer caching COPY requirements.txt . # Install dependencies into a local deployment wheelhouse/directory # We use --no-cache-dir to minimize final image size RUN pip install --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # --- Step 2: Final Runtime Stage --- FROM python:3.11-slim WORKDIR /app # Set runtime environment parameters ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV PORT=8000 # Copy installed dependencies from the builder stage COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Copy the application source code into the execution environment COPY . . # Expose the internal port that Uvicorn will listen on EXPOSE ${PORT} # Healthcheck to verify the container is alive and responding HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:${PORT}/docs || exit 1 # Launch the Uvicorn application server CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]