环境介绍
操作系统Ubuntu 16.04.5,其中docker 18.09.0已安装,nvidia显卡驱动已安装。
步骤
(1)安装nvidia-docker
nvidia-docker是docker引擎的一个应用插件,专门面向nvidia的GPU。官方github给出的nvidia-docker在Ubuntu 16.04中的安装方法如下:
#If you have nvidia-docker 1.0 installed: we need to remove it and all existing GPU containers
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 -y nvidia-docker
# Add the package repositories
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
# Install nvidia-docker2 and reload the Docker daemon configuration
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd
测试安装是否成功:
# Test nvidia-smi with the latest official CUDA image
docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi
如果nvidia-docker安装成功,系统会显示GPU信息:
(2)启动pytorch容器
拉取合适的pytorch镜像:
docker pull floydhub/pytorch
通过nvidia-docker启动容器,容器名称为torch,容器内目录/workspace挂载于服务器目录~/xuawai/pytorch:
nvidia-docker run -it -d --name="torch" -v ~/xuawai/pytorch:/workspace pytorch/pytorch:latest
以交互模式进入容器:
docker exec -it torch /bin/bash
进入python控制台,可以通过pytorch在docker内使用nvidia显卡:
网友评论