Setting up (nvidia-)docker for GPU support on Ubuntu

In this post I would like to show how to set up your Computer for the use of nvidia-docker with GPU support.

I will set up the GPU support for nvidia-docker version 1 and 2 and show you how to implement that as easy as possible with the help of docker and docker-compose.

I will be using:

Docker version 17.06.2-ce, build cec0b72

docker-compose version 1.13.0, build 1719ceb

on Ubuntu 16.04 LTS

I would also like to provide some basic code snippets as docker-compose.yml starting points to help you get everything set up.

The .yml snippets shown here at can also be found at our github repo.

There are some prerequesits to this set up.

You need to have installed docker (asuming ubuntu as operating system) –> see here

You will also need to install your nvidia graphics card drivers. Here is a great tutorial on how to do that –> see here

There is no need for any Cuda Toolkit or anything else but the driver itself is sufficient!

And last you will need to install nvidia-docker version 1 or version 2. Tutorials can be found here: –> version 1 –> version 2

For version 1 you will also have to install nvidia-modprobe –> see here

Either version will work and I will show how to use both but you cannot use them side by side!

Ok let’s start with nvidia-docker version 1

After installing all of the things mentioned above start nvidia-docker plugin:

sudo nvidia-docker-plugin

Now check if nvidia-docker-plugin works:

service nvidia-docker status

Ok we are done at this place if you want to take it with just docker you could ‘simply’ type the following at the  command line:

docker run \
--rm \
--device /dev/nvidia0:/dev/nvidia0 \
--device /dev/nvidiactl:/dev/nvidiactl \
--device /dev/nvidia-uvm:/dev/nvidia-uvm \
-p 8888:8888 \
-v `pwd`:/home/user \

This is some big command (it uses a tensorflow-gpu image)!

That’s why we decided to use docker-compose so we will end up with something like this:

docker-compose up

Much nicer isn’t it.

Ok to make the gpu(s) of your host be used by docker-compose to be mapped into the container we need to configure some additional things in our docker-compose.yml file.

But first we need to create a docker-volume for the graphics card driver to map it into the container later.

Let’s go:

Figure out your nvidia-grapics card driver version:


Something like this will show up:

| NVIDIA-SMI 387.34                 Driver Version: 387.34                    |
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|   0  GeForce GTX 106...  Off  | 00000000:01:00.0  On |                  N/A |
| 29%   41C    P8     9W / 120W |   6009MiB /  6069MiB |     15%      Default |

Now create a docker- volume with the exact driver specifications listed by the output above… so in my case it would be:

docker volume create --name=nvidia_driver_387.34 -d nvidia-docker

Let’s now see what things need to be considered in the docker-compose.yml:

version:                '3'



      devices:          #this is required
        - /dev/nvidiactl
        - /dev/nvidia-uvm
        - /dev/nvidia0 #in general: /dev/nvidia# where # depends on which gpu card is wanted to be used

        - network

        - "nvidia_driver_384.90:/usr/local/nvidia:ro"

## Driver Volume for CUDA Version
    external:           true

    driver:             bridge

Actually the file version does not matter much. But notice the devices section and the external volume which gets called and mapped as a read only volume into the container.

That’s it for version 1… you can now run any container with GPU support via:

docker-compose up

However I kind of found it tyring to set all this up for version 1 that’s why I want to show you how you could set all this up with nvidia-docker version 2…. it is much shorter!

Here we go:

first follow the installation instructions mentioned above for version 2.

Now edit the daemon.json file at /etc/docker/ like so:

  "default-runtime": "nvidia",
  "runtimes": {
    "nvidia": {
      "path": "/usr/bin/nvidia-container-runtime",
      "runtimeArgs": []

Notice the new line–> default-runtime at the top.

This tells docker-compose to use nvidia-docker as the default runtime and takes care of all the GPU handling.

Now you can adjust your docker-compose.yml like so:

version:                            '3'



        NVIDIA_DRIVER_CAPABILITIES: compute,utility

        - network

    driver:                         bridge

Notice now we do not need any external volume drivers and device mappings… we just set up two additional lines of enviromental settings and we are done.

Now run:

docker-compose up

and everything should be working as you whish.

As mentioned above the .yml file snippets can also be found here.

If you would like to see how to use these settings with a Docker Image defining a Jupyter Notebook container with tensorflow, keras and pytorch support (and many other packages more)

Look at this blog post


Addition informations