NVIDIA-DOCKER2の始め方

Dockerコンテナ内でGPUを使うためのnvidia-docker2が出ていたので、導入手順をまとめました。

Dockerのインストール

Get Docker CE for Ubuntu | Docker Documentation にしたがってDockerをインストールします。

依存パッケージのインストール

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

Docker本体のインストール

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add 
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable
sudo apt-get update
sudo apt-get install docker-ce

動作確認

sudo docker run hello-world

うまく動けば下記のようなメッセージが表示されます。

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/

nvidia-docker2のインストール

Installation (version 2.0) · NVIDIA/nvidia-docker Wiki · GitHub にしたがってnvidia-docker 2.0をインストールします

[option] nvidia-docker 1.0のアンインストール

古いnvidia-dockerを入れている場合は以下のコマンドで先にアンインストールします。

docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo apt-get purge nvidia-docker

nvidia-docker2のインストール

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
sudo apt-get install nvidia-docker2
sudo pkill -SIGHUP dockerd

動作確認

sudo docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

正常動作の場合、Dockerコンテナ内で実行されたnvidia-smiの結果を確認できます。

+-----------------------------------------------------------------------------+
| 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 106...  Off  | 00000000:01:00.0 Off |                  N/A |
| 28%   35C    P8     6W / 120W |    627MiB /  6072MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
+-----------------------------------------------------------------------------+

[option?] nvidia runtimeの登録

docker: Error response from daemon: Unknown runtime specified nvidia.
See 'docker run --help'.

こんなエラーメッセージが出た場合は、以下のコマンド群でnvidia-container-runtimeを登録してもう一度動作確認してください。

sudo tee /etc/docker/daemon.json <<EOF
{
    "runtimes": {
        "nvidia": {
            "path": "/usr/bin/nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}
EOF
sudo pkill -SIGHUP dockerd