🌏

オンプレのML開発環境の紹介

に公開

1.はじめに

本記事では、オンプレのML開発環境について紹介します。今回使用するオンプレ環境はこちらになります。こちらの記事ではUbuntu Desktopを使用していますが、本記事ではUbuntu Serverを使用して開発環境を構築します。

https://zenn.dev/eiden0029/articles/my-custom-pc

2.開発環境の概要

手元のPCからUbuntu ServerにSSHで接続し、コンテナを立ち上げて、コンテナ内で作業を行います。SSH接続にはVS CodeのRemote SSH拡張機能を使用し、コンテナの管理にはDev Containers拡張機能を使用します。これにより、手元のPCのVS Codeから直接サーバー上のコンテナにアクセスして作業が可能になります。ちなみに手元にあるPCはMacです。

3.開発環境の構築

3.1.VSCodeに拡張機能をインストール

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers

3.2.秘密鍵と公開鍵

手元のPCで以下のコマンドを実行し、秘密鍵と公開鍵を作成します。

ssh-keygen -t ed25519

作成した公開鍵をUbuntu Serverに登録します。

ssh-copy-id -i ~/.ssh/id_ed25519.pub username@192.168.XXX.XXX

次に、以下の内容を~/.ssh/configファイルに追加し、SSH接続の設定を行います。

Host ubuntu-server
  HostName 192.168.XXX.XXX
  User username
  IdentityFile ~/.ssh/id_ed25519

3.3.Ubuntu ServerにSSH接続

3.4.コンテナの作成と起動

SSH接続の完了後に以下のファイルを作成し、コンテナの作成と起動を行います。

.devcontainer/
├── compose.yaml
├── devcontainer.json
└── Dockerfile
compose.yaml
services:
  kaggle:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - $PWD/input:/kaggle/input
      - $PWD:/kaggle/working
    environment:
      - TZ=Asia/Tokyo
    working_dir: /kaggle/working
    ports:
      - "8888:8888"
    command: jupyter lab --allow-root --ip=0.0.0.0 --port=8888 --no-browser --NotebookApp.token=''
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [ gpu ]
    shm_size: '8gb'
devcontainer.json
{
    "name": "hogehoge",
    "dockerComposeFile": "./compose.yaml",
    "service": "kaggle",
    "workspaceFolder": "/kaggle/working",
    "customizations": {
        "vscode": {
            "extensions": [
                "ms-python.python",
                "ms-toolsai.jupyter",
                "charliermarsh.ruff",
                "GitHub.copilot",
                "GitHub.copilot-chat"
            ],
            "settings": {
                "[python]": {
                    "editor.formatOnSave": true,
                    "editor.codeActionsOnSave": {
                        "source.fixAll": "explicit",
                        "source.organizeImports": "explicit"
                    },
                    "editor.defaultFormatter": "charliermarsh.ruff"
                },
                "notebook.formatOnSave.enabled": true,
                "notebook.codeActionsOnSave": {
                    "notebook.source.fixAll": "explicit",
                    "notebook.source.organizeImports": "explicit"
                },
                "notebook.defaultFormatter": "charliermarsh.ruff"
            }
        }
    }
}
Dockerfile
FROM gcr.io/kaggle-gpu-images/python:v162

RUN pip install --no-cache-dir \
    hydra-core

あとは「コンテナーで再度開く」を選択すれば、コンテナが立ち上がり、開発環境の構築は完了です。

4.おわりに

本記事ではオンプレのML開発環境の構築方法について簡単に紹介しました。手元のPCからSSHで接続し、コンテナ内で作業を行うことで、効率的な開発環境を実現できます。ぜひ参考にして、ご自身の環境に合わせた開発環境を構築してみてください。

Discussion