1、安装最新版docker
使用docker的好处是不用再自己处理各种依赖库和CUDA版本不兼容的问题(下带CUDA的镜像就好了)。
根据docker官网 的教程进行docker的安装:
#### 如果装有旧版, 先卸载
sudo apt-get remove docker docker-engine docker.io containerd runc
#### 设置源
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
#### 安装docker-ce
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
#### 测试是否安装成功
sudo docker run hello-world
2、安装最新版nvidia-docker(现在叫NVIDIA Container Toolkit)
根据NVIDIA Container Toolkit官网上的教程进行安装:
#### 添加软件源
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
#### 安装NVIDIA Container Toolkit
sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit
#### 重启docker
sudo systemctl restart docker
#### 测试NVIDIA Container Toolkit是否安装成功,如果输出显卡驱动信息则安装成功
docker run --gpus all nvidia/cuda:9.0-base nvidia-smi
3、docker使用
docker使用中的常用命令可以参考[1][2],下面是如何在docker中启用NVIDIA Container Toolkit的例子:
# Start a GPU enabled container on two GPUs
$ docker run --gpus 2 nvidia/cuda:9.0-base nvidia-smi
# Starting a GPU enabled container on specific GPUs
$ docker run --gpus '"device=1,2"' nvidia/cuda:9.0-base nvidia-smi
$ docker run --gpus '"device=UUID-ABCDEF,1"' nvidia/cuda:9.0-base nvidia-smi
# Specifying a capability (graphics, compute, ...) for my container
# Note this is rarely if ever used this way
$ docker run --gpus all,capabilities=utility nvidia/cuda:9.0-base nvidia-smi
[1] Docker 容器使用 https://www.runoob.com/docker/docker-container-usage.html
[2] Docker 镜像使用 https://www.runoob.com/docker/docker-image-usage.html