HomeLinuxUse the NVIDIA GPU in Docker Containers on Ubuntu 22.04 LTS

Use the NVIDIA GPU in Docker Containers on Ubuntu 22.04 LTS


You may passthrough your NVIDIA GPU in Docker containers and run CUDA applications in your NVIDIA GPU from these Docker containers. It is a very helpful characteristic for studying AI (Synthetic Intelligence). Having the ability to run AI codes (i.e. Tensorflow) on Docker containers will prevent numerous time. You too can check out your AI codes on completely different CUDA variations without having to alter the CUDA model in your pc.

Twiddling with NVIDIA drivers and CUDA variations in your pc could trigger issues just like the NVIDIA drivers not working or leaving you with a black/blue display screen of loss of life. So, it’s all the time a good suggestion to depart your system as it’s and make modifications in an remoted atmosphere like Docker containers. This retains your principal pc clear (of pointless improvement instruments).

On this article, I’m going to indicate you find out how to setup Docker CE and NVIDIA docker on Ubuntu 22.04 LTS to be able to entry the NVIDIA GPU of your pc from Docker containers and run CUDA applications in your NVIDIA GPU.

Desk of Contents:

  1. Checking if the Official NVIDIA GPU Drivers are Put in on Ubuntu 22.04 LTS
  2. Putting in Docker CE Dependencies on Ubuntu 22.04 LTS
  3. Putting in Docker CE GPG Keys on Ubuntu 22.04 LTS
  4. Putting in Docker CE Repository on Ubuntu 22.04 LTS
  5. Putting in Docker CE on Ubuntu 22.04 LTS
  6. Including the Ubuntu 22.04 LTS Login Consumer to the docker Group
  7. Checking if Docker CE was Put in Appropriately on Ubuntu 22.04 LTS
  8. Putting in NVIDIA Container Toolkit GPG Keys on Ubuntu 22.04 LTS
  9. Putting in the NVIDIA Container Toolkit Repository on Ubuntu 22.04 LTS
  10. Putting in nvidia-docker Drivers on Ubuntu 22.04 LTS
  11. Checking if NVIDIA GPU is Accessible from Docker Containers in Ubuntu 22.04 LTS
  12. Conclusion
  13. References

Checking if the Official NVIDIA GPU Drivers are Put in on Ubuntu 22.04 LTS:

Earlier than you get began, run the next command to confirm that you’ve got an NVIDIA GPU put in in your pc.

$ lspci | egrep -i ‘vga|3d|show’

 

On this case, I’ve NVIDIA GTX 1050 Ti GPU put in on my pc. You’ll probably have a special NVIDIA GPU put in in your pc.


Additionally, just remember to have the official NVIDIA drivers put in in your Ubuntu 22.04 working system with the next command:

 

If the official NVIDIA drivers are put in in your Ubuntu 22.04 working system, you will notice the next outputs.


Additionally, test if the official NVIDIA GPU drivers are working with the next command:

 

If the official NVIDIA GPU drivers are working, you will notice the next outputs. As you possibly can see, I’ve the official NVIDIA GPU Driver model 525.78.01 put in on my Ubuntu 22.04 machine.


Should you don’t have the official NVIDIA GPU drivers put in in your Ubuntu 22.04 machine and also you want any help with that, please test the article
Set up NVIDIA Drivers on Ubuntu 22.04 LTS.

Putting in Docker CE Dependencies on Ubuntu 22.04 LTS:

Earlier than you possibly can set up Docker CE on Ubuntu 22.04, you’ll need to put in the required Docker CE dependency packages on Ubuntu 22.04.

First, replace the APT package deal repository cache with the next command:

 

The APT package deal repository cache ought to be up to date.


To put in the required dependency packages of Docker CE, run the next command:

$ sudo apt-get set up ca-certificates curl gnupg lsb-release

 

To verify the set up, press Y after which press <Enter>.


The required Docker CE dependency packages ought to be put in.

Putting in Docker CE GPG Keys on Ubuntu 22.04 LTS:

On this part, I’m going to indicate you find out how to set up the GPG key of the Docker CE package deal repository on Ubuntu 22.04.

First, create a brand new folder /and so forth/apt/keyrings with the next command:

$ sudo mkdir -p /and so forth/apt/keyrings

 

To put in the GPG key of the Docker CE package deal repository on Ubuntu 22.04, run the next command:

$ curl -fsSL https://obtain.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o /and so forth/apt/keyrings/docker.gpg

 

Putting in Docker CE Repository on Ubuntu 22.04 LTS:

To put in the Docker CE package deal repository on Ubuntu 22.04, run the next command:

$ echo “deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://obtain.docker.com/linux/ubuntu $(lsb_release -cs) steady”  | sudo tee /and so forth/apt/sources.listing.d/docker.listing > /dev/null

 

Replace the APT package deal repository cache for the modifications to take impact.

 

Putting in Docker CE on Ubuntu 22.04 LTS:

To put in the most recent model of Docker CE on Ubuntu 22.04, run the next command:

$ sudo apt-get set up docker-ce docker-ce-cli containerd.io docker-compose-plugin

 

To verify the set up, press Y after which press <Enter>.


Docker CE and the required dependency packages are being downloaded. It is going to take some time to finish.


Docker CE and the required dependency packages are being put in. It is going to take some time to finish.


Docker CE and the required dependency packages ought to be put in at this level.

Including the Ubuntu 22.04 LTS Login Consumer to the docker Group:

To create Docker containers and handle them with out utilizing sudo or logged in as root consumer, you’ll have to add your login consumer to the docker group.

So as to add the login consumer of your Ubuntu 22.04 to the docker group, run the next command:

$ sudo usermod -aG docker $(whoami)

 

For the modifications to take impact, restart your pc with the next command:

 

Checking if Docker CE was Put in Appropriately on Ubuntu 22.04 LTS:

As soon as your pc begins, run the next command to confirm you could entry Docker with out superuser privileges.

 

If every little thing works, it is best to see the next outputs.

As you possibly can see, I’m operating Docker model 20.10.23 – the most recent model of Docker CE on the time of this writing.

Putting in NVIDIA Container Toolkit GPG Keys on Ubuntu 22.04:

On this part, I’m going to indicate you find out how to set up the GPG key of the NVIDIA Container Toolkit package deal repository on Ubuntu 22.04.

To put in the GPG key of the NVIDIA Container Toolkit package deal repository on Ubuntu 22.04, run the next command:

$ curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg –dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg

 

Putting in the NVIDIA Container Toolkit Repository on Ubuntu 22.04 LTS:

On this part, I’m going to indicate you find out how to set up the NVIDIA Container Toolkit package deal repository on Ubuntu 22.04.

First, create a brand new APT supply file nvidia-container-toolkit.listing within the /and so forth/apt/sources.listing.d/ listing as follows:

$ sudo nano /and so forth/apt/sources.listing.d/nvidia-container-toolkit.listing

 

Within the nvidia-container-toolkit.listing file, add the next line and press <Ctrl> + X adopted by Y and <Enter> to save lots of the file.

deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/steady/ubuntu22.04/$(ARCH) /

 

NOTE: Should you’re utilizing Ubuntu 20.04 LTS, then all you should do is add the next line within the nvidia-container-toolkit.listing file as an alternative and every little thing else ought to work unchanged.

deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/steady/ubuntu20.04/$(ARCH) /

 
NOTE: Should you’re utilizing Ubuntu 18.04 LTS, then all you should do is add the next line within the nvidia-container-toolkit.listing file as an alternative and every little thing else ought to work unchanged.

deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/steady/ubuntu18.04/$(ARCH) /

 
For the modifications to take impact, replace the APT package deal repository cache with the next command:

 

Putting in nvidia-docker Drivers on Ubuntu 22.04 LTS:

To put in the NVIDIA Docker drivers on Ubuntu 22.04, run the next command:

$ sudo apt set up nvidia-docker2

 

To verify the set up, press Y after which press <Enter>.


The NVIDIA docker drivers ought to be put in.


For the modifications to take impact, restart your pc with the next command:

 

Checking if NVIDIA GPU is Accessible from Docker Containers in Ubuntu 22.04 LTS:

On this part, I’m going to indicate you find out how to create an NVIDIA CUDA Docker container and confirm that the container can entry the NVIDIA GPU out of your pc.

To create an NVIDIA CUDA 12 Docker container based mostly on Ubuntu 20.04 LTS and run the nvidia-smi command in it as soon as it’s created to confirm whether or not it will probably entry the NVIDIA GPU out of your pc, run the next command:

$ docker run –rm –gpus all nvidia/cuda:12.0.0-base-ubuntu20.04 nvidia-smi

 

Docker is pulling the nvidia/cuda:12.0.0-base-ubuntu20.04 picture from Docker Hub. It is going to take some time to finish.


As soon as the NVIDIA CUDA docker picture is pulled and a container is created, the nvidia-smi command will run on it and print the output on the console as you possibly can see within the screenshot beneath.

The Docker container is utilizing the NVIDIA GPU driver 525.78.01[1] and the CUDA model 12.0[2]. Should you see related outputs, then the Docker container can entry the NVIDIA GPU of your pc.


If you should use older variations of CUDA, test nvidia/cuda picture at Docker Hub.

Conclusion:

On this article, I’ve proven you find out how to setup the Docker CE package deal repository on Ubuntu 22.04. I’ve additionally proven you find out how to set up the most recent model of Docker CE on Ubuntu 22.04. I’ve proven you find out how to set up the NVIDIA Container Toolkit package deal repository on Ubuntu 22.04 in addition to find out how to set up the NVIDIA Docker driver on Ubuntu 22.04. Lastly, I’ve proven you find out how to entry the NVIDIA GPU of your pc from a Docker container.

References:

  1. https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html
  2. https://hub.docker.com/r/nvidia/cuda/tags
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments