avatarMB20261

Summary

This web content provides a comprehensive guide for setting up GPU support in the Docker environment on Windows WSL2 or Linux systems such as Ubuntu 22.04LTS, detailing the installation and configuration of Docker Engine, NVIDIA Container Toolkit, and testing procedures.

Abstract

The article offers a step-by-step tutorial to enable GPU acceleration within Docker on WSL2 or Ubuntu 22.04LTS platforms. It begins with the necessary steps to upgrade the system, install prerequisites, and add Docker's repository to the system's package manager. The installation of Docker Engine, including relevant packages and setting up user permissions, is thoroughly explained. The guide then moves on to the installation of the NVIDIA Container Toolkit, which is essential for GPU support in Docker containers, and configures the Docker runtime to use NVIDIA's runtime. Finally, the article demonstrates how to test the GPU support in a Docker container using the nvidia-smi command and provides further readings for advanced configurations such as running Docker in rootless mode.

Opinions

  • The author recommends hardening configurations for production use, implying that the outlined steps are primarily for testing or development environments.
  • It is suggested that running GPU-enabled Docker containers directly in WSL2 is not optimal, indicating a preference for native Linux environments for such tasks.
  • The article encourages users to explore rootless Docker configurations for enhanced security, which reflects a best practice mindset in managing Docker environments.
  • The inclusion of direct commands and links to official documentation signifies a trust in the reliability and authority of these sources for further information and support.

Enable GPU support in WSL2 to test or build docker images

In this article, we will go through step by step to setup GPU support in local docker engine or runtime for Windows WSL2 or Linux like Ubuntu 22.04LTS. It is recommended to harden below configurations if you plan for production purpose.

Install Docker Engine

Install prerequisites:

sudo apt update && sudo apt upgrade -y

sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg \
        lsb-release -y

Register docker repository to apt source list:

sudo install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install the Docker Engine:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Setup permissions:

sudo usermod -aG docker $USER

Start docker service:

sudo service docker start

Test Docker installation:

sudo docker run hello-world

Install NVIDIA Container Toolkit

Register toolkit repository to apt source list:

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

sudo apt-get update

Install NVIDIA Container Toolkit packages:

sudo apt-get install -y nvidia-container-toolkit

Configure Docker runtime:

sudo nvidia-ctk runtime configure --runtime=docker

sudo service docker restart

Test

sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

Further Readings

It is not recommended to run GPU docker inside WSL2. If you want to run GPU docker environment in a Linux system with GPU supports, you might want to configure rootless. To do so, you could find more details from below links:

Enjoy!

Docker
Wsl 2
Nvidia Container Toolkit
Containerization
Gpu
Recommended from ReadMedium