はじめに
この記事はDockerでDeep LearningやってみたいけどCUIはめんどくさいという人向けの記事です。
Docker と nvidia-dockerのインストール方法等はもっとわかりやすい記事があるので、そちらをご覧ください。
Docker入門 ~Tensorflowも超簡単に!!~
https://qiita.com/yakigac/items/f14f12f182a564c091d0
今回の最終目標はDockerコンテナでTensorflow-gpuを動かすことです。
動作環境
- Ubuntu 18.04.2 LTS
- Docker 18.09.5
- nvidia-docker 2.0.3
- portainer 1.20.2
portainarインストール
以下のコマンドを順に実行
docker volume create portainer_data
docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
ブラウザからhttp://localhost:9000
で接続できるます。
ポートを変えたいときはコマンド内の9000
の部分を置き換えればいいです。
Deployment — Portainer 1.20.2 documentation
https://portainer.readthedocs.io/en/stable/deployment.html
portainerの初期設定
下の画面が表示されるので、ユーザー登録をします。
![]() |
---|
続いて、どこのDockerに接続するか聞かれるのでlocal
を選択。
![]() |
---|
Home
に移動できるので、Endpoints
の中のlocal
をクリックするとDashboard
に移動できます。
![]() |
---|
![]() |
---|
基本的にportainerはDashboard
から操作します。次回からはログインをするとHome
を開けます。
コンテナを作る
今回はTensorflow公式のイメージを使用します(公式ページ)。
使用するイメージはtensorflow/tensorflow:latest-gpu-py3
としました。
(tensolflowの最新バージョンのGPU版でpython3系のイメージ)
コンテナの一覧を開く
Dashboard
のContainers
を選択すると, 以下のように現在のコンテナ一覧が表示されます(起動していないものも含む)。
![]() |
---|
コンテナの新規作成画面を開く
Container list
のAdd container
をクリックするとCreate container
画面が開きます。
![]() |
---|
コンテナ名とイメージ名を入力
Name
にコンテナ名を入力する。今回はコンテナ名をTensorflowとしました。
先ほどのイメージ名を入力。このイメージはDockerHubにあるのでRegistry
はDockerHubでいいです。
![]() |
---|
Interactive & TTYを有効にする
Docker内のコンソールに接続できるようにInteractive & TTY
を有効にします。
docker run
のコマンドのオプションで言うと-it
です。
変更する場所は、Create container
画面の下側のAdvanced container settings
内のCommand & logging
です。
Console
のInteractive & TTY
のチェックします。
![]() |
---|
ランタイムの変更
GPUを使用するコンテナを作成するときは、nvidia-dockerを使用することを明示しなければなりません。
docker run
のオプションで言うと--runtime=nvidia
の部分です。
変更する場所は、Create container
画面の下側のAdvanced container settings
内のRuntime & Resources
です。
Runtime
をnvidia
に変更してください。
![]() |
---|
共有フォルダの作成
ホスト側とコンテナ内に共有フォルダを作成します。必須ではありませんがdocker cp
とかしなくていいので楽です。
Create container
画面の下側のAdvanced container settings
内のVolumes
から設定できます。
Volume mapping
のmap additional volume
をクリックしてボリュームを追加します。
Bind
とWritable
を選択します。
container
側にコンテナ側の共有フォルダのパス、host
側にホスト側の共有フォルダのパスを入力します。
![]() |
---|
コンテナを作成
最後にDeploy the container
をクリックすればコンテナが作成されます。
![]() |
---|
共有フォルダの権限変更
今のままでは共有フォルダの権限のせいでホスト側から書き込めません。
なので、コンテナ内から権限を付与する必要があります(もっといい方法あれば教えてください…)。
Container list
の作成したコンテナのQuick actions
の>_
をクリックすると、Container console
画面が開くので、Connect
をクリック。
![]() |
---|
するとコンソールが開くので、次のコマンドを入力。
chmod a+rwx <共有フォルダのパス>
これでTensorflowが動くコンテナ完成です。
動作確認
コンテナ内のコンソールで以下を実行してください。
python -c "from tensorflow.python.client import device_lib;device_lib.list_local_devices()"
実行結果
2019-05-02 19:53:24.463116: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2FMA
2019-05-02 19:53:24.601321: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-05-02 19:53:24.610259: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-05-02 19:53:24.610914: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x4cbebd0 executing computations on platform CUDA. Devices:
2019-05-02 19:53:24.610929: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): GeForce GTX 1080 Ti, Compute Capability 6.1
2019-05-02 19:53:24.610934: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (1): GeForce GTX 1080 Ti, Compute Capability 6.1
2019-05-02 19:53:24.630432: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3192000000 Hz
2019-05-02 19:53:24.631122: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x4d305d0 executing computations on platform Host. Devices:
2019-05-02 19:53:24.631140: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined>
2019-05-02 19:53:24.631495: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433]Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6325
pciBusID: 0000:01:00.0
totalMemory: 10.91GiB freeMemory: 10.68GiB
2019-05-02 19:53:24.631743: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433]Found device 1 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.6325
pciBusID: 0000:02:00.0
totalMemory: 10.92GiB freeMemory: 10.77GiB
2019-05-02 19:53:24.632553: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512]Adding visible gpu devices: 0, 1
2019-05-02 19:53:24.633699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-05-02 19:53:24.633719: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0 1
2019-05-02 19:53:24.633727: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003]0: N Y
2019-05-02 19:53:24.633733: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003]1: Y N
2019-05-02 19:53:24.634226: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115]Created TensorFlow device (/device:GPU:0 with 10389 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
2019-05-02 19:53:24.634577: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115]Created TensorFlow device (/device:GPU:1 with 10479 MB memory) -> physical GPU (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:02:00.0, compute capability: 6.1)
GPUが認識できているのがわかります。
最後に
コンテナがGUIで全部使えるのほんとに楽でいいです。portainerすごい。
Deep LearningをちゃんとやっていくにはDockerは必須なんじゃないかなぁと個人的には思うので、もっと勉強しなきゃと思う次第です。
portainerには、まだよくわかってない機能がたくさんありそうなのでもっと使いこなしていきたい所存。
時間があったら、portainerでimageビルドする記事も書きます。