ComfyUI/Dockerfile
clsferguson bc0e12819d
build(docker): compile SageAttention 2.2 on slim trixie with Debian CUDA toolkit; install wheel into runtime
Switch to a two-stage build that uses python:3.12-slim-trixie as both builder and runtime, enabling contrib/non-free/non-free-firmware in APT to install Debian’s nvidia-cuda-toolkit (nvcc) for compiling SageAttention 2.2 from source. Install Torch cu129 in the builder and build a cp312 wheel, then copy and install that wheel into the slim runtime so --use-sage-attention works at startup. This removes the heavy CUDA devel base, avoids a venv by permitting pip system installs during build, and keeps the final image minimal while ensuring ABI alignment with Torch cu129.
2025-09-21 22:42:46 -06:00

79 lines
2.6 KiB
Docker

# --------------------------
# Stage 1: build SageAttention 2.2 wheel (Debian trixie + nvcc)
# --------------------------
FROM python:3.12.11-slim-trixie AS sage-builder
ENV DEBIAN_FRONTEND=noninteractive \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1
# Enable contrib/non-free/non-free-firmware, install CUDA toolkit + build deps
RUN set -eux; \
sed -i 's/ main$/ main contrib non-free non-free-firmware/g' /etc/apt/sources.list; \
apt-get update; \
apt-get install -y --no-install-recommends \
ca-certificates curl git build-essential cmake \
nvidia-cuda-toolkit \
; \
rm -rf /var/lib/apt/lists/*
WORKDIR /tmp/sage
# Install Torch cu129 in system site (PEP 668: allow system installs)
RUN python -m pip install --upgrade pip setuptools wheel --break-system-packages && \
python -m pip install torch torchvision torchaudio \
--extra-index-url https://download.pytorch.org/whl/cu129 \
--break-system-packages
# Shallow clone SageAttention and build a cp312 wheel
RUN git clone --depth 1 https://github.com/thu-ml/SageAttention.git . && \
python -m pip wheel . --no-deps --no-build-isolation -w /dist --break-system-packages
# --------------------------
# Stage 2: runtime image (slim)
# --------------------------
FROM python:3.12.11-slim-trixie
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
COMFY_AUTO_INSTALL=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1
# System deps
RUN apt-get update && apt-get install -y --no-install-recommends \
git build-essential cmake libgl1 libglx-mesa0 libglib2.0-0 \
fonts-dejavu-core fontconfig util-linux \
&& rm -rf /var/lib/apt/lists/*
# Create runtime user/group
RUN groupadd --gid 1000 appuser \
&& useradd --uid 1000 --gid 1000 --create-home --shell /bin/bash appuser
WORKDIR /app/ComfyUI
# Install core deps (Torch cu129 must match builder)
COPY requirements.txt ./
RUN python -m pip install --upgrade pip setuptools wheel && \
python -m pip install torch torchvision torchaudio \
--extra-index-url https://download.pytorch.org/whl/cu129 && \
python -m pip install -r requirements.txt && \
python -m pip install imageio-ffmpeg "av>=14.2" nvidia-ml-py
# Install the SageAttention wheel built in the builder stage
COPY --from=sage-builder /dist/sageattention-*.whl /tmp/
RUN python -m pip install /tmp/sageattention-*.whl
# Copy the application
COPY . .
# Entrypoint and launch
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh \
&& chown -R appuser:appuser /app /home/appuser /entrypoint.sh
EXPOSE 8188
USER root
ENTRYPOINT ["/entrypoint.sh"]
CMD ["python", "main.py", "--listen", "0.0.0.0", "--use-sage-attention"]