As an expert full-stack developer and avid Linux user for over 10 years, I utilize Docker daily to streamline my workflow. With the rise of AI/ML, leveraging GPUs for performance is becoming increasingly common. However, properly configuring GPU access in Docker can be challenging.
In this comprehensive guide, I‘ll demonstrate how to passthrough your NVIDIA GPU in Docker containers on Ubuntu 22.04 LTS. You‘ll be able to run CUDA workloads on your GPU from Docker for AI/ML training or other GPU compute purposes.
Table of Contents
- Overview of NVIDIA GPUs and Docker
- Checking NVIDIA Drivers on Host Ubuntu 22.04 LTS
- Installing Docker CE on Ubuntu 22.04 LTS
- Docker Storage Drivers for GPU Support
- Installing NVIDIA Container Toolkit
- Installing nvidia-docker2 Driver
- Verifying GPU Access in Docker Container
- Conclusion and Key Takeways
Overview of NVIDIA GPUs and Docker
Docker containerization continues to grow dramatically, with an estimated 5 million Docker engines deployed. For AI and HPC workloads, leveraging the parallel processing power of GPUs is crucial for performance.
NVIDIA quotes their GPUs now power 236 of the world‘s TOP500 supercomputers. Furthermore, they claim:
NVIDIA GPU-powered servers are now able to replace 500 mainstream CPU-only servers
Integrating these high-powered GPUs into Docker deployments unlocks immense computing potential. However, it requires specific configuration on the host OS and Docker daemon to passthrough the GPU.
My Goal: Provide a step-by-step guide to properly configure Ubuntu 22.04 LTS and Docker for NVIDIA GPU passthrough. By following these instructions closely, you‘ll have a working development environment for GPU-leveraging containers.
Checking NVIDIA Drivers on Host Ubuntu 22.04 LTS
Before configuring Docker, first confirm your Ubuntu 22.04 LTS host has the appropriate NVIDIA GPU drivers installed.
Identify your GPU model with lspci
:
$ lspci | egrep -i ‘vga|3d|display‘
01:00.0 VGA compatible controller: NVIDIA Corporation GP107 [GeForce GTX 1050 Ti] (rev a1)
Verify official NVIDIA drivers are operational with lsmod
:
$ lsmod | grep nvidia
nvidia_drm 53248 0
nvidia_modeset 110592 1 nvidia_drm
nvidia 3187968 40 nvidia_modeset
drm_kms_helper 217088 1 nvidia_drm
drm 471040 5 drm_kms_helper,nvidia_drm
And inspect driver metadata/GPU utilization with nvidia-smi
:
$ nvidia-smi
Wed Feb 22 23:06:29 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.78.01 Driver Version: 525.78.01 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce GTX 105... Off | 00000000:01:00.0 Off | N/A |
| 20% 34C P8 9W / 75W | 8MiB / 4042MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
With the latest NVIDIA drivers installed and operational, Docker can be configured to access the GPU.
Installing Docker CE on Ubuntu 22.04 LTS
Alongside the NVIDIA packages, Docker needs installation too. Add the GPG key for the official Docker repository:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
And append its repository details at /etc/apt/sources.list.d/docker.list
:
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
With the GPG key and repository configured, install the latest Docker CE version:
$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
To control Docker as a non-root user, add your account to the docker
group:
$ sudo usermod -aG docker $(whoami)
Then reboot your system to apply the changes.
Validate Docker is operational by checking the version:
$ docker version
Client: Docker Engine - Community
Version: 20.10.23
API version: 1.41
Go version: go1.19.4
Built: Tue Feb 21 23:00:20 2023
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.23
API version: 1.41 (minimum version 1.12)
Go version: go1.19.4
Git commit: 9d1b6165b6
Built: Tue Feb 21 23:02:37 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.15
GitCommit: ff48f57fc83a8c8469cdc032d80a7987f93eb63e
runc:
Version: 1.1.4
GitCommit: v1.1.4-0-g5fd4c4d
docker-init:
Version: 0.19.0
GitCommit: de40ad0
With functional NVIDIA drivers and Docker CE in place, drivers specifically for GPU integration can now be installed.
Docker Storage Drivers for GPU Support
Docker relies on a storage driver to handle layering filesystem changes as images are built. By default, Ubuntu 22.04 LTS utilizes the overlay2
driver.
However, according to NVIDIA, the overlay2
driver does not currently support NVIDIA GPU integration.
Instead, the recommendation is to use nvidia-docker2
along with either:
overlay
storage driverdevicemapper
storage driver indirect-lvm
mode
To configure Docker to use one of these compatible storage drivers:
-
Update
/etc/docker/daemon.json
adding the desired storage driver – e.g.:{ "storage-driver": "overlay" }
-
Restart Docker CE service:
$ sudo systemctl restart docker
Now with a compatible storage driver configured, specialized NVIDIA integrations can be installed.
Installing NVIDIA Container Toolkit
NVIDIA provides a Container Toolkit tailored for GPU access in Docker.
Add the public key to APT:
$ curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | \
sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
And register the toolkit repository:
$ echo "deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://nvidia.github.io/libnvidia-container/stable/ubuntu22.04/$(ARCH) /" | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
Install the nvidia-container-toolkit
package:
$ sudo apt update
$ sudo apt install nvidia-container-toolkit
The toolkit includes a root daemon (nvidia-container-runtime
) needed for GPU sharing across containers. Validate it is operational:
$ sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.78.01 Driver Version: 525.78.01 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Type: GPU-Util Temp Fan Tach | Vol/Avg Power Disp.|
| Fan Speed Perf Pwr:En/Cap Mem Bus-Id | |
| | | TDP Peak|
|===============================+======================+======================|
| 0 NVIDIA GeForce ... On | 00000000:01:00.0 Off | N/A |
| N/A 25C P8 0W / 75W | 0MiB / 4042MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
The toolkit helps coordinate GPU sharing to containers. But direct access still requires the nvidia-docker2
driver.
Installing nvidia-docker2 Driver
nvidia-docker2
is tailored for simple NVIDIA GPU integration with Docker containers. Once installed, usage is seamless as --gpus
arguments transparently enable access.
Install the latest driver:
$ sudo apt update
$ sudo apt install nvidia-docker2
Restart the Docker daemon and nvidia-container-runtime to apply changes:
$ sudo systemctl restart docker
$ sudo systemctl restart nvidia-container-runtime
nvidia-docker2
wraps Docker commands, automatically configuring containers to leverage GPUs when the --gpus
argument is provided.
Verifying GPU Access in Docker Container
Putting everything together, quick test confirming GPU accessibility from a Docker container:
$ docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.78.01 Driver Version: 525.78.01 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Type: GPU-Util Temp Fan Tach | Vol/Avg Power Disp.|
| Fan Speed Perf Pwr:En/Cap Mem Bus-Id | |
| | | TDP Peak|
|===============================+======================+======================|
| 0 NVIDIA GeForce ... On | 00000000:01:00.0 Off | N/A |
| N/A 26C P8 2W / 75W | 0MiB / 4042MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
The CUDA container successfully outputs GPU status, confirming access is properly configured.
With these steps completed, you can now leverage your GPU for AI/ML inferencing, Bitcoin mining, analytics, simulation workloads, etc from Docker containers, maximizing hardware utilization.
Conclusion and Key Takeaways
Walking through these steps to install Docker CE, NVIDIA drivers, the NVIDIA container toolkit, and nvidia-docker2 driver may seem arduous. However, properly configuring GPU passthrough enables immense computing potential from containers.
Some closing takeaways:
- Docker containerization continues explosive growth
- NVIDIA GPUs unlock order-of-magnitude performance gains
- But coordinating Docker deployments with NVIDIA demands precision configuration
- Ubuntu host must have official NVIDIA drivers operational
- Docker daemon requires compatible storage driver like
overlay
- NVIDIA container toolkit manages coordinated GPU sharing
- nvidia-docker2 driver streamlines GPU-enabled containers
With these pieces in place, developers can tap abundant GPU horsepower, accelerating AI workloads and more within portable Docker containers. The configuration process may seem complex, but the performance pay-off can‘t be understated – with reports of 50-100x speed-ups.
I hope this guide served as comprehensive reference for getting your Docker environment GPU-ready. Please reach out with any questions!