Skip to content

Docker 部署 SD WebUI 和 ComfyUI

1. 构建镜像

使用项目 stable-diffusion-webui-docker 来构建镜像,实现同时搭建 SD WebUI 和 ComfyUI。

首先,克隆仓库:

bash
git clone https://github.com/AbdBarho/stable-diffusion-webui-docker.git
cd stable-diffusion-webui-docker

下面构建镜像,如果不希望自行构建可以直接拉取预构建的镜像,然后跳转到 2. 配置并使用代理

预构建镜像

本文作者已经构建好了镜像 alexsuntop/sd-autoalexsuntop/sd-comfy,可以直接拉取:

bash
docker pull alexsuntop/sd-comfy:0.2.7-t2.3.0-cu121-8
docker pull alexsuntop/sd-auto:1.9.4-t2.3.0-cu121-8

可以通过修改 services/AUTOMATIC1111/Dockerfileservices/comfy/Dockerfile 文件来选择 ComfyUI 的版本和基础镜像版本。

例如,修改 ComfyUI 的 Dockerfile,使用最新的 PyTorch 镜像,并且使用 ComfyUI 的最新版本:

dockerfile
FROM pytorch/pytorch:2.3.0-cuda12.1-cudnn8-runtime
FROM pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime

ENV DEBIAN_FRONTEND=noninteractive PIP_PREFER_BINARY=1

RUN apt-get update && apt-get install -y git && apt-get clean

ENV ROOT=/stable-diffusion
RUN --mount=type=cache,target=/root/.cache/pip \
  git clone https://github.com/comfyanonymous/ComfyUI.git ${ROOT} && \
  cd ${ROOT} && \
  git checkout master && \
  git reset --hard 276f8fce9f5a80b500947fb5745a4dde9e84622d && \ 
  git reset --hard v1.10.0 && \ 
  pip install -r requirements.txt

WORKDIR ${ROOT}
COPY . /docker/
RUN chmod u+x /docker/entrypoint.sh && cp /docker/extra_model_paths.yaml ${ROOT}

ENV NVIDIA_VISIBLE_DEVICES=all PYTHONPATH="${PYTHONPATH}:${PWD}" CLI_ARGS=""
EXPOSE 7860
ENTRYPOINT ["/docker/entrypoint.sh"]
CMD python -u main.py --listen --port 7860 ${CLI_ARGS}

不建议修改 SD WebUI 的 Dockerfile,因为 SD WebUI 的版本比较复杂,难以维护。

构建 SD WebUI 镜像:

bash
docker compose --profile auto up --build

构建 ComfyUI 镜像:

bash
docker compose --profile comfy up --build

如果构建失败则可能需要设置 Docker Daemon 的代理。

2. 配置并使用代理

可以将 docker-compose.yml 文件中的 --medvram 替换为其他参数,可以根据电脑的显卡配置来选择。

很多模型需要代理才能正常工作,可以修改 docker-compose.yml 文件,添加环境变量 HTTPS_PROXY

yaml
x-base_service: &base_service
    ports:
      - "${WEBUI_PORT:-7860}:7860"
    volumes:
      - &v1 ./data:/data
      - &v2 ./output:/output
    stop_signal: SIGKILL
    tty: true
    deploy:
      resources:
        reservations:
          devices:
              - driver: nvidia
                device_ids: ['0']
                capabilities: [compute, utility]
    extra_hosts: 
      - host.docker.internal:host-gateway

name: webui-docker

services:
  download:
    build: ./services/download/
    profiles: ["download"]
    volumes:
      - *v1

  auto: &automatic
    <<: *base_service
    profiles: ["auto"]
    build: ./services/AUTOMATIC1111
    image: sd-auto:78
    environment:
      - CLI_ARGS=--allow-code --medvram --xformers --enable-insecure-extension-access --api
      - HTTPS_PROXY=http://host.docker.internal:7890

  comfy: &comfy
    <<: *base_service
    profiles: ["comfy"]
    build: ./services/comfy/
    image: sd-comfy:7
    environment:
      - CLI_ARGS=
      - HTTPS_PROXY=http://host.docker.internal:7890
    volumes: 
      - *v1 
      - *v2 
      - ./custom_nodes:/stable-diffusion/custom_nodes
      - ./comfy-models:/stable-diffusion/models

3. 启动容器

启动 SD WebUI:

bash
WEBUI_PORT=7860 docker compose --profile auto up -d

启动 ComfyUI:

bash
WEBUI_PORT=17860 docker compose --profile comfy up -d

4. ComfyUI 可能的额外操作

模型文件夹

共享的 CLIP 模型放在 ./data/models/CLIPEncoder 文件夹下,ComfyUI 私有的模型放在 ./comfy-models 文件夹下。

这个部分可以写入 Dockerfile 文件中以便进行自动化构建。

进入容器:

bash
docker exec -it webui-docker-comfy-1 bash

安装插件

./data/config/comfy/custom_nodes 文件夹下安装的插件不能正常工作,请到容器内安装 ComfyUI-Manager 插件,然后通过此插件安装其他插件。

安装 ComfyUI-Manager:

bash
cd /stable-diffusion/custom_nodes/
git clone https://github.com/ltdrdata/ComfyUI-Manager.git

安装两个版本的 OpenCV:

bash
pip uninstall opencv-python opencv-python-headless -y
pip uninstall opencv-contrib-python opencv-contrib-python-headless -y
pip install opencv-python opencv-contrib-python
pip install opencv-python-headless opencv-contrib-python-headless

退出容器,然后重启容器:

bash
docker restart webui-docker-comfy-1
docker logs -ft webui-docker-comfy-1 -n 100

5. 相关项目

可参考 Docker 部署 Fooocus 文档,使用 Docker 部署 Fooocus。