FROM nvidia/cuda:12.8.1-cudnn-runtime-oraclelinux8

# Get release version from https://github.com/comfyanonymous/ComfyUI/releases
ENV ComfyUI_Release=0.3.43

# Install build dependencies
RUN dnf update -y && dnf install -y \
    gcc \
    gcc-c++ \
    make \
    wget \
    libffi-devel \
    openssl-devel \
    bzip2-devel \
    zlib-devel \
    xz-devel \
    readline-devel \
    sqlite-devel && \
    dnf clean all

# Set workdir for python
WORKDIR /usr/src

# Download Python 3.11 source code (adjust version as needed)
RUN wget https://www.python.org/ftp/python/3.11.6/Python-3.11.6.tgz && \
    tar xzf Python-3.11.6.tgz

# Set new workdir for python
WORKDIR /usr/src/Python-3.11.6

# Build and install Python 3.11
RUN ./configure --enable-optimizations && \
    make -j$(nproc) && \
    make altinstall

# Set python3.11 as default python and pip
RUN ln -sf /usr/local/bin/python3.11 /usr/bin/python && \
    ln -sf /usr/local/bin/pip3.11 /usr/bin/pip

# Set workdir for application download
WORKDIR /app

# Pull source and extract
RUN wget https://github.com/comfyanonymous/ComfyUI/archive/refs/tags/v${ComfyUI_Release}.tar.gz && \
        tar xzf v${ComfyUI_Release}.tar.gz && \
        rm v${ComfyUI_Release}.tar.gz

# Set workdir for application
WORKDIR /app/ComfyUI-${ComfyUI_Release}

# Upgrade pip and install dependencies
RUN python -m pip install --upgrade pip && \
    python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu128 && \
    python -m pip install -r requirements.txt

# Expose the port
EXPOSE 8188

# Start CompyUI
CMD ["python", "main.py"]
