この記事の概要

    • Windows 上の VS Code で Dev Container 環境を構築。

 

    • Docker Desktop for Windows をインストールしない(※)。

 

    • Ubuntu へ Docker Engine をインストールする。

 

    • Dockerfile、docker-compose.yml を使用しない(作らない)。

「Dev container を試してみる」が目的です。

Rust の dev container で Hello, world! の動作を確認する。

※ 公式には Dev Container の使用に Docker Desktop for Windows は必要の様です。

前提

ホストOS はクリーンインストールした Windows11 22H2 です。
Windows10 では WSL2 のインストールが成功すれば、後は多分同じはずです。
この記事では、物理PCではなく VMware Workstation 上の仮想マシンで確認しています。

手順

WSL2 Ubuntu のインストール

Windows PowerShell を起動して、以下を実行します。

wsl --install

インストールが完了したら再起動させます。

インストール中: 仮想マシン プラットフォーム
仮想マシン プラットフォーム はインストールされました。
インストール中: Linux 用 Windows サブシステム
Linux 用 Windows サブシステム はインストールされました。
インストール中: Ubuntu
Ubuntu はインストールされました。
要求された操作は正常に終了しました。変更を有効にするには、システムを再起動する必要があります。

何度か再起動が繰り返されます。ログインすると、自動的に Ubuntu のウィンドウが開きます。
ユーザー名、パスワード、パスワード(確認)の順に入力します。

Ubuntu は既にインストールされています。
Ubuntu を起動しています...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: <ユーザー名>
New password: <パスワード>
Retype new password: <パスワード(確認)>
passwd: password updated successfully
この操作を正しく終了しました。
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.90.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/home/ain/.hushlogin file.
ユーザー名@dev-container:~$

以下の様にメッセージが表示された場合は、仮想化が有効になっていません。BIOS 等の設定を確認して、wsl –install を再度実行します。
Ubuntu は既にインストールされています。
Ubuntu を起動しています…
Installing, this may take a few minutes…
WslRegisterDistribution failed with error: 0x80370102
Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS.
For information please visit https://aka.ms/enablevirtualization
Press any key to continue…

Docker Engine のインストール

公式サイトの Install Docker Engine on Ubuntu -> Install using the apt repository 等を参照して行います。

複数行のコマンドを単純にコピペすると、正しく実行できない場合があります。例えば新規インストールでは、最初に以下の内容を実行する必要があります。この時に sudo のパスワードを求められると、2行目がパスワードの代わりとして入力されてしまいます。正しいパスワードを入力すると 1行目は実行されますが、2行目は実行されません。当然の動作なのですが、気付けないとハマります。
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

一行ずつ張り付けするか、以下の様に && \ で行を連結してから張り付けます。
sudo apt-get update && \
sudo apt-get install ca-certificates curl gnupg

以下の様に apt-get update が失敗する場合がありますが、大抵は WSL Ubuntu 自体の再起動で直ります。https://ginpen.com/2021/06/05/apt-update-release-file-is-not-valid-yet/ で詳しく書かれています。作業中に suspend/resume しなければ、通常では発生しない様です。
E: Release file for http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease is not valid yet (invalid for another 5h 24min 9s). Updates for this repository will not be applied.
E: Release file for http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease is not valid yet (invalid for another 6h 14min 25s). Updates for this repository will not be applied.
E: Release file for http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease is not valid yet (invalid for another 4h 28min 5s). Updates for this repository will not be applied.

root 以外のユーザーから docker を使用できる様にする

sudo usermod -aG docker $USER

WSL の再起動

Windows PowerShell から以下を入力します。

wsl --shutdown
wsl

VS Code のインストール

公式サイトからダウンロード、インストールします。日本語化等はお好みで。

 

WSL 拡張機能を追加

    1. VS Code 上で Shift+Ctrl+X を押下して、拡張機能を表示します。

 

    1. 検索キーワードへ WSL と入力して、(多分)最初に出てくる WSL を選択します。

 

    Install を押下してインストールします。
image.png
image.png
image.png
image.png
image.png

Dev Containers 拡張機能を追加

    1. VS Code 上で Shift+Ctrl+X を押下して、拡張機能を表示します。

 

    1. Dev Containers を検索します。

 

    1. Install Locally を押下してインストールします。

 

    インストール完了(Install Locally が Uninstall に変わる)まで待ちます。
image.png

Dev container の使用

例として Rust の Dev container で hello world を実行してみます。

image.png

ディレクトリの準備

    1. ファイルを保存するディレクトリを、WSL Ubuntu 上の /home/<ユーザー名>/ 以下に作っておきます。

 

    1. Start の下の OpenFolder… を選択(または Ctrl+K, Ctrl+O を押下)します。

 

    Open Folder ドロップダウンリストが開きます。(1) で作成したディレクトリを選択します。ここでは、リストの最後にある rust-dev-container-test を選択して進めます。
image.png
image.png

VS Code 1.78.1 以降から、デフォルトでは UNC パスを拒否します。以前は Ubuntu 上のディレクトリを開く際、\\wsl.localhost\Ubuntu\… の様なパスを指定可能だった様です。
Working with UNC paths に公式の情報があります。

image.png

Dev container の追加

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

Rust Dev Container の確認

Ctrl+Shift+@ でターミナルを開くと、docker container 内の bash が表示されます。
rustc (Rust コンパイラ)のバージョンを確認してみます。

vscode ➜ /workspaces/rust-dev-container-test $ rustc --version
rustc 1.69.0 (84c898d65 2023-04-16)
vscode ➜ /workspaces/rust-dev-container-test $

hello world の実行

準備

cargo init を実行します。

vscode ➜ /workspaces/rust-dev-container-test $ cargo init
     Created binary (application) package
vscode ➜ /workspaces/rust-dev-container-test (master) $
image.png
fn main() {
    println!("Hello, world!");
}

実行

F5 キーを押して実行します。

Console is in 'commands' mode, prefix expressions with '?'.
Launching: /workspaces/rust-dev-container-test/target/debug/rust-dev-container-test
Launched process 3723
Process exited with code 0.

なぜか初回はこの出力になりますが、気にせず、再度 F5 キーを押します。

Hello, world!
vscode ➜ /workspaces/rust-dev-container-test (master) $

表示できました。

ローカルでの開発と同じ様に、デバッグ実行も可能です。

ファイルの保存場所について

上記の手順で起動した Rust の Dev Container に必要なファイルは、WSL Ubuntu のローカルディレクトリ /home/${USER}/rust-dev-container-test/ 内に生成されます。

/home/${USER}/rust-dev-container-test/ は Docker container 上では /workspace/rust-dev-container-test/ として mount されています。

/home/ が丸ごと /workspace/ として mount されている訳ではありません。/home/ 内の他のファイルは見えません。

Rust の cargo install 実行等による /workspace/ 外の変更は、Docker container で保持されます。当然ですが、container を削除すると変更は失われます。

Rust dev container 起動直後の container は約 214MB です。

$ docker ps -s
CONTAINER ID   IMAGE                                  COMMAND                  CREATED          STATUS          PORTS     NAMES                SIZE
2c4e5a734ed9   vsc-rust-dev-container-test-XXXX-uid   "/bin/sh -c 'echo Co…"   22 minutes ago   Up 22 minutes             naughty_mcclintock   214MB (virtual 2.43GB)

cargo install cago-watch を行った後は約 283MB となります。

$ docker ps -s
CONTAINER ID   IMAGE                                  COMMAND                  CREATED          STATUS          PORTS     NAMES                SIZE
2c4e5a734ed9   vsc-rust-dev-container-test-XXXX-uid   "/bin/sh -c 'echo Co…"   28 minutes ago   Up 28 minutes             naughty_mcclintock   283MB (virtual 2.5GB)

なお、Try a Dev Container で起動した Dev Container のファイルはローカルディレクトリ上に見えず、全て Docker で管理されます。 /workspace/ は Docker volume、/workspace/ 外の変更は上と場合と同じく、Docker container です。

$docker volume ls
DRIVER    VOLUME NAME
local     vscode
local     vscode-remote-try-rust-f4a98917819ee8715d578a94b93e2a78688188f686e1b1dcd83fed7e6ab0d330

余談

VMware 仮想マシン上での環境構築について

WSL はハイパーバイザー上で仮想マシンとして動作します。同様に、VMware 仮想マシン内でもハイパーバイザーを必要するため、VMware 仮想マシン上の WSL は「仮想マシン内のハイバーバイザー上の仮想マシン(= ネストした仮想マシン)」として動作します。仮想マシン内でのパイパーバイザーは、通常の OS では動作に必須ではないため、デフォルトは無効の設定になっています。有効にするには、仮想マシン設定の「Intel VT-x/EPT AMD-V/RVI 仮想化」オプションを ON にします。ただし、このオプションは、ホストOS上で既にハイパーバイザーまたは VBS (仮想ベースセキュリティ)が有効の状態では動作しません。(必要は無いと思いますが)、ホストの WSL と、VMware 仮想マシン内の WSL は同時に使用できない、という事になります。

参考

 

bannerAds