# ============================================================================
# ImageLab RunPod ComfyUI image
# CUDA 12.8 — Blackwell / Hopper / Ampere / consumer GPUs
# ============================================================================
# Build: docker build -t imagelab-runpod .
# Run:   docker run --gpus all -p 8188:8188 \
#          -e CIVITAI_TOKEN=... \
#          -e IMAGELAB_AUTO_DOWNLOAD=... \
#          -v /path/to/models:/workspace/ComfyUI/models \
#          imagelab-runpod
#
# All model downloading is owned by the imagelab_v3_extension custom node
# (CIVITAI_TOKEN + IMAGELAB_AUTO_DOWNLOAD). This image only sets up the
# environment, ComfyUI itself, and the custom nodes the web app's workflow
# graph depends on.
# ============================================================================

FROM nvidia/cuda:12.8.1-cudnn-devel-ubuntu24.04

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV PIP_NO_CACHE_DIR=1
ENV PIP_PREFER_BINARY=1
ENV CMAKE_BUILD_PARALLEL_LEVEL=8

# System deps: git for clones, build-essential + ninja for SageAttention,
# libgl/glib/etc for OpenCV-using custom nodes, jq for startup helpers.
RUN apt-get update && apt-get install -y --no-install-recommends \
        git \
        curl \
        wget \
        ca-certificates \
        python3 \
        python3-venv \
        python3-pip \
        python3-dev \
        build-essential \
        ninja-build \
        libgl1 \
        libglib2.0-0 \
        libsm6 \
        libxext6 \
        libxrender-dev \
        libgomp1 \
        jq \
    && rm -rf /var/lib/apt/lists/*

# Isolated venv keeps the system Python untouched on RunPod.
RUN python3 -m venv /opt/venv
ENV PATH=/opt/venv/bin:$PATH
RUN pip install --upgrade pip setuptools wheel

# PyTorch nightly w/ CUDA 12.8 — required for Blackwell (B200/GB200/5090).
RUN pip install --pre torch torchvision torchaudio \
      --index-url https://download.pytorch.org/whl/nightly/cu128

# Triton up front so the SageAttention install step is fast at boot.
RUN pip install triton

# A handful of deps the startup script and most custom nodes pull in.
RUN pip install pyyaml huggingface_hub torchsde einops packaging aiohttp

WORKDIR /workspace

# Startup
COPY startup.sh /startup/startup.sh
RUN chmod +x /startup/startup.sh

# Model dir scaffolding (RunPod mounts a volume on top of /workspace/ComfyUI/models
# in most templates; pre-creating the tree means the imagelab_v3_extension node
# finds it whether or not a volume is mounted).
RUN mkdir -p /workspace/ComfyUI/models/checkpoints \
          /workspace/ComfyUI/models/vae \
          /workspace/ComfyUI/models/loras \
          /workspace/ComfyUI/models/controlnet \
          /workspace/ComfyUI/models/upscale_models \
          /workspace/ComfyUI/models/embeddings \
          /workspace/ComfyUI/models/clip \
          /workspace/ComfyUI/models/clip_vision

EXPOSE 8188

# Defaults — override at runtime.
ENV CIVITAI_TOKEN=""
ENV IMAGELAB_AUTO_DOWNLOAD=""
ENV HF_TOKEN=""
ENV SAGE_ATTENTION="true"
ENV COMFYUI_ARGS=""

CMD ["/startup/startup.sh"]
