How to use Tensorflow-gpu with Docker

Rouyun Pan
2 min readJan 9, 2019

Install Software package for tensorflow-GPU with Docker

#install GPU driver

$sudo add-apt-repository ppa:graphics-drivers
$sudo apt-get update
$sudo apt install nvidia-390

#Uninstall old versions

$sudo apt-get remove docker docker-engine docker.io 
$sudo apt-get update

#Install packages to allow apt to use a repository over HTTP

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

#Add Docker’s official GPG key

$curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$sudo apt-key fingerprint 0EBFCD88

#set up the stable repository

$sudo add-apt-repository "deb [arch=amd64] $https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

#INSTALL DOCKER CE (Community Edition)

$sudo apt-get update
$sudo apt-get install docker-ce

#install Nvidias’s docker runtime
#Uninstall old versions


$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 nvidia-docker 2.0 repository

$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 to check GPU

$docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi

Run a container for tensorflow-GPU with Docker

#run a containter

$docker run --runtime=nvidia -u $(id -u):$(id -g) -it -v /home/pan/docker_ws:/docker_ws -w /docker_ws tensorflow/tensorflow:latest-gpu-py3 bash

# Get container ID

$docker ps
CONTAINER ID ...
cfb9d7c95c9b ...

# Save the container

$docker commit -m “add tensorflow” -a “Roy” cfb9d7c95c9b tensorflow/tensorflow:v0001

#check whether new image is created

$docker images

# Stop the container

$docker stop cfb9d7c95c9b
or
type “exit” in docker command line

#restart the same container

$docker start cfb9d7c95c9b
$docker attach cfb9d7c95c9b

[Reference]

--

--