# Get cuda version from https://hub.docker.com/r/nvidia/cuda
FROM nvcr.io/nvidia/pytorch:26.04-py3 AS build-deps

# Set ENV
ENV PIP_BREAK_SYSTEM_PACKAGES=1
ENV PYTHONUNBUFFERED=1
ENV DEBIAN_FRONTEND=noninteractive
ENV USE_CUDA=1
ENV BUILD_SOX=1

# Install system packages to build torchaudio
RUN apt update && \
    apt upgrade -y && \
    apt install -y \
    libavformat-dev \
    libavcodec-dev \
    libavutil-dev \
    libavdevice-dev \
    libavfilter-dev \
    libswresample-dev \
    sox \
    libsox-dev

# 2. Clone and build the torchaudio stable
RUN wget https://github.com/pytorch/audio/archive/refs/tags/v2.11.0.tar.gz && \
    tar -xf v2.11.0.tar.gz && \
    cd audio-2.11.0 && \
    pip install --no-build-isolation --no-deps -v .

# Set working directory
WORKDIR /app

# Clone ComfyUI
RUN git clone https://github.com/comfyanonymous/ComfyUI.git .

# Install depencies
RUN grep -vE "^(torch|#)" requirements.txt | xargs pip install && \
    pip install torchsde imageio controlnet_aux

# Set custom Nodes
WORKDIR /app/custom_nodes

# Install ComfyUi Manager
RUN git clone https://github.com/Comfy-Org/ComfyUI-Manager.git

# Go back to root
WORKDIR /app

# Expose the default ComfyUI port
EXPOSE 8188

# Launch ComfyUI
CMD ["python3", "main.py", "--listen", "0.0.0.0"]