Docker Containers with GPU access in Fedora
This guide will let you use a NVIDIA GPU in a Docker container (e.g. for Tensorflow/Pytorch).
Prerequisites:
- Docker must be installed.
- You have a NVIDIA GPU.
1. Install NVIDIA drivers from the RPM Fusion repository1:
It is necessary to reboot the system, as this package includes a script which blacklists the default Nouveau drivers.
2. Add the NVIDIA Container Runtime Repository2:
curl -s -L https://nvidia.github.io/libnvidia-container/centos7/libnvidia-container.repo | sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
3. Install the NVIDIA Container Toolkit
This includes the NVIDIA Container Runtime, which adds a custom pre-start hook to all containers to allow GPU access):
4. Register the runtime with Docker:
sudo tee /etc/docker/daemon.json <<EOF
{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
Tell dockerd
to reload configuration:
(Optional) To make all containers use the NVIDIA runtime by default, add the following to /etc/docker/daemon.json
:
Alternatively, you can use nvidia-ctk
:
5. Finally, test GPU access in Docker with either:
or
And that's it!
Note on VSCode
If you are using VSCode Remote Containers, you will need to add
to your devcontainer.json
. For some reason, --runtime=nvidia
does not work.
-
While there is no official Fedora support for
nvidia-container-runtime
, the Centos 7 repository seems to work. ↩