Quantumleap
784 words
4 minutes
Stable Diffusion WebUI+DockerをUbuntu Serverで動かす

概要#

Stable Diffusion WebUIをDockerで動かすというのは前々からある試みである。

有名所だとstable-diffusion-webui-dockerが頻繁に更新されている。

で、以前はこれをUbuntu DesktopからDocker Desktopを導入して環境構築していたのだが、よく考えたら別にDesktop環境は要らないので最も軽いUbuntu Serverで動かせばいいじゃないかという話になった。

ちゃんと書いたつもりですが、ミスっていたらごめんなさい

Docker#

まず、不要なものがインストールされている可能性があるので全て削除する。

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

削除したらパッケージを登録する。

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

最後にインストールをする。

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

ここでdocker infoPermission deniedが表示される場合は権限が足りていないので、

sudo usermod -aG docker $USER

現在のユーザーをdockerグループに追加することでsudoなしでdockerが動かせるようになる。

そしてコンピュータを再起動します。

Install Docker Engine on Ubuntu

Nvidia Container Runtime#

DockerコンテナからはデフォルトではGPUが認識できないのでこれを入れます。

sudo apt-get install nvidia-container-runtime
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey |   sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list |   sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update
sudo apt-get install nvidia-container-runtime
service docker restart

docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]]. ERRO[0000] error waiting for container: context canceledを解決する

導入#

Stable Diffusion WebUIはDockerを使って導入します。

環境が汚れないのでこちらの方がよいです。

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

デフォルトの状態だとTensorRTを有効化するとバグって動かなくなってしまうので、TensorRTのextensionのインストールがうまくいくように修正します。

最新のコミットのバグ?

stable-diffusion-webui-docker/services/AUTOMATIC1111/Dockerfileを編集します。

# TensorRT
RUN pip install --pre --extra-index-url https://pypi.nvidia.com tensorrt==9.0.1.post11.dev4
RUN pip install polygraphy --extra-index-url https://pypi.ngc.nvidia.com

上の三行を後ろの方のどっかに適当に付け加えます。私は以下のようにしました。

RUN --mount=type=cache,target=/root/.cache/pip \
  pip install pyngrok xformers==0.0.23.post1 \
  git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 \
  git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 \
  git+https://github.com/mlfoundations/[email protected]

# TensorRT
RUN pip install --pre --extra-index-url https://pypi.nvidia.com tensorrt==9.0.1.post11.dev4
RUN pip install polygraphy --extra-index-url https://pypi.ngc.nvidia.com

# there seems to be a memory leak (or maybe just memory not being freed fast enough) that is fixed by this version of malloc
# maybe move this up to the dependencies list.
RUN apt-get -y install libgoogle-perftools-dev && apt-get clean
ENV LD_PRELOAD=libtcmalloc.so

これで保存したら以下のコマンドで起動します。

このようにすることでTensorRTの動作に必要なパッケージをTensorRTインストール時ではなくあらかじめStable Diffusion WebUIにインストールすることができます。もちろん、TensorRTを使わないつもりならこの設定は不要です。

最初は時間がかかりますが、二回目以降はすぐに立ち上がります。

docker compose --profile download up --build
docker compose --profile auto up --build

記事は以上。

Stable Diffusion WebUI+DockerをUbuntu Serverで動かす
https://fuwari.vercel.app/posts/2024/02/stable_diffusion_server/
Author
tkgling
Published at
2024-02-06