goroumaru41gou

遊んでいる中でインプットした内容をアウトプットする場

WSL2 + docker導入について

WSL2導入の備忘録とする。

取り扱うもの

環境

  • windows10 Pro(ver1909、build18363.1256)

 ※build 18363.1049 以降であればよい

  • windows terminal インストール済み

 ※インストールされてなくてもよい

  • docker for windows インストール済み

 ※インストールされてなくてもよい

1. 機能追加

power shellで、ふたつの機能を有効にする。

ふたつとも有効にできたら、PCを再起動する。

1.1 ひとつめの機能追加

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

イメージのバージョン: 10.0.18363.1256

機能を有効にしています
[==========================100.0%==========================]
操作は正常に完了しました。

1.2 ふたつめの機能追加

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
展開イメージのサービスと管理ツール
バージョン: 10.0.18362.1139

イメージのバージョン: 10.0.18363.1256

機能を有効にしています
[==========================100.0%==========================]
操作は正常に完了しました。

2.WSL2 カーネル更新

以下から更新プログラムをダウンロードして実行する。

x64 マシン用 WSL2 Linux カーネル更新プログラム パッケージ

f:id:goroumaru41gou:20201225105431p:plain この画面が表示されたら、Finishを押して終了。

3.WSL2を規定へ設定

wsl --set-default-version 2

これが表示されたら、wsl2を規定にセットできている。

WSL 2 との主な違いについては、https://aka.ms/wsl2 を参照してください

4.Ubuntu20.04LTSインストール

以下のリンクからインストールする。

Ubuntu 20.04 LTS

f:id:goroumaru41gou:20201225110757p:plain 入手を押すと、Microsoft Storeに移行するので、そちらで再び入手を押す。

5.Ubuntu20.04LTS起動

WindowsスタートアップメニューからUbuntuを起動する。

f:id:goroumaru41gou:20201225111712p:plain

このとき、1~2分経ってもubuntuターミナルに何も表示されなかったとき、PCを再起動してから再度試す

5.1 ubuntuアカウント登録

Ubuntu用のアカウントとパスワードを登録する。

このアカウントは、Windowsアカウントとは別ものとなる。

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

パスワード登録が完了すると、続けて以下が表示される。

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)

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

  System information as of Fri Dec 25 11:34:37 JST 2020

  System load:  0.34               Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 2%                 IPv4 address for eth0: <IP addr>
  Swap usage:   0%

1 update can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once once a day. To disable it please create the
/home/<user>/.hushlogin file.
<user name>@<PC device name>:~$ 

5.2 ubuntuアップデート&アップグレード

sudo apt update

アップデートが正常に完了したら、アップグレードする。

sudo apt upgrade

5.3 その他セットアップ

必要に応じて、ubuntuをセットアップする。

6. windows terminal導入

bash(ubuntu)、power shell(windows)の切り替えが簡単になるので、windows terminalをこちらからインストールする。

また、windows terminalのカスタマイズは、必要に応じて実施する。

qiita.com

7. docker導入

dockerが利用できるようにする。

7.1 docker desktop for windowsインストール

以下からwindows版のdocker desktopをこちらからインストールする。

インストールについては、この記事を参考にして進める。 qiita.com

7.2 dockerとwsl2起動状況

power shellなどでwsl起動状況を確認する。

wsl -l -v

dockerインストール直後だと、以下となっている。

  NAME                   STATE           VERSION
* docker-desktop-data    Running         2
  Ubuntu-20.04           Stopped         2
  docker-desktop         Running         2

wsl2のデフォルトLinuxディストリビューションubuntuへ設定する。

wsl --set-default Ubuntu-20.04

すべてRunningステータスであれば、問題ない。

  NAME                   STATE           VERSION
* Ubuntu-20.04           Running         2
  docker-desktop         Running         2
  docker-desktop-data    Running         2

7.3 dockerコンテナ起動

windows terminalを起動して、ubuntu20.04のタブを開く。 f:id:goroumaru41gou:20201225133650p:plain

試しにdocker desktopのお試しコンテナ起動するため、ubuntuでdockerコマンドを入力し、コンテナを起動する。

docker run -d -p 80:80 docker/getting-started

docker imageをPullしてきているのが確認できる。

Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
188c0c94c7c5: Pull complete
617561f33ec6: Pull complete
7d856acdaa9c: Pull complete
a0d3c6e28e6d: Pull complete
af69a9b963c8: Pull complete
0739f3815ad8: Pull complete
7c7b75d0baf8: Pull complete
Digest: sha256:b821569034e3b5fae03b40e64a866017067f3bf17effe185b782bdbf02179528
Status: Downloaded newer image for docker/getting-started:latest
4e673700023e91cbe1ede4463b29084aef96ca0d719f67d2a97e52f437b01dd0

コンテナ起動を確認する。

docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                NAMES
4e673700023e   docker/getting-started   "/docker-entrypoint.…"   52 seconds ago   Up 49 seconds   0.0.0.0:80->80/tcp   cool_chebyshev

docker desktopでもコンテナ起動できていることを確認できる。 f:id:goroumaru41gou:20201225135229p:plain

7.4 dockerコンテナ内へ

起動したコンテナ名 or IDをdocker psで調べる。

起動したコンテナには、bashがなかったので、ashとした。

docker exec --it <コンテナ名 or ID> ash  // ここでは、ID:4e673700023e

コンテナ内に入れた。

/ #

試しにディレクトリを確認してみる。

ls -la
/ # ls -la
total 80
drwxr-xr-x    1 root     root          4096 Dec 25 04:52 .
drwxr-xr-x    1 root     root          4096 Dec 25 04:52 ..
-rwxr-xr-x    1 root     root             0 Dec 25 04:52 .dockerenv
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 bin
drwxr-xr-x    5 root     root           340 Dec 25 04:52 dev
drwxr-xr-x    1 root     root          4096 Nov 25 00:31 docker-entrypoint.d
-rwxrwxr-x    1 root     root          1202 Nov 25 00:30 docker-entrypoint.sh
drwxr-xr-x    1 root     root          4096 Dec 25 04:52 etc
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 home
drwxr-xr-x    1 root     root          4096 Oct 21 09:23 lib
drwxr-xr-x    5 root     root          4096 Oct 21 09:23 media
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 mnt
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 opt
dr-xr-xr-x  163 root     root             0 Dec 25 04:52 proc
drwx------    1 root     root          4096 Dec 25 04:59 root
drwxr-xr-x    1 root     root          4096 Dec 25 04:52 run
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 sbin
drwxr-xr-x    2 root     root          4096 Oct 21 09:23 srv
dr-xr-xr-x   11 root     root             0 Dec 25 04:52 sys
drwxrwxrwt    1 root     root          4096 Nov 25 00:30 tmp
drwxr-xr-x    1 root     root          4096 Oct 21 09:23 usr
drwxr-xr-x    1 root     root          4096 Oct 21 09:23 var

コンテナから抜けるときは、exitを入力する。

7.4 dockerコンテナ削除

コンテナを削除する。

docker rm -f <コンテナ名 or ID>  // ここでは、ID:4e673700023e

削除されたか確認する。コンテナIDが表示されなければ、削除されている。

docker ps -a

これもdocker desktopで確認すると、先ほどのコンテナ表示が消えている。

7.5 docker image確認と削除

インストールされているイメージファイルを確認する。

docker images

docker imagesで確認したイメージファイルIDを使ってイメージを削除する。

docker rmi <image ID>

参考

*docker導入

*Dockerコマンド よく使うやつ