Spaces:
Running
Running
Create Dockerfile
Browse files- Dockerfile +49 -0
Dockerfile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python 3.11 slim image for a smaller footprint
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# 1. Install xvfb for the virtual display (needed because we use headless=False)
|
| 5 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 6 |
+
xvfb \
|
| 7 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
# 2. Hugging Face requires running as a non-root user (UID 1000)
|
| 10 |
+
RUN useradd -m -u 1000 user
|
| 11 |
+
ENV HOME=/home/user \
|
| 12 |
+
PATH=/home/user/.local/bin:$PATH
|
| 13 |
+
|
| 14 |
+
WORKDIR /app
|
| 15 |
+
|
| 16 |
+
# 3. Copy and install Python dependencies (as root for global install)
|
| 17 |
+
COPY requirements.txt .
|
| 18 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 19 |
+
pip install --no-cache-dir -r requirements.txt
|
| 20 |
+
|
| 21 |
+
# 4. Install ALL Playwright system dependencies reliably
|
| 22 |
+
# (We run this as ROOT so it can execute apt-get internally to fetch libXfixes.so.3, etc.)
|
| 23 |
+
RUN playwright install-deps chromium
|
| 24 |
+
|
| 25 |
+
# 5. Switch to the non-root user
|
| 26 |
+
USER user
|
| 27 |
+
WORKDIR $HOME/app
|
| 28 |
+
|
| 29 |
+
# 6. Install Playwright browser binaries (chromium only, as user)
|
| 30 |
+
# (This just downloads the Chrome binary to /home/user/.cache)
|
| 31 |
+
RUN playwright install chromium
|
| 32 |
+
|
| 33 |
+
# 7. Copy application code (owned by user)
|
| 34 |
+
COPY --chown=user . .
|
| 35 |
+
|
| 36 |
+
# 8. Create browser_data dir for persistent context (writeable by user)
|
| 37 |
+
RUN mkdir -p ./browser_data
|
| 38 |
+
|
| 39 |
+
# 9. Expose the port Hugging Face Spaces expects (7860)
|
| 40 |
+
EXPOSE 7860
|
| 41 |
+
|
| 42 |
+
# 10. Start with Xvfb virtual display + uvicorn
|
| 43 |
+
# Xvfb provides a virtual screen so Chromium never fails on missing $DISPLAY
|
| 44 |
+
CMD ["sh", "-c", "Xvfb :99 -screen 0 1024x768x16 & export DISPLAY=:99 && uvicorn app:app --host 0.0.0.0 --port 7860"]
|
| 45 |
+
|
| 46 |
+
# ---------------------------------------------------------
|
| 47 |
+
# FUTURE SCALING: For 4+ CPU HF Spaces, switch to Gunicorn:
|
| 48 |
+
# CMD ["sh", "-c", "Xvfb :99 -screen 0 1024x768x16 & export DISPLAY=:99 && gunicorn app:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:7860 --timeout 120"]
|
| 49 |
+
# ---------------------------------------------------------
|