LoginSignup
21
16

More than 5 years have passed since last update.

nvidia-docker2のインストール(On Ubuntu 16.04 LTS)

Last updated at Posted at 2018-03-11

nvidia-dockerをインストールして、DNN環境の構築を先人の方のページを参考にしながら進めています。
nvidia-dockerは、Version 2.0がリリースされているようなので、2.0を使用することにしました。
尚、Nvidiaのドライバはインストール済みです。
ドライバのインストール方法は、こちらの投稿をご参照下さい。

環境

  • OS : Ubuntu 16.04 LTS
  • GPU : NVIDIA GTX 1080Ti
  • PC : DAIV-DGZ510
  • Nvidiaドライバ:nvidia 387.34

Dockerのインストール

Dockerの公式ページの「Install using the repository」に従い、インストールしていきます。

まず、aptをアップデートします。

$ sudo apt update

次に必要なパッケージをインストールします。

$ sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Docker公式のGPGキーを取得・追加します。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

aptリポジトリにdockerのリポジトリを追加します。

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

再度、aptをアップデートします。

$ sudo apt update

Docker CEのインストールを行います。

$ sudo apt-get install docker-ce

この時点でプログラムのデーモンは立ち上がるようです。
psコマンドで確認してみます。

$ ps ax | grep docker
 2108 ?        Ssl    0:21 /usr/bin/dockerd -H fd://
 2205 ?        Ssl    0:19 docker-containerd --config /var/run/docker/containerd/containerd.toml
27377 pts/2    S+     0:00 grep --color=auto docker
$

立ち上がっているようです・・・

Dockerの動作確認

hello-world を実行してみます。
「Hello from Docker!」と表示されているので、問題ないようです。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
$

以下のコマンドで、バージョン確認する。

$ sudo docker version
Client:
 Version:   17.12.1-ce
 API version:   1.35
 Go version:    go1.9.4
 Git commit:    7390fc6
 Built: Tue Feb 27 22:17:40 2018
 OS/Arch:   linux/amd64

Server:
 Engine:
  Version:  17.12.1-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   7390fc6
  Built:    Tue Feb 27 22:16:13 2018
  OS/Arch:  linux/amd64
  Experimental: false
$ 

補足ですが、自分のユーザをdockerグループに所属させれば、sudoしなくてもOKとなります。(hoge: 自分のユーザ)

$ sudo gpasswd -a hoge docker

2018/03/13追記
dockerグループユーザは、root権限を持っていて、「-v」オプションを使用するとホストのroot所有権のディレクトリもコンテナ内へマウントできてしまうらしい。
(回避策は、@DUxCA さんのページで紹介されています)

docker-composeのインストール

nvidia-docker2では、YAMLファイル書式でコンテナを管理できるdocker-composeに対応したらしい。まだ、勉強不足で理解していませんが、こちらのページにdocker-composeの便利さが投稿されていたので、便利になることを期待してインストールしてみます。

公式ページの手順に従い、インストールして行きます。
image.png

最新バージョンとなる1.19.0をダウンロードします。
このコマンドでダウンロードと同時に、/usr/local/bin/docker-composeにインストールされます。

$ sudo curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

インストールが完了したら、パーミッション(実行権限をつける)を変える必要があります。
変更は、以下のコマンドで行います。

$ sudo chmod +x /usr/local/bin/docker-compose

動作確認は、以下のコマンドで行います。
バージョンが出力されれば、OKです。

$ docker-compose --version
docker-compose version 1.19.0, build 9e633ef
$

nvidia-docker 2.0 のインストール

公式ページのQuickstartに従って、インストールして行きます。
image.png

私の環境には、nvidia-docker 1.0をインストールしていません。
インストール済みの方は、1.0を削除してから、2.0をインストールする必要があるようです。

以下のコマンドでaptリポジトリに必要なパッケージリポジトリパスを追加します。

$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
$ curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

以下のコマンドでリポジトリをアップデートします。

$ sudo apt-get update

以下のコマンドで、nvidia-docker 2.0をインストールします。

$ sudo apt-get install -y nvidia-docker2

次に、dockerのデーモン設定をリロードします。

$ sudo pkill -SIGHUP dockerd

最後にdocker環境で、nvidia-smiを実行し、動作確認します。
dockerイメージのダウンロード(時間が少しかかる)後、にnvidia-smiが実行されます。

$ docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
Unable to find image 'nvidia/cuda:latest' locally
latest: Pulling from nvidia/cuda
22dc81ace0ea: Pull complete 
1a8b3c87dba3: Pull complete 
91390a1c435a: Pull complete 
07844b14977e: Pull complete 
b78396653dae: Pull complete 
95e837069dfa: Pull complete 
fef4aadda783: Pull complete 
343234bd5cf3: Pull complete 
64e8786fc8c1: Pull complete 
d6a4723d353c: Pull complete 
Digest: sha256:3524adf9b563c27d9a0f6d0584355c1f4f4b38e90b66289b8f8de026a9162eee
Status: Downloaded newer image for nvidia/cuda:latest
Sun Mar 11 05:57:19 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 387.34                 Driver Version: 387.34                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:01:00.0 Off |                  N/A |
|  0%   28C    P0    55W / 250W |      0MiB / 11172MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
$ 

nvidia-smiの結果が表示されれば、OKです。

docker-composeでの確認

docker-composeをインストールしたので、nvidia-smiがdocker-composeで実行できることを確認してみる。

docker-compose.ymlファイルを作成し、以下の内容を記述し保存します。

version: '2.3'
services:
  nvidia:
    image: nvidia/cuda
    runtime: nvidia
    command: nvidia-smi

docker-compose upを実行します。

$ docker-compose up
Recreating mytest01_nvidia_1 ... done
Attaching to mytest01_nvidia_1
nvidia_1  | Sun Mar 11 06:47:24 2018       
nvidia_1  | +-----------------------------------------------------------------------------+
nvidia_1  | | NVIDIA-SMI 387.34                 Driver Version: 387.34                    |
nvidia_1  | |-------------------------------+----------------------+----------------------+
nvidia_1  | | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
nvidia_1  | | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
nvidia_1  | |===============================+======================+======================|
nvidia_1  | |   0  GeForce GTX 108...  Off  | 00000000:01:00.0 Off |                  N/A |
nvidia_1  | |  0%   30C    P0    55W / 250W |      0MiB / 11172MiB |      0%      Default |
nvidia_1  | +-------------------------------+----------------------+----------------------+
nvidia_1  |                                                                                
nvidia_1  | +-----------------------------------------------------------------------------+
nvidia_1  | | Processes:                                                       GPU Memory |
nvidia_1  | |  GPU       PID   Type   Process name                             Usage      |
nvidia_1  | |=============================================================================|
nvidia_1  | |  No running processes found                                                 |
nvidia_1  | +-----------------------------------------------------------------------------+
mytest01_nvidia_1 exited with code 0
$

上記のようにnvidia-smiの結果が表示されれば、OKです。

その他参考ページ

上記、記載内のリンク先の他に以下のページも参考にさせて頂きました。
- 第458回 UbuntuでDocker再入門@Ubuntu Weekly Recipe
- nvidia-docker2を使ってみる@WonderPlanet Tech Blog
- nvidia-dockerでGPUコンテナの作成@uni-3さん

21
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
16