You can passthrough your NVIDIA GPU in Docker containers and run CUDA programs on your NVIDIA GPU from these Docker containers. This is a very useful feature for learning AI (Artificial Intelligence). Being able to run AI codes (i.e. Tensorflow) on Docker containers will save you a lot of time. You can also try out your AI codes on different CUDA versions without needing to change the CUDA version on your computer.
Fiddling with NVIDIA drivers and CUDA versions on your computer may cause problems like the NVIDIA drivers not working or leaving you with a black/blue screen of death. So, it’s always a good idea to leave your system as it is and make changes in an isolated environment like Docker containers. This keeps your main computer clean (of unnecessary development tools).
In this article, I am going to show you how to setup Docker CE and NVIDIA docker on Ubuntu 22.04 LTS so that you can access the NVIDIA GPU of your computer from Docker containers and run CUDA programs on your NVIDIA GPU.
Table of Contents:
- Checking if the Official NVIDIA GPU Drivers are Installed on Ubuntu 22.04 LTS
- Installing Docker CE Dependencies on Ubuntu 22.04 LTS
- Installing Docker CE GPG Keys on Ubuntu 22.04 LTS
- Installing Docker CE Repository on Ubuntu 22.04 LTS
- Installing Docker CE on Ubuntu 22.04 LTS
- Adding the Ubuntu 22.04 LTS Login User to the docker Group
- Checking if Docker CE was Installed Correctly on Ubuntu 22.04 LTS
- Installing NVIDIA Container Toolkit GPG Keys on Ubuntu 22.04 LTS
- Installing the NVIDIA Container Toolkit Repository on Ubuntu 22.04 LTS
- Installing nvidia-docker Drivers on Ubuntu 22.04 LTS
- Checking if NVIDIA GPU is Accessible from Docker Containers in Ubuntu 22.04 LTS
- Conclusion
- References
Checking if the Official NVIDIA GPU Drivers are Installed on Ubuntu 22.04 LTS:
Before you get started, run the following command to verify that you have an NVIDIA GPU installed on your computer.
In this case, I have NVIDIA GTX 1050 Ti GPU installed on my computer. You will most likely have a different NVIDIA GPU installed on your computer.
Also, make sure that you have the official NVIDIA drivers installed on your Ubuntu 22.04 operating system with the following command:
If the official NVIDIA drivers are installed on your Ubuntu 22.04 operating system, you will see the following outputs.
Also, check if the official NVIDIA GPU drivers are working with the following command:
If the official NVIDIA GPU drivers are working, you will see the following outputs. As you can see, I have the official NVIDIA GPU Driver version 525.78.01 installed on my Ubuntu 22.04 machine.
If you don’t have the official NVIDIA GPU drivers installed on your Ubuntu 22.04 machine and you need any assistance with that, please check the article How to Install NVIDIA Drivers on Ubuntu 22.04 LTS.
Installing Docker CE Dependencies on Ubuntu 22.04 LTS:
Before you can install Docker CE on Ubuntu 22.04, you will need to install the required Docker CE dependency packages on Ubuntu 22.04.
First, update the APT package repository cache with the following command:
The APT package repository cache should be updated.
To install the required dependency packages of Docker CE, run the following command:
To confirm the installation, press Y and then press <Enter>.
The required Docker CE dependency packages should be installed.
Installing Docker CE GPG Keys on Ubuntu 22.04 LTS:
In this section, I am going to show you how to install the GPG key of the Docker CE package repository on Ubuntu 22.04.
First, create a new folder /etc/apt/keyrings with the following command:
To install the GPG key of the Docker CE package repository on Ubuntu 22.04, run the following command:
Installing Docker CE Repository on Ubuntu 22.04 LTS:
To install the Docker CE package repository on Ubuntu 22.04, run the following command:
Update the APT package repository cache for the changes to take effect.
Installing Docker CE on Ubuntu 22.04 LTS:
To install the latest version of Docker CE on Ubuntu 22.04, run the following command:
To confirm the installation, press Y and then press <Enter>.
Docker CE and the required dependency packages are being downloaded. It will take a while to complete.
Docker CE and the required dependency packages are being installed. It will take a while to complete.
Docker CE and the required dependency packages should be installed at this point.
Adding the Ubuntu 22.04 LTS Login User to the docker Group:
To create Docker containers and manage them without using sudo or logged in as root user, you will have to add your login user to the docker group.
To add the login user of your Ubuntu 22.04 to the docker group, run the following command:
For the changes to take effect, restart your computer with the following command:
Checking if Docker CE was Installed Correctly on Ubuntu 22.04 LTS:
Once your computer starts, run the following command to verify that you can access Docker without superuser privileges.
If everything works, you should see the following outputs.
As you can see, I am running Docker version 20.10.23 – the latest version of Docker CE at the time of this writing.
Installing NVIDIA Container Toolkit GPG Keys on Ubuntu 22.04:
In this section, I am going to show you how to install the GPG key of the NVIDIA Container Toolkit package repository on Ubuntu 22.04.
To install the GPG key of the NVIDIA Container Toolkit package repository on Ubuntu 22.04, run the following command:
Installing the NVIDIA Container Toolkit Repository on Ubuntu 22.04 LTS:
In this section, I am going to show you how to install the NVIDIA Container Toolkit package repository on Ubuntu 22.04.
First, create a new APT source file nvidia-container-toolkit.list in the /etc/apt/sources.list.d/ directory as follows:
In the nvidia-container-toolkit.list file, add the following line and press <Ctrl> + X followed by Y and <Enter> to save the file.
NOTE: If you’re using Ubuntu 20.04 LTS, then all you need to do is add the following line in the nvidia-container-toolkit.list file instead and everything else should work unchanged.
NOTE: If you’re using Ubuntu 18.04 LTS, then all you need to do is add the following line in the nvidia-container-toolkit.list file instead and everything else should work unchanged.
For the changes to take effect, update the APT package repository cache with the following command:
Installing nvidia-docker Drivers on Ubuntu 22.04 LTS:
To install the NVIDIA Docker drivers on Ubuntu 22.04, run the following command:
To confirm the installation, press Y and then press <Enter>.
The NVIDIA docker drivers should be installed.
For the changes to take effect, restart your computer with the following command:
Checking if NVIDIA GPU is Accessible from Docker Containers in Ubuntu 22.04 LTS:
In this section, I am going to show you how to create an NVIDIA CUDA Docker container and verify that the container can access the NVIDIA GPU from your computer.
To create an NVIDIA CUDA 12 Docker container based on Ubuntu 20.04 LTS and run the nvidia-smi command in it once it’s created to verify whether it can access the NVIDIA GPU from your computer, run the following command:
Docker is pulling the nvidia/cuda:12.0.0-base-ubuntu20.04 image from Docker Hub. It will take a while to complete.
Once the NVIDIA CUDA docker image is pulled and a container is created, the nvidia-smi command will run on it and print the output on the console as you can see in the screenshot below.
The Docker container is using the NVIDIA GPU driver 525.78.01[1] and the CUDA version 12.0[2]. If you see similar outputs, then the Docker container can access the NVIDIA GPU of your computer.
If you need to use older versions of CUDA, check nvidia/cuda image at Docker Hub.
Conclusion:
In this article, I have shown you how to setup the Docker CE package repository on Ubuntu 22.04. I have also shown you how to install the latest version of Docker CE on Ubuntu 22.04. I have shown you how to install the NVIDIA Container Toolkit package repository on Ubuntu 22.04 as well as how to install the NVIDIA Docker driver on Ubuntu 22.04. Finally, I have shown you how to access the NVIDIA GPU of your computer from a Docker container.