File size: 2,337 Bytes
37a9ecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# ============================================================
# 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"]