# STAGE 1: Base (Heavy Dependencies) FROM python:3.10-slim as base WORKDIR /app # Install system deps if needed (e.g., usually needed for cv2 or weird numpy versions) # RUN apt-get update && apt-get install -y --no-install-recommends gcc libgomp1 COPY requirements.txt . # 1. Install Torch (Heavy - Cached in this layer) RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu # 2. Install other Prod deps RUN pip install --no-cache-dir -r requirements.txt # STAGE 2: Test (Needs App Code + Tests) FROM base as test # 1. Install Test Tools RUN pip install pytest flake8 httpx # 2. COPY THE APP CODE (Crucial Step!) # The tests need to import 'main', so main.py must be here. COPY main.py /app/ # 3. Copy Tests COPY tests/ /app/tests/ ENV PYTHONPATH=/app # 4. Run Linting and Tests RUN flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics RUN pytest tests/ # STAGE 3: Production (Final Image) FROM base as prod # We start fresh from 'base'. The junk from 'test' stage is gone. COPY main.py /app/ # Note: We do NOT copy tests/ here to keep prod image smaller EXPOSE 8000 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]