| # Use an official NVIDIA CUDA base image | |
| # CUDA 12.1 is highly recommended for PyTorch 2.x stability and xformers compatibility | |
| FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04 | |
| # Set environment variables to prevent interactive prompts during build | |
| ENV DEBIAN_FRONTEND=noninteractive \ | |
| PYTHONUNBUFFERED=1 \ | |
| PYTHONDONTWRITEBYTECODE=1 | |
| # Install required system dependencies | |
| # Added libgl1 and libglib2.0-0 which are strictly required by OpenCV and many custom nodes | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| python3 \ | |
| python3-pip \ | |
| python3-dev \ | |
| git \ | |
| git-lfs \ | |
| wget \ | |
| ffmpeg \ | |
| libgl1 \ | |
| libglib2.0-0 \ | |
| && apt-get clean && rm -rf /var/lib/apt/lists/* | |
| # Hugging Face Spaces STRICT REQUIREMENT: Create and switch to a non-root user | |
| # Spaces run containers as UID 1000 by default. Failing to do this causes permission denied errors. | |
| RUN useradd -m -u 1000 user | |
| USER user | |
| ENV HOME=/home/user \ | |
| PATH=/home/user/.local/bin:$PATH | |
| # Set the working directory | |
| WORKDIR $HOME/app | |
| # Clone the main ComfyUI repository | |
| RUN git clone https://github.com/comfyanonymous/ComfyUI.git . | |
| # Install PyTorch, TorchVision, and TorchAudio with explicit CUDA support | |
| # This ensures pip does not accidentally fetch the CPU-only wheels | |
| RUN pip3 install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 | |
| # Install core ComfyUI dependencies | |
| RUN pip3 install --no-cache-dir -r requirements.txt | |
| # Install ComfyUI-Manager (Highly recommended for installing custom nodes from the UI) | |
| RUN git clone https://github.com/ltdrdata/ComfyUI-Manager.git custom_nodes/ComfyUI-Manager && \ | |
| cd custom_nodes/ComfyUI-Manager && \ | |
| pip3 install --no-cache-dir -r requirements.txt | |
| # Expose the standard port used by Hugging Face Spaces | |
| EXPOSE 7860 | |
| # Start ComfyUI | |
| # Legacy flags like --use-pytorch-cross-attention are omitted since SDPA is natively used in modern PyTorch | |
| CMD ["python3", "main.py", "--listen", "0.0.0.0", "--port", "7860"] | |
| # Before running the ComfyUI | |
| COPY extra_model_paths.yaml /home/user/app/extra_model_paths.yaml |