😸

DockerでStable Diffusionを動かす(GPU版)

2023/02/14に公開約4,100字

1. はじめに

Dockerを使って、Stable Diffusion(ディープラーニングによるテキスト→画像作成ツール)を構築します。
本内容は、WindowsでGPUを使用して動かす方法について記載しています。

2. 事前準備

Windows版は以下の環境で実施しました。

OS Windows 11 Home
GPU nVIDIA Geforce RTX3060 12GB
メモリ 16GB

2.1 Docker Desktopのダウンロード、インストール

以下URLからインストーラ(.exe)をダウンロードします。
https://docs.docker.com/desktop/install/windows-install/
ダウンロードしたファイルを実行し、Docker Desktopをインストールします。
Docker DesktopにはDocker Composeも含まれています。

2.2 Gitのダウンロード、インストール

以下URLからインストーラ(.exe)をダウンロードします。
https://gitforwindows.org/
ダウンロードしたファイルを実行し、Gitをインストールします。

2.3 NVIDIA CUDA Toolkitのインストール

以下URLからNVIDIA CUDA Toolkitをインストールします。
23/2/14現在 最新版はCUDA 12.0.0でしたが、
12.0.0では私の環境ではインストールできなかったので、CUDA 11.8.0(ひとつ前)をダウンロードしました。
以下から「CUDA Toolkit 11.8.0」のリンクをたどり、ダウンロードしました。
私のPC環境でインストール前のnVIDIAのドライバのバージョンが472.12だったのが関係しているのかもしれません。
https://developer.nvidia.com/cuda-toolkit-archive

(参考)NVIDIA CUDA Toolkitの最新版インストール。
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

2.4 wslの設定変更

StableDiffusionなど、メモリを大量に使用するコンテナを動かす場合は、メモリの割当を増やす必要があります。

LinuxではDocker Desktopの設定でメモリの変更ができましたが、Windows版ではwslのコンフィグファイルに直接記載する必要がありました。
「C:\Users\【ユーザ名】\.wslconfig」ファイルを新しく作成し、

.wslcomfig
[wsl2]
memory=13GB

の記述を追加します。

.wslconfig ファイルを変更したら一度PCを再起動します。

3. Stable Diffusion web UI (AUTOMATIC1111)のダウンロード

端末より以下のコマンドを実行し、Stable Diffusion WebUI Dockerをcloneします。

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

4. Stable Diffusionのコンテナを定義

端末より以下のコマンドを実行し、Stable Diffusion のコンテナを定義します。

cd stable-diffusion-webui-docker
docker compose --profile download up --build

正常に終了しない場合、2行目のコマンドを再度実行することで上手く行くことがあります。

5. Stable Diffusionを実行

端末より以下のコマンドを実行し、Stable Diffusion(GPU版) を実行します。

docker compose --profile auto up --build

Docker DesktopでStable Diffusionが動いていることを確認します。

6. ブラウザから起動

ブラウザより以下のアドレスを入力します。
http://localhost:7860

7. トラブルシューティング

Stable Diffusion を動かしているときにDockerDesktopでエラーコード137(以下の画面)が出る場合は、以下が参考になります。

7.1 GPUがDocker内で認識できているかの確認

端末から以下のコマンドを実行することで、nVIDIAがDocker内で認識できているか確認することができます。

docker run -it --gpus=all --rm nvidia/cuda:11.8.0-base-ubuntu22.04 /bin/bash
root@hostname:/#
nvidia-smi

表示例ですが、以下のように表示されていればDocker内で認識できています。

7.2 GPUメモリのカスタマイズ

GPUのメモリが足りない場合、以下のホームページが参考になるかもしれません。
https://stealthinu.hatenadiary.jp/entry/2022/10/07/160746

7.3 Dockerで使用するメモリのカスタマイズ

Dockerで使用するメモリを増やすことで上手く行ったことがあります。(上記「wslの設定変更」を参照)

.wslconfigの内容については以下ホームページも参考になりました。
https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig
以下が.wslconfigファイルのサンプルです。(stable diffusion向けではないのでご注意ください。)

.wslconfig
# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB 

# Sets the VM to use two virtual processors
processors=2

# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
kernel=C:\\temp\\myCustomKernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false

# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true

Discussion

ログインするとコメントできます