dockerのdefault runtimeをnvidia(GPUが使えるruntime)にする




# 概要
dockerのdefault runtimeをnvidiaにする設定方法について記載します。
この設定をすることで、dockerで--gpusオプションをつけなくてもGPUが使えるようになります。

# 前提
nvidia container toolkitがインストール済みであること。

参考：
以下は公式のnvidia container toolkitのインストール手順です。
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html


# 設定前
--gpusオプションを付けないとGPUを利用できません。


```:--gpusを付けた場合
# sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi
Tue Dec  8 14:51:49 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.45.01    Driver Version: 455.45.01    CUDA Version: 11.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  On   | 00000000:00:07.0 Off |                    0 |
| N/A   32C    P0    26W / 250W |      0MiB / 16280MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
```


```:--gpusを付けない場合
# sudo docker run --rm  nvidia/cuda:11.0-base nvidia-smi
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"nvidia-smi\": executable file not found in $PATH": unknown. 
```

# 設定方法
/etc/docker/daemon.jsonに``"default-runtime": "nvidia"``を加えてサービスを再起動します。

```
# vi /etc/docker/daemon.json
# cat /etc/docker/daemon.json 
{
    "default-runtime": "nvidia",  # <-追加
    "runtimes": {
        "nvidia": {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}
# systemctl restart docker
# docker info 2> /dev/null | grep -i runtime
Runtimes: nvidia runc
Default Runtime: nvidia

```

# 確認
--gpusオプションなしでもGPUが使える

```
# sudo docker run --rm  nvidia/cuda:11.0-base nvidia-smi
Tue Dec  8 15:11:11 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 455.45.01    Driver Version: 455.45.01    CUDA Version: 11.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  On   | 00000000:00:07.0 Off |                    0 |
| N/A   32C    P0    26W / 250W |      0MiB / 16280MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
```