access: 267606
Dockerを使ってローカルにComfyUIをインストールします。
Dockerで動作させるための場所を作成します。今回は、~/localai/comfyui というディレクトリを作成して、そこにファイルを置くことにします。
インストール後、ファイルで指定したモデルをダウンロードします。~/localai/comfyui/models に配置します。 Docker Composeファイルを作成します。今回は、~/localai/comfyui/docker-compose.yml というファイルを作成して、以下の内容を記述します。
Dockerファイルを作成します。今回は、~/localai/comfyui/Dockerfile というファイルを作成して、以下の内容を記述します。
entrypoint.shファイルを作成します。今回は、~/localai/comfyui/entrypoint.sh というファイルを作成して、以下の内容を記述します。
~/localai/comfyui/config/ ディレクトリを作成し、モデルの構成ごとに設定ファイルを作成します。
設定ファイル名は models.*.txt とします。1つの設定ファイルに複数のモデルを記述でき、それぞれの配置先を指定できます。
まずはdefaultの設定ファイルをmodels.default.txtとして作成します。
初回起動時は、モデルのダウンロードに時間がかかる場合があります。ダウンロードが完了するまで待ちます。
動作ログも確認できます。
ブラウザで http://localhost:8188 にアクセスして、ComfyUIが表示されることを確認します。
初期フローを表示して、画像が生成できることを確認します。
前準備
動作場所の作成
$ mkdir -p ~/localai/comfyui
設定の概要
手動で配置してもいいのですが、models.*.txt に記述しておくと、起動時に自動でダウンロードされるようにスクリプトを組みます。
Docker Composeファイル
services:
comfyui:
build: .
container_name: comfyui
runtime: nvidia
restart: always
environment:
- NVIDIA_VISIBLE_DEVICES=all
ports:
- "8188:8188"
volumes:
- ./models:/workspace/ComfyUI/models
- ./output:/workspace/ComfyUI/output
- ./custom_nodes:/workspace/ComfyUI/custom_nodes
- ./config:/workspace/config:ro
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
Dockerファイル
FROM nvidia/cuda:13.4.2-runtime-ubuntu26.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
git \
python3 \
python3-pip \
python3-venv \
wget \
libgl1 \
libglib2.0-0 \
rsync \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /workspace
RUN git clone https://github.com/comfyanonymous/ComfyUI.git /workspace/ComfyUI
WORKDIR /workspace/ComfyUI/custom_nodes
RUN git clone https://github.com/ltdrdata/ComfyUI-Manager.git
RUN mkdir -p /opt/defaults/models \
/opt/defaults/custom_nodes && \
cp -a /workspace/ComfyUI/models/. /opt/defaults/models/ && \
cp -a /workspace/ComfyUI/custom_nodes/. /opt/defaults/custom_nodes/
WORKDIR /workspace/ComfyUI
RUN python3 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
RUN pip install --no-cache-dir -r requirements.txt
COPY entrypoint.sh /workspace/entrypoint.sh
EXPOSE 8188
ENTRYPOINT ["bash","/workspace/entrypoint.sh"]
CMD ["python3", "main.py", "--listen", "0.0.0.0"]
entrypoint.shファイル
#!/bin/bash
set -e
is_empty_dir() {
local dir="$1"
[ -d "$dir" ] && [ -z "$(find "$dir" -mindepth 1 -maxdepth 1 2>/dev/null)" ]
}
sync_if_empty() {
local src="$1"
local dst="$2"
mkdir -p "$dst"
if is_empty_dir "$dst"; then
echo "Initializing $dst from $src ..."
if [ -d "$src" ]; then
rsync -av "$src"/ "$dst"/
fi
fi
}
if [ -d /opt/defaults ]; then
sync_if_empty /opt/defaults/models /workspace/ComfyUI/models
sync_if_empty /opt/defaults/custom_nodes /workspace/ComfyUI/custom_nodes
rm -rf /opt/defaults/models /opt/defaults/custom_nodes
if [ -z "$(find /opt/defaults -mindepth 1 -maxdepth 1 -print -quit)" ]; then
rmdir /opt/defaults
fi
fi
MODEL_DIR="/workspace/ComfyUI/models"
for file in /workspace/config/models.*.txt; do
[ -f "$file" ] || continue
echo "Loading model configuration: $(basename "$file")"
while read -r subfolder url rest; do
[ -z "$subfolder" ] && continue
[[ "$subfolder" == \#* ]] && continue
if [ -z "$url" ]; then
echo "ERROR: Invalid configuration: $subfolder" >&2
continue
fi
target_dir="$MODEL_DIR/$subfolder"
mkdir -p "$target_dir"
clean_url="${url%%\?*}"
filename=$(basename "$clean_url")
target_path="$target_dir/$filename"
if [ ! -f "$target_path" ]; then
echo "Downloading $filename -> $subfolder"
tmp_path="${target_path}.part"
rm -f "$tmp_path"
if wget -O "$tmp_path" "$url"; then
mv "$tmp_path" "$target_path"
echo "Download completed: $filename"
else
rm -f "$tmp_path"
echo "ERROR: Download failed: $url" >&2
fi
else
echo "Already exists, skipping: $filename"
fi
done < "$file"
done
exec "$@"
初期配置するモデルを指定する
$ mkdir -p ~/localai/comfyui/config
# 配置先 URL
text_encoders https://huggingface.co/Comfy-Org/z_image_turbo/resolve/main/split_files/text_encoders/qwen_3_4b.safetensors
diffusion_models https://huggingface.co/Comfy-Org/z_image_turbo/resolve/main/split_files/diffusion_models/z_image_turbo_bf16.safetensors
vae https://huggingface.co/Comfy-Org/z_image_turbo/resolve/main/split_files/vae/ae.safetensors
起動
$ cd ~/localai/comfyui
$ sudo docker compose build
$ sudo docker compose up -d
$ sudo docker compose logs -f comfyui
動作確認