LoginSignup
1
2

はじめに

この記事では、Windows Subsystem for Linux (WSL)上のUbuntu-22.04にDocker EngineとNVIDIA Container Toolkitをインストールし、Visual Studio Code(VSCode)の拡張機能Dev Containersを使用してGPUを利用可能なDocker開発環境を構築する方法を説明します。

Windows Subsystem for Linux (WSL) のインストール

まず、WSLをインストールします。
詳細な手順は以下のリンクを参照してください。

インストールができているかの確認

PowerShellで以下のコマンドを実行し、WSLのインストールを確認します。

wsl -l -v
インストール成功例 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3665667/420acd2e-b428-6508-50f8-fb648c12cefe.png)

WSL上のUbuntu-22.04にDocker Engineをインストール

次に、WSL上のUbuntu-22.04にDocker Engineをインストールします。
詳細な手順は以下のリンクを参照してください。

インストールができているかの確認

以下のコマンドを実行し、Docker Engineのインストールを確認します。

sudo docker run hello-world
インストール成功例 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3665667/2178e6bd-8ec8-97a6-20f8-5cdf7424636e.png)

WSL上のUbuntu-22.04にNVIDIA Container Toolkitをインストール

WSL上のUbuntu-22.04にNVIDIA Container Toolkitをインストールします。
詳細な手順は以下のリンクを参照してください。

Visual Studio Code(VSCode)に拡張機能Dev Containersをインストール

拡張機能Dev Containersのインストール

VSCodeの拡張機能マーケットプレイスから「Dev Containers」をインストールします。

image.png

image.png

.devcontainerフォルダを作成し、直下にdevcontainer.jsonを作成する

プロジェクトのルートディレクトリに「.devcontainer」フォルダを作成し、その中に「devcontainer.json」ファイルを作成します。

image.png

devcontainer.jsonの例:

devcontainer.json
{
	"name": "Ubuntu-CUDA-Python",
	"image": "nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04",
	"features": {
		"ghcr.io/devcontainers/features/python:1": {
			"installTools": true,
			"version": "3.12"
		}
	},
	"runArgs": [
		"--gpus",
		"all"
	],
	"customizations": {
		"vscode": {
			"extensions": [
				"ms-python.black-formatter"
			]
		}
	}
	// Use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "pip install requirements.txt",
}

コンテナーで再度開く

VSCodeの左下にあるリモートウィンドウアイコンをクリックし、「コンテナーで再度開く」を選択します。

image.png

オプション選択が表示されるので、「コンテナーで再度開く」をクリックする。

image.png

CUDAおよびGPU が使用できるかの確認

以下のコマンドを実行し、PyTorchをインストールしてCUDAとGPUが使用可能かを確認します。

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

image.png

以上の手順により、WSL上のUbuntu-22.04でGPUを使用可能なDocker開発環境が構築できます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2