Use Nvidia-Docker to never worry about installing a specific version of a framework again

Rémi Connesson
4 min readMar 4, 2019

Using nvidia-docker has several advantages, the biggest are:

  • you don’t need to install CUDA manually.
  • you can easily install and get out-of-the-box GPU installation on pretty much any libraries and versions you want.
  • it’s super easy to install and work with.

You can have access to the latest or oldest versions of:
- MXNet
- Torch
- Caffe
- Caffe2 (Version 0.8.2 and below)
- Theano
- CNTK (Versio 2.5 an below)
- Tensorflow
- PyTorch with Caffe2 (Version 0.8.2 and above)

For this guide, copy and paste the command lines one by one.

Install GPU Driver

(if you come from this article: “Get a GPU Machine Working on GCP” go straight to the next part: “Install Nvidia-Docker”)

If and only if you already have a driver installed, and are not sure what its version is, just perform these extra two steps before proceeding.

sudo apt-get purge nvidia*
sudo reboot

Follow these steps to install the nvidia GPU driver on a clean install.

sudo apt-get update -y
sudo apt-get upgrade -yl
spci | grep -i nvidia
uname -m && cat /etc/*release
sudo apt-get install linux-headers-$(uname -r) -y
sudo apt autoremove -y
sudo apt-get install build-essential -y
sudo apt-get install cmake git unzip zip -y
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:graphics-drivers/ppa -y
sudo apt install nvidia-driver-415 -y
sudo reboot

Check that everything works so far

Enter the command line below.

nvidia-smi

And this screen should be displayed.

Install Nvidia-Docker2

Once drivers are correctly installed, you can continue with the next steps.

sudo apt updatesudo apt install apt-transport-https ca-certificates curl software-properties-common -ycurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"sudo apt updateapt-cache policy docker-cesudo apt install docker-ce -ycurl -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 updatesudo apt-get install nvidia-docker2 -ysudo pkill -SIGHUP dockerdsudo docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
If everything is working fine so far, you should see something like this.

Choose An Image To Work With

Just read the Release Notes to know which image you want to pull.

- MXNet
- Torch
- Caffe
- Caffe2 (Version 0.8.2 and below)
- Theano
- CNTK (Versio 2.5 an below)
- Tensorflow
- PyTorch with Caffe2 (Version 0.8.2 and above)

Example: How To Use

Then create an account on https://ngc.nvidia.com/signup and sign in.

Now you can generate your API key and login exactly as shown here:

sudo docker login nvcr.io

We choose to test how it works with Pytorch but you can use any libraries supported by Nvidia.

sudo docker pull nvcr.io/nvidia/pytorch:19.02-py3

Once the download is complete, we need to find the ID of the docker image.

sudo docker images
the result of “sudo docker images”

Then to run from the image and be able to access it using jupyter , use the command lines below.

Nota Bene: If you are on a cloud machine, you will need to have the port 6969 opened on your cloud server. If you are on Google Cloud Platform you can learn how to this easily here.

sudo nvidia-docker run -v ~/work:/workspace/work -it -p 6969:6969 --shm-size=
1g --ulimit memlock=-1 --ulimit stack=67108864 --rm e53aa3cb6f1c

Then once you are in, launch the Jupyter notebook server.

jupyter notebook --no-browser --port=6969 --ip='0.0.0.0' --allow-root

Then you can access the Jupyter notebook at http://yourserverIP:6969/ and work as you are used to with Jupyter.

WARNING: Every time you stop your docker everything will be erased, except the content of the work/ directory. So make sure to actually do all the meaningful work inside this work/ directory.

Inside of the Docker !

Congratulations, you’re in!

Happy Coding ;)

--

--

Rémi Connesson

I use Roam + Notion along my way of building expertise in marketing and ML ops. You can tag along the journey here https://tinyletter.com/remiconnesson :)