Using GPU in Docker container

I need to access GPU within my docker container, so I followed the Docker documentation, added the following lines to the docker-compose.yml file:

deploy:
  resources:
    reservations:
      devices:
        - driver: nvidia
          count: 1
          capabilities: [gpu]

After running docker-compose up command, I got the following error:

Error response from daemon: could not select device driver "nvidia" with capabilities: [[gpu]]

Although I have already installed NVIDIA drivers on my host, it looks like the GPU is not recognized in Docker container.

After reading this, I realized that I also need to install the NVIDIA Container Toolkit.

  • Install the nvidia-docker2 package:
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
   && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.repo | sudo tee /etc/yum.repos.d/nvidia-container-toolkit.reponvidia-container-toolkit.repo
$ sudo yum clean expire-cache
$ sudo yum install -y nvidia-docker2
  • Then restart the Docker daemon:
$ sudo systemctl restart docker

Tags:

Updated: