hey / Dockerfile
ShadowHunter222's picture
Create Dockerfile
948d5fa verified
# Use official Python 3.11 slim image for a smaller footprint
FROM python:3.11-slim
# 1. Install xvfb for the virtual display (needed because we use headless=False)
RUN apt-get update && apt-get install -y --no-install-recommends \
xvfb \
&& rm -rf /var/lib/apt/lists/*
# 2. Hugging Face requires running as a non-root user (UID 1000)
RUN useradd -m -u 1000 user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
WORKDIR /app
# 3. Copy and install Python dependencies (as root for global install)
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# 4. Install ALL Playwright system dependencies reliably
# (We run this as ROOT so it can execute apt-get internally to fetch libXfixes.so.3, etc.)
RUN playwright install-deps chromium
# 5. Switch to the non-root user
USER user
WORKDIR $HOME/app
# 6. Install Playwright browser binaries (chromium only, as user)
# (This just downloads the Chrome binary to /home/user/.cache)
RUN playwright install chromium
# 7. Copy application code (owned by user)
COPY --chown=user . .
# 8. Create browser_data dir for persistent context (writeable by user)
RUN mkdir -p ./browser_data
# 9. Expose the port Hugging Face Spaces expects (7860)
EXPOSE 7860
# 10. Start with Xvfb virtual display + uvicorn
# Xvfb provides a virtual screen so Chromium never fails on missing $DISPLAY
CMD ["sh", "-c", "Xvfb :99 -screen 0 1024x768x16 & export DISPLAY=:99 && uvicorn app:app --host 0.0.0.0 --port 7860"]
# ---------------------------------------------------------
# FUTURE SCALING: For 4+ CPU HF Spaces, switch to Gunicorn:
# 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"]
# ---------------------------------------------------------