2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WSL2+VSCode+DockerでGPU認識とGit連携

2
Last updated at Posted at 2024-03-10

今までWSL2上のAnaconda+JupyterLabでGPU認識させていた。
そろそろAnaconda卒業してDocker入門し、さらにGit連携して自分のコードの修正点を管理したい。
※VSCodeが入っていること前提の話。

Step 1. WSL2(Ubuntu)の確認

$ lsb_release -a

※実行環境

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

Step 2. Docker Engineのインストール

下記の記事を参考にDocker Engineのインストール
https://qiita.com/fsdg-adachi_h/items/3bceee25ac67cc73ebbe
※Docker Desktopを使う方法もあるが、Linux操作に慣れるためにもDocker Engineを選んだ

$ sudo apt remove docker docker-engine docker.io containerd runc

$ sudo apt update
$ sudo apt install ca-certificates curl gnupg lsb-release

$ sudo mkdir -m 0755 -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

$ 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

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

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

$ exit

$ sudo service docker start
$ docker –-version 

※ 2024年3月現在の出力

Docker version 25.0.3, build 4debf41

Step 3. GPU環境構築の下準備

【注意】NVIDIA DriverはWSL2上ではなくWindows上にインストールすること!

NVIDIA Container Toolkitが必要らしいので公式のinstall guideの通り進めていく
https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
下記も参考に。
https://qiita.com/tkusumi/items/f275f0737fb5b261a868

$ 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
$ sudo apt-get install -y nvidia-container-toolkit

下記のURLから利用可能なNVIDIA CUDA Linux Container image Sourcesを探す
https://gitlab.com/nvidia/container-images/cuda/blob/master/doc/supported-tags.md
※今回、RAPIDSを使いたいのでpython3.10の環境とする。cudaは12.1を選択。
image.png
※このサイトによると"devel"が色々と機能が入っていて良いらしい

Step 4. Dockerfileとdocker-compose.yml作成

下記を参考に。
https://qiita.com/tf63/items/618f192a810c28e4d2b7
https://zenn.dev/ushknn/articles/19e9aa500cb1e7

Cドライブ上のデスクトップをマウント

$ sudo mkdir ./mnt #マウント用のフォルダを作成
$ sudo mkdir ./mnt/desktop #デスクトップマウント用のフォルダを作成
$ sudo mount -t drvfs C:/Users/(ユーザー名)/Desktop ./mnt/desktop #デスクトップをマウント

$ sudo mkdir docker_git # "docker_git"フォルダをデスクトップ上に作成
$ cd docker_git 
$ sudo mkdir py_cuda # "py_cuda”フォルダをdocker_git内に作成
$ cd py_cuda
$ code . # VSCodeを開く

"py_cuda"フォルダ内にDockerfileを作成していく
※&&\でつないだ方が良いらしい

FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04 

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /tmp_container

RUN apt update && \
  apt install -y \
  wget \
  bzip2 \
  build-essential \
  git \
  git-lfs \
  curl \
  ca-certificates \
  libsndfile1-dev \
  libgl1 \
  python3.10 \
  python3-pip

COPY ./requirements.txt /tmp_container
RUN pip3 install --no-cache-dir -U pip && \
      pip3 install --no-cache-dir -r requirements.txt && \
      pip3 install jupyterlab && \
      pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 && \
      pip3 install torch_geometric && \
      pip3 install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-2.2.0+cu121.html

Docker container作成時にインストールしておきたいパッケージをrequirements.txtにまとめておく

#./mnt/desktop/docker_git/py_cuda/requirements.txt
pandas
numpy
matplotlib

docker-compose.yml
https://sepicles.net/build-jupyterlab-on-docker/#toc12

version: '3'

services:
  py310:
    image: py310:cuda121
    container_name: py310-cuda121
    ports:
      - "8008:8888"

    deploy:
      resources:
        reservations:
          devices:
          - driver: nvidia
            count: 1
            capabilities: [gpu]

Step 5. Docker containerの作成

Step 4.で作成したDockerfileをもとにdocker images→docker containerの順に作成していく。
現在VSCodeを開いているので、上メニューバーから、Terminal>New Terminalでターミナルを開く

$ # docker imagesの作成
$ docker build ./ -t py310:cuda121
$ # docker containerの作成

【説明】
$ docker build ./ -t (イメージ名):(タグ名)
$ docker compose up -d
※10分未満でイメージができる。コンテナ作成は一瞬。
http://localhost:8008/lab 
にアクセスして下記の画面が出れば成功!
image.png

Step 6. Git連携

VSCode上でGit連携してコミットによる修正箇所の追跡を残せるようにしたい。
下記を参考に。
https://qiita.com/midori-game/items/0abf0013a70790518738

6-1. initialize Repositry

$ git config --global init.defaultBranch mainを走らすことでデフォルトのmasterブランチがmainブランチとして初期化されるようになる
※現在のディレクトリ(./mnt/desktop/docker_git/py_cuda/)にローカルリポジトリを作成する

$ git --version #git version 2.34.1
$ git config --global user.name (ユーザー名)
$ git config --global user.email (メールアドレス)
$ git config --global core.editor "code --wait"
$ sudo git init #フォルダをマウントしているのでsudoつけないと失敗する
$ code .

6-2. VSCodeを開くとコミットできるようになっている!

スクリーンショット 2024-03-10 115458.png

まとめ

・WSL2(Ubuntu)上でDockerコンテナ作成+Git連携の備忘録
・現時点では、dockerコンテナからjupyterlabを開き、そこにファイルをアップロードして変更->ダウンロードしてGitHubデスクトップ上でコミットをする、という段階。
・もっと楽にGit上でコミットできるようにしたい。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?