Open Nav

How to setup Docker and Nvidia-Docker 2.0 on Ubuntu 18.04

This tutorial will help you set up Docker and Nvidia-Docker 2 on Ubuntu 18.04. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker was popularly adopted by data scientists and machine learning developers since its inception in 2013. It enables data scientists to build environments once – and ship their training/deployment quickly and easily.

NVIDIA-Docker is a tool created by Nvidia to enable support for GPU devices in the containers. If you’re working on Deep Learning applications or on any computation that can benefit from GPUs – you’ll probably need this tool.


A computer/server with GPU and Ubuntu 16.04 installed

Step 1 – Install Docker

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. What exactly is a container? Containers allow data scientists and developers to wrap up an environment with all of the parts it needs – such as libraries and other dependencies – and ship it all out in one package.

Docker is an important component when building machine learning models. First, it allows you to track your environment and your model dependencies. Additionally, it allows you to scale to the cloud and other servers without rebuilding your environment from scratch every time. This component is crucial for rapid experimentation.

Begin by updating the apt index and lists. This won’t install anything, but simply download the package lists with their latest versions.

$ sudo apt-get update 

Next, use “apt-get upgrade” to fetch new versions of packages existing on the machine

$ sudo apt-get upgrade 

Next, you’ll need to install dependencies to support the addition of a new package repo (docker) that’s using HTTPS connectivity:

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

Add Docker’s official GPG key:

$ curl -fsSL | sudo apt-key add -

Add Docker’s official apt repo

$ sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"

You can then update the apt lists with the new docker repo, and install Docker CE (Community Edition)

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

Now, docker should be installed. You can verify the installation by running a few commands.

$ docker -v
>> Docker version 18.09.2, build 6247962 # as of writing of this tutorial

Finally, to remove the requirement of “sudo” when running docker commands, add your user to the docker group. To do so, simply type the following:

$ sudo usermod -aG docker Username

Your username is now part of the docker group. To apply changes, either logout and login or type:

$ su - Username

Step 2 – Install Nvidia-Docker

NVIDIA designed NVIDIA-Docker in 2016 to enable portability in Docker images that leverage NVIDIA GPUs. It wrapped CUDA drivers for ease of use for Docker with a GPU. Its main function is to mount the user mode components of the driver, and the GPU device files into the container at launch.

Similarly to Docker installation, start by setting the GPG and remote repo for the package

$ curl -s -L | \
 sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L$distribution/nvidia-docker.list | \
 sudo tee /etc/apt/sources.list.d/nvidia-docker.list

Then update the apt lists

$ sudo apt-get update

Now you install nvidia-docker (2) and reload the Docker daemon configurations

$ sudo apt-get install -y nvidia-docker2
$ sudo pkill -SIGHUP dockerd

Nvidia GPUs first require drivers to be installed. Here is how you make sure they are installed

$ sudo apt-get remove nvidia -384 ; sudo apt-get install nvidia-384

Now, the only thing left to do is test your environment and to make sure everything is installed correctly. Just simply launch the nvidia-smi (system management interface) application.

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

The output should look something like this:

| NVIDIA-SMI 390.77                 Driver Version: 390.77      |
| GPU  Name     Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|   0 Tesla K80           Off | 00000000:00:1E.0 Off |                    0 |
| N/A   39C P0    83W / 149W |     0MiB / 11441MiB |    98% Default |
| Processes:                                                       GPU Memory |
|  GPU       PID Type   Process name                            Usage |
|  No running processes found                                                 |

Congrats! You have successfully installed Docker and Nvidia-Docker in your environment.

Top MLOps guides and news in your inbox every month