ひゃまだのblog

ひゃまだ(id:hymd3a)の趣味のブログ

Debian 12(bookworm)の Docker で Tensorflow を動かす

(2023-10-25 初稿 - )

Debian 11 (bullseye) から Docker で、TensorFlowを使ってきたけど、12(Bookworm)もマイナーアップデートしていることだし、そろそろDockerの環境を入れ替えて末永く使おうと思った。

ネットで検索すると、Debianpythonが新しすぎるとか、apt がうまく動かないとかの記事があったけど、結論から言うと、問題なく動作させることができた。

といっても、記事によっては古いバージョンの場合もあって、2〜3度は試行錯誤をしたので、自分の環境を以下に示すので参考までに。

$ uname -a
Linux fam 6.1.0-13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.55-1 (2023-09-29) x86_64 GNU/Linux
$ cat /etc/debian_version
12.2

ほぼインストールログのようになってしまった。(^_^;)

Dockerのインストール

Dockerのインストールについては、Dockerのサイトを参考にインストールを行った。

過去にDebianをインストールをしたことがある方は、一度全てを apt remove --purgeで削除し、再起動してから、以下を実行した方が良い。

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Add the repository to Apt sources:
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

上記で、gpgキーをゲットして、aptにソースラインを設定する。

以下で、dockerの関連ファイルをインストールする。

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

無事にインストールできたら、一度、heloo-worldで確認。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

正常にインストールできれば、上記のようなメッセージが出る。

毎回、sudoをつけるのは面倒なので、自分(hoge)をdockerグループに入れる。

$ sudo adduser hoge docker    # Debianのみ
$ usermod -aG docker hoge     # Debian 以外の Linux たぶん

グループに追加したら、一度ログアウトして、ログインし直す。

TensorFlow(CPU版) のインストール

TensorFlowのインストールは、TensorFlowのサイトに従いインストールする。

以下のコマンドで、TensorFlowの最新版をインストールする。訳あって、他のバージョンをインストールする場合は、上記サイトに従ってインストールしてね。

$ docker pull tensorflow/tensorflow 

上記で、CPU版はインストールできたと思うので、以下で確認する。

docker run -it --rm tensorflow/tensorflow \
   python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2023-10-24 11:19:32.691271: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
tf.Tensor(-1365.0759, shape=(), dtype=float32)

途中はともかく最後の行が出力されれば、正常にインストールできているよ。

CPU版の環境を整備するbashスクリプト

毎回、TensorFlow環境を整えるコマンドを打つのは大変なので、簡単なスクリプトを作ったので、参考に。

ちなみに、カレントディレクトリが /tmp となるので、実行したいスクリプトがあるディレクトリで実行してね。

#!/usr/bin/env bash
# docker tensorflow 環境を整備するスクリプト
# ver0.02 2023-06-09 add message

msg="You need following process after tf-env
pip3 install -r requirements.txt"

echo "$msg"
/usr/bin/docker run -it --rm -v $PWD:/tmp -w /tmp tensorflow/tensorflow bash

TensorFlow (GPU版)のインストール

nVidiaビデオカードを使っている場合は、GPU版のTensorFlowが使える。

やはり配布元のnvidiaのサイトに従ってインストールする。

以下のコマンドで、GPGキーとaptライン(nvidia-container-tootlkit.list)を作る。

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
  && \
    sudo apt-get update

上記のコマンド後、nvidia-container-toolkitをインストールする。

$ sudo apt-get install -y nvidia-container-toolkit

インストール後の確認は、以下のサイトの情報に基づき行う。

docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu \
   python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
  (中略)
tf.Tensor(-1365.0759, shape=(), dtype=float32)

値は異なっても、上記のようなメッセージがでれば正常にインストールできている。

GPU版の環境を整備するbashスクリプト

GPU版でも同様にbashスクリプトを作った。

#!/usr/bin/env bash
# docker tensorflow 環境を整備するスクリプト
# ver0.02 2023-06-09 add message

msg="You need following process after tf-env
pip3 install -r requirements.txt"

echo "$msg"
/usr/bin/docker run --gpus all -it --rm -v $PWD:/tmp -w /tmp tensorflow/tensorflow:latest-gpu bash

Connmanによるトラブル発生と対策

無事にインストールできたので、早速本格的にpythonスクリプトを動かそうとしたら、connmanがネットワークを途中で遮断して邪魔をする。(笑)

実は、Debian 11(Bullseye)のときにも、原因不明なネットワークエラーで2週間ぐらい悩んだことがあったので、今回は約1日でconnmanが影響していることが推察できた。特徴としては、「ネットワークが最初はうまく動くのに、途中から切断される」ってところ。

筆者は、利用するPCがデスクトップだったので、以下のとおりconnmanをアンインストールしてしまった。

$ sudo apt remove connman

もし、使う場合が想定される場合は、以下のとおりconnmanを一時的にストップさせて、ネットワークサービスを再起動すると良いかも。

$ sudo systemctl stop connman
$ sudo systemctl restart networking 

皆さんのDocker - TensorFlow環境構築の参考になれば幸い。

関連ページ