Nvidia Docker on WSL2

Frank Chung
DeepQ Research Engineering Blog
4 min readSep 13, 2022
Linux + Windows + Nvidia + Docker

In 2022 if we intend to build containers with GPU supports on Windows, the only solutions is WSL2 (Windows Subsystem). This tutorial guides step-by-step to install from scratch.

Hardware Environment

CPU: Intel(R) Xeon(R) W-2223 CPU @ 3.60GHz

RAM: 8.00 GB

GPU: NVIDIA Quadro P1000

Software Environment

Edition: Windows 11 Pro for Workstations

OS Build: 22000.795

BIOS: HP Sure Starter

Step by Step Instruction

1. Install Nvidia driver for specific GPU on Windows from Official Drivers | NVIDIA, and make sure your nvidia-smi works normally.

Download and install the Nvidia driver for Windows
Run nvidia-smi in Windows command shell to test the installation

2. Install Docker Desktop for Windows from Docker Desktop WSL 2 backend, and enable WSL2 based engine in the Docker setting.

Enable WSL2 backend

3. Enable Virtualization Technology on your BIOS. For example: HP PCs — Enable Virtualization Technology in the BIOS | HP® Customer Support

Enable Virtualization Technology in your BIOS

4. Install WSL and Linux distribution (here we use Ubuntu 20.04) in the Microsoft Store step by step: Install Ubuntu on WSL2 on Windows 11 with GUI support

Enable Virtual Machine Platform in Windows Feature
Download WSL on Microsoft Store
Download Ubuntu on Microsoft Store
Boot the Ubuntu on WSL and create a linux user

5. Enable the Docker integration with WSL for the Ubuntu distribution in Docker setting.

After installation of Ubuntu, enable the WSL integration for Docker

6. Install CUDA on WSL2, Run the following commands by CUDA on WSL User Guide

# set default WSL engine to WSL2
C:\> wsl.exe --set-default-version 2
# show all linux distribution
C:\> wsl.exe --install
# start the Ubuntu system by WSL2
C:\> wsl.exe -d Ubuntu-20.04

In the Ubuntu shell:

# First, remove the old GPG key:
$ sudo apt-key del 7fa2af80
# Install CUDA
$ wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
$ sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
$ sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get -y install cuda

7. Install Nvidia Docker on Ubuntu by INSTALLING DOCKER

# Add the package repositories:
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
# Download information from all configured sources
$ sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
# Restart the Docker service
$ sudo systemctl restart docker

8. We are done here, let just check the GPU capability in the Ubuntu by WSL2.

Run nvidia-smi in the Ubuntu shell
# Run docker container to test GPU capability
$ sudo docker run --gpus all nvcr.io/nvidia/k8s/cuda-sample:nbody nbody -gpu -benchmark
Test GPU with benchmark in docker container

References

--

--