# Docker buildfile for the ComfyUI image, with support for hardware
# acceleration, file ownership synchronization, custom nodes, and custom node
# managers.

# Use the recommended Python version 3.12, as specified in the README.
FROM python:3.12.11-bookworm

# Install cmake, which is an indirect installation dependencies
RUN apt-get update && apt-get install -y --no-install-recommends cmake

# Create a mount point for user-generated data.
RUN mkdir -p      \
	/data/input   \
	/data/output  \
	/data/temp    \
	/data/user

# Create a regular user whose UID and GID will match the host user's at runtime.
# Also create a home directory for this user (-m), as some common Python tools
# (such as uv) interact with the user’s home directory.
RUN useradd -m comfyui

# Install ComfyUI under /comfyui and set folder ownership to the comfyui user.
# With the legacy Docker builder (DOCKER_BUILDKIT=0), WORKDIR always creates missing
# directories as root (even if a different USER is active). To ensure the comfyui user
# can write inside, ownership must be fixed manually.
WORKDIR /comfyui
RUN chown comfyui:comfyui .

# Install ComfyUI as ComfyUI
USER comfyui

# Set up a Python virtual environment and configure it as the default Python.
#
# Reasons for using a virtual environment:
# - Some custom nodes use third-party tools like uv, which do not support
#   user-level installations.
# - Custom node managers may install or update dependencies as the regular user,
#   so a global installation is not an option.
# This leaves virtual environments as the only viable choice.
RUN python -m venv .venv
ENV PATH="/comfyui/.venv/bin:$PATH"

# Install Python dependencies. This step is also performed automatically by the
# entrypoint script, but doing it at build time reduces startup time on the
# first run.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Install ComfyUI and link the data mount points.
COPY . .
RUN ln -sf /data/* .

# Purely declarative: inform Docker and image users that this image is designed
# to listen on port 8188 for the web GUI.
EXPOSE 8188

# Declare persistent volumes:
# - /data: stores user-generated data from ComfyUI,
# - /comfyui/.venv: stores Python data generated by the entrypoint and custom
#   node managers,
# - /comfyui/custom_nodes: stores custom nodes installed at runtime by custom
#   node managers,
# - /comfyui/models: Stores models installed by model managers,
# - /home/comfyui: stores data from Python packages that may write outside the
#   virtual environment and into the user’s home directory.
VOLUME [ "/data", "/comfyui/.venv", "/comfyui/custom_nodes", "/comfyui/models", "/home/comfyui" ]

# Switch back to root to run the entrypoint and to install additional system
# dependencies
USER root

# Configure entrypoint
RUN chmod +x entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]
CMD [ "python", "./main.py" ]

# Install additional system dependencies
ARG APT_EXTRA_PACKAGES
RUN apt-get install -y --no-install-recommends $APT_EXTRA_PACKAGES \
	&& apt-get clean                                               \
	&& rm -rf /var/lib/apt/lists/*
