File size: 2,129 Bytes
2ebd518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
# ============================================
# Dockerfile for Hugging Face Spaces
# Novel Scraper with Playwright
# ============================================

# --- Stage 1: Use Python 3.10 slim base ---
FROM python:3.10-slim-bookworm

# --- Hugging Face requires port 7860 ---
ENV PORT=7860
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV DEBIAN_FRONTEND=noninteractive

# --- Create non-root user (HF requirement) ---
RUN useradd -m -u 1000 user

# --- Install system dependencies for Playwright ---
RUN apt-get update && apt-get install -y --no-install-recommends \
    # Playwright browser dependencies
    libnss3 \
    libnspr4 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libdbus-1-3 \
    libxkbcommon0 \
    libatspi2.0-0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2 \
    libwayland-client0 \
    # Additional libs often needed
    libglib2.0-0 \
    libgtk-3-0 \
    libx11-xcb1 \
    fonts-liberation \
    fonts-noto-cjk \
    wget \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# --- Set working directory ---
WORKDIR /home/user/app

# --- Copy requirements first (Docker cache optimization) ---
COPY --chown=user:user requirements.txt .

# --- Install Python dependencies ---
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# --- Install Playwright browsers (Chromium only to save space) ---
# Must run as user who will execute the app
USER user
RUN playwright install chromium

# --- Copy application code ---
COPY --chown=user:user . .

# --- Create screenshots directory ---
RUN mkdir -p /home/user/app/app/static/screenshots

# --- Expose the port ---
EXPOSE 7860

# --- Health check ---
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python -c "import httpx; httpx.get('http://localhost:7860/health')" || exit 1

# --- Start the application ---
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--timeout-keep-alive", "120"]