docker安装tensorflow-gpu版本(ubuntu系统)

1.安装docker

1)卸载可能存在的旧版本
sudo apt-get remove docker docker-engine docker-ce docker.io
2)更新apt包索引
sudo apt-get update
3)安装以下包以使apt可以通过HTTPS使用存储库(repository)
$ sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
4)添加Docker官方的GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
5)再次更新apt包索引
sudo apt-get update
6)安装docker-ce
sudo apt-get install -y docker-ce
7)验证docker
   systemctl status docker 查看docker状态
   systemctl start docker  启用docker服务
   sudo docker run hello-world 经典hello-world程序实验

2.安装invidia-docker

# 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

3.安装tensorflow-gpu版本

    https://tensorflow.google.cn/install/docker  

#拉取镜像
docker pull tensorflow/tensorflow:1.11.0-devel-gpu-py3

#使用镜像,进入bash环境
docker run --runtime=nvidia -it tensorflow/tensorflow:1.11.0-devel-gpu-py3 bash

#使用镜像,执行python指令
docker run --runtime=nvidia -it tensorflow/tensorflow:1.11.0-devel-gpu-py3 \
python -c "import tensorflow as tf;a=tf.constant(1);b=tf.constant(2);c=tf.add(a,b);sess=tf.Session();result=sess.run(c);print('The result is',result)"

#使用镜像,执行本地python程序(装载主机目录并更改容器的工作目录 (-v hostDir:containerDir -w workDir)):
docker run --runtime=nvidia -it --rm -v $PWD:/tmp -w /tmp tensorflow/tensorflow \
python ./script.py

总结:1.主机需要安装 NVIDIA® 驱动程序(无需安装 NVIDIA® CUDA® 工具包)

            2.成功安装docker-ce和invidia-docker。

            3.拉取对应的tensorflow/tensorflow镜像

你可能感兴趣的:(深度学习)