Docker Install to Windows WSL2

1. WSL Ubuntuターミナルを起動します
2. Ubuntuを最新の状態にアップデートします。パスワードを求められるので入力します
```bash:Ubuntu - 最新の状態にアップデート
sudo apt update
[sudo] password for teruroom:
```
:::details 実行結果
```bash:Ubuntu - 実行結果
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1135 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [970 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/main Translation-en [506 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 c-n-f Metadata [29.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [205 kB]
Get:11 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [9104 B]
Get:12 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [643 kB]
Get:13 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [91.7 kB]
Get:14 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 c-n-f Metadata [536 B]
Get:15 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [675 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [115 kB]
Get:17 http://security.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [13.0 kB]
Get:18 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [21.8 kB]
Get:19 http://security.ubuntu.com/ubuntu focal-security/multiverse Translation-en [4948 B]
Get:20 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [536 B]
Get:21 http://archive.ubuntu.com/ubuntu focal/universe Translation-en [5124 kB]
Get:22 http://archive.ubuntu.com/ubuntu focal/universe amd64 c-n-f Metadata [265 kB]
Get:23 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [144 kB]
Get:24 http://archive.ubuntu.com/ubuntu focal/multiverse Translation-en [104 kB]
Get:25 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 c-n-f Metadata [9136 B]
Get:26 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1469 kB]
Get:27 http://archive.ubuntu.com/ubuntu focal-updates/main Translation-en [291 kB]
Get:28 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [14.7 kB]
Get:29 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [694 kB]
Get:30 http://archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [99.0 kB]
Get:31 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 c-n-f Metadata [532 B]
Get:32 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [892 kB]
Get:33 http://archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [196 kB]
Get:34 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [20.0 kB]
Get:35 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [24.8 kB]
Get:36 http://archive.ubuntu.com/ubuntu focal-updates/multiverse Translation-en [6928 B]
Get:37 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 c-n-f Metadata [620 B]
Get:38 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [42.0 kB]
Get:39 http://archive.ubuntu.com/ubuntu focal-backports/main Translation-en [10.0 kB]
Get:40 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 c-n-f Metadata [864 B]
Get:41 http://archive.ubuntu.com/ubuntu focal-backports/restricted amd64 c-n-f Metadata [116 B]
Get:42 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [19.5 kB]
Get:43 http://archive.ubuntu.com/ubuntu focal-backports/universe Translation-en [13.4 kB]
Get:44 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [672 B]
Get:45 http://archive.ubuntu.com/ubuntu focal-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 23.1 MB in 6s (3826 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
251 packages can be upgraded. Run 'apt list --upgradable' to see them.
```
:::
3. Dockerをインストールします
```bash:Ubuntu - Dockerをインストール
curl https://get.docker.com | sh
```
:::details 実行結果
```bash:Ubuntu
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18617  100 18617    0     0  34994      0 --:--:-- --:--:-- --:--:-- 34928
# Executing docker install script, commit: 93d2499759296ac1f9c510605fef85052a2c32be

WSL DETECTED: We recommend using Docker Desktop for Windows.
Please get Docker Desktop from https://www.docker.com/products/docker-desktop


You may press Ctrl+C now to abort this script.
+ sleep 20
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sudo -E sh -c curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | gpg --dearmor --yes -o /usr/share/keyrings/docker-archive-keyring.gpg
+ sudo -E sh -c echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu focal stable" > /etc/apt/sources.list.d/docker.list
+ sudo -E sh -c apt-get update -qq >/dev/null
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends  docker-ce-cli docker-scan-plugin docker-ce >/dev/null
+ version_gte 20.10
+ [ -z  ]
+ return 0
+ sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce-rootless-extras >/dev/null

================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

    dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================
```
:::
4. docker-composeをインストールします。パスワードを求められるので入力します。インストールパスは`/usr/local/bin/docker-compose`です
```bash:Ubuntu - docker-composeをインストール
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```
* Shell変数の意味
  * $(uname -s) : Linux
  * $(uname -m) : x86_64
```bash:Ubuntu - パスワード入力
[sudo] password for teruroom:
```
:::details 実行結果
```bash:Ubuntu - 実行結果
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   664  100   664    0     0   2228      0 --:--:-- --:--:-- --:--:--  2228
100 23.5M  100 23.5M    0     0   9.8M      0  0:00:02  0:00:02 --:--:-- 12.5M
```
:::
5. インストールパス`/usr/local/bin/docker-compose`に実行権限を与えます
```bash:Ubuntu - 実行権限を与える
sudo chmod +x /usr/local/bin/docker-compose
```
```bash:Ubuntu - 確認
-rwxr-xr-x 1 root root 24653824 Jan 15 03:03 /usr/local/bin/docker-compose
```
6. インストールされたバージョンを確認します
```basu:Ubuntu インストールされたバージョンを確認
docker-compose --version
```
:::details 実行結果
```basu:Ubuntu - 実行結果
Docker Compose version v2.2.2
```
:::

8. Docker Engineを起動します
```bash:Ubuntu - Docker Engineを起動
sudo service docker start
```
:::details 実行結果
```basu:Ubuntu - 実行結果
 * Starting Docker: docker
```
:::

9. Docker Engineの実行状況を確認します
```bash:Ubuntu - Docker Engineの実行状況を確認
service docker status
```
:::details 実行結果
```basu:Ubuntu - 実行結果
 * Docker is running
```
:::
10. 管理者権限なしでDockerコマンドを実行できるようにWSL実行ユーザアカウントを`docker`グループに追加します
```bash:Ubuntu - WSL実行ユーザアカウントをdockerグループに追加
sudo usermod -aG docker $USER
```
:::details 実行結果
* パスワードを求められるので入力します
```basu:Ubuntu - 実行結果
[sudo] password for teruroom:
```
:::
* Ubuntuターミナルを再起動します
```basu:Ubuntu - ターミナルを閉じる
exit
```

11. `hello-world`実行イメージを実行して動作を確認します
```bash:Ubuntu  hello-worldイメージを実行
docker run --rm hello-world
```
:::details 実行結果
ローカル環境には`hello-world`実行イメージが無いのでリモートライブラリからプルして実行します
```basu:Ubuntu - 実行結果
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
```
:::
12. 実行イメージ一覧を確認します
```bash:Ubuntu - 実行イメージ一覧を確認
docker images
```
:::details 実行結果
```basu:Ubuntu - 実行結果
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB
```
:::
13. 動作確認用の`hello-world`実行イメージを削除します
```bash:Ubuntu - hello-worldイメージを削除
docker rmi hello-world
```
:::details 実行結果
```basu:Ubuntu - 実行結果
Untagged: hello-world:latest
Untagged: hello-world@sha256:975f4b14f326b05db86e16de00144f9c12257553bba9484fed41f9b6f2257800
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
```
:::