FROM docker.io/library/python:3.13-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    git libgl1 libglib2.0-0 libsm6 libxext6 libgomp1 \
    && rm -rf /var/lib/apt/lists/*

ENV GIT_TERMINAL_PROMPT=0

WORKDIR /app

RUN git clone --depth 1 https://github.com/comfyanonymous/ComfyUI . \
    && pip install --no-cache-dir torch torchvision torchaudio \
       --index-url https://download.pytorch.org/whl/cpu \
    && pip install --no-cache-dir -r requirements.txt

RUN cd /app/custom_nodes \
    && git clone --depth 1 https://github.com/city96/ComfyUI-GGUF \
    && pip install --no-cache-dir -r ComfyUI-GGUF/requirements.txt

RUN cd /app/custom_nodes \
    && git clone --depth 1 https://github.com/XLabs-AI/x-flux-comfyui \
    && pip install --no-cache-dir -r x-flux-comfyui/requirements.txt

RUN sed -i 's/def forward(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor) -> tuple\[Tensor, Tensor\]:/def forward(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor, attn_mask=None, transformer_options={}, **kwargs):/' /app/custom_nodes/x-flux-comfyui/xflux/src/flux/modules/layers.py \
    && grep "attn_mask" /app/custom_nodes/x-flux-comfyui/xflux/src/flux/modules/layers.py

RUN useradd -u 1000 -m comfyui && chown -R 1000:1000 /app

USER 1000
EXPOSE 8188
CMD ["python", "main.py", "--listen", "0.0.0.0", "--port", "8188", "--cpu"]