# ============================================================ # Stage 1 : builder — compile les wheels # ============================================================ FROM python:3.12-slim AS builder ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 RUN apt-get update && apt-get install -y --no-install-recommends \ gcc g++ git curl \ libxml2-dev libxslt1-dev zlib1g-dev \ libffi-dev libssl-dev \ && rm -rf /var/lib/apt/lists/* WORKDIR /build COPY requirements.txt . RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt # ============================================================ # Stage 2 : production — image minimale # ============================================================ FROM python:3.12-slim ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PORT=7860 \ PYTHONPATH=/app RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ libxml2 libxslt1.1 \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean # Utilisateur non-root RUN useradd -m -u 1000 -s /bin/bash scraper \ && mkdir -p /app/cache /app/logs \ && chown -R scraper:scraper /app WORKDIR /app # Installer les wheels compilés COPY --from=builder /wheels /wheels RUN pip install --no-cache-dir --no-index --find-links=/wheels /wheels/* \ && rm -rf /wheels # Copier le code applicatif COPY --chown=scraper:scraper app.py models.py scraper.py utils.py ./ USER scraper ENV HF_HOME=/app/cache \ NUMEXPR_MAX_THREADS=4 \ OMP_NUM_THREADS=4 \ # Scrapy asyncio reactor — doit être défini avant tout import SCRAPY_SETTINGS_MODULE="" HEALTHCHECK --interval=20s --timeout=5s --start-period=40s --retries=2 \ CMD curl -f -m 4 http://localhost:${PORT}/health || exit 1 EXPOSE ${PORT} # Scrapy install_reactor doit être importé AVANT Twisted/Scrapy au démarrage. # On passe par un petit script d'amorçage pour garantir l'ordre des imports. CMD ["sh", "-c", "python -c 'import scrapy.utils.reactor; scrapy.utils.reactor.install_reactor(\"twisted.internet.asyncioreactor.AsyncioSelectorReactor\")' && uvicorn app:app --host 0.0.0.0 --port ${PORT} --workers 1 --loop uvloop --http httptools --log-level warning --no-access-log --limit-concurrency 100 --timeout-keep-alive 30"]