8
10

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でDocker

Last updated at Posted at 2022-12-19

はじめに

会社の開発にDockerが必要っぽいのだが、Docker Desktopが有料化してるので、WindowsでDocker使えないと思っていたのですが、WSL2のLinux上でDocker入れれば使えるらしい(?)ので、セットアップしてみたらまあまあハマったので備忘録がてら記事にしてみます。

環境は、「Windows11」でLinuxは「Ubuntu20.04」です。

Windowsの機能

まず、WSLを使うには、「Windowsの機能」で「Linux用Windowsサブシステム」にチェックが入っている必要があります。(下図の赤枠の部分)

2022-12-18_09h11_52.png

ちなみに、「Windowsの機能」はコントールパネルから表示も出来ますが、以下のコマンドでも表示されます。
コマンドプロンプトやPowerShellから実行します。

OptionalFeatures

WSL2の設定

WSLのステータスを見ます。
以下を、コマンドプロンプトやPowerShellから実行します。

wsl --status

現在の私の環境では以下のようになりますが、「規定のバージョン」が「2」になっていたら問題ありません。

C:\WINDOWS\system32>wsl --status
既定のディストリビューション: Ubuntu-20.04
既定のバージョン: 2

「規定のバージョン」が「1」の場合、以下を実行して、デフォルトのバージョンを「2」に変更します。

wsl --set-default-version 2

私の場合、うまく変更できずに以下の「手順 4 - Linux カーネル更新プログラム パッケージをダウンロードする」を実行する必要がありました。

Ubuntuのインストール

Microsoft Storeから、Ubntuを入手します。

Ubuntuインストール後はOSを最新にしておきます。

sudo apt update
sudo apt upgrade

Dockerのインストール

UbuntuにDocker Engineをインストールします。(今後は、UbuntuのCLIとなります。)
以下を参考にリポジトリからインストールします。

過去のDockerを削除します。

コマンド

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

実行ログ

$ sudo apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package docker-engine

リポジトリのセットアップ

Dockerのリポジトリをセットアップします。

1.リポジトリのセットアップに必要なパッケージをインストール

コマンド

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

実行ログ

user@MyComputer:~$ sudo apt-get update
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done

user@MyComputer:~$ sudo apt-get install \
>     ca-certificates \
>     curl \
>     gnupg \
>     lsb-release
Reading package lists... Done
Building dependency tree
Reading state information... Done
lsb-release is already the newest version (11.1.0ubuntu2).
lsb-release set to manually installed.
ca-certificates is already the newest version (20211016ubuntu0.20.04.1).
ca-certificates set to manually installed.
curl is already the newest version (7.68.0-1ubuntu2.14).
curl set to manually installed.
gnupg is already the newest version (2.2.19-3ubuntu2.2).
gnupg set to manually installed.
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 libxmlb1
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

2.DockerのオフィシャルGPGキーを追加

コマンド

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

3.リポジトリのセットアップ

コマンド

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

Docker Engineインストール

1.apt更新

Dockerのリポジトリを追加したので、aptを最新に更新します。

コマンド

sudo apt-get update

実行ログ

user@MyComputer:~$ sudo apt-get update
Get:1 https://download.docker.com/linux/ubuntu focal InRelease [57.7 kB]
Get:2 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages [22.2 kB]
Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Fetched 79.9 kB in 1s (73.7 kB/s)
Reading package lists... Done

2.最新のDocker Engineをインストール

コマンド

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
実行ログ
user@MyComputer:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  docker-ce-rootless-extras docker-scan-plugin pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin docker-scan-plugin pigz
  slirp4netns
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 111 MB of archives.
After this operation, 428 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 https://download.docker.com/linux/ubuntu focal/stable amd64 containerd.io amd64 1.6.13-1 [27.7 MB]
Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 slirp4netns amd64 0.4.3-1 [74.3 kB]
Get:4 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-cli amd64 5:20.10.22~3-0~ubuntu-focal [41.5 MB]
Get:5 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce amd64 5:20.10.22~3-0~ubuntu-focal [20.5 MB]
Get:6 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-ce-rootless-extras amd64 5:20.10.22~3-0~ubuntu-focal [8395 kB]
Get:7 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-compose-plugin amd64 2.14.1~ubuntu-focal [9562 kB]
Get:8 https://download.docker.com/linux/ubuntu focal/stable amd64 docker-scan-plugin amd64 0.23.0~ubuntu-focal [3622 kB]
Fetched 111 MB in 11s (10.4 MB/s)
Selecting previously unselected package pigz.
(Reading database ... 32633 files and directories currently installed.)
Preparing to unpack .../0-pigz_2.4-1_amd64.deb ...
Unpacking pigz (2.4-1) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../1-containerd.io_1.6.13-1_amd64.deb ...
Unpacking containerd.io (1.6.13-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../2-docker-ce-cli_5%3a20.10.22~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-cli (5:20.10.22~3-0~ubuntu-focal) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../3-docker-ce_5%3a20.10.22~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce (5:20.10.22~3-0~ubuntu-focal) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../4-docker-ce-rootless-extras_5%3a20.10.22~3-0~ubuntu-focal_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:20.10.22~3-0~ubuntu-focal) ...
Selecting previously unselected package docker-compose-plugin.
Preparing to unpack .../5-docker-compose-plugin_2.14.1~ubuntu-focal_amd64.deb ...
Unpacking docker-compose-plugin (2.14.1~ubuntu-focal) ...
Selecting previously unselected package docker-scan-plugin.
Preparing to unpack .../6-docker-scan-plugin_0.23.0~ubuntu-focal_amd64.deb ...
Unpacking docker-scan-plugin (0.23.0~ubuntu-focal) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../7-slirp4netns_0.4.3-1_amd64.deb ...
Unpacking slirp4netns (0.4.3-1) ...
Setting up slirp4netns (0.4.3-1) ...
Setting up docker-scan-plugin (0.23.0~ubuntu-focal) ...
Setting up containerd.io (1.6.13-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-compose-plugin (2.14.1~ubuntu-focal) ...
Setting up docker-ce-cli (5:20.10.22~3-0~ubuntu-focal) ...
Setting up pigz (2.4-1) ...
Setting up docker-ce-rootless-extras (5:20.10.22~3-0~ubuntu-focal) ...
Setting up docker-ce (5:20.10.22~3-0~ubuntu-focal) ...
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Created symlink /etc/systemd/system/sockets.target.wants/docker.socket → /lib/systemd/system/docker.socket.
invoke-rc.d: could not determine current runlevel
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.19) ...

2.検証

Dockerの検証を行います。

sudo docker run hello-world

ちなみに、私はうまくいきませんでした。

user@MyComputer:~$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

Dockerを再起動します。

コマンド(Dockerサービス停止)

sudo service docker stop

コマンド(Dockerサービス開始)

sudo service docker start

コマンド(Dockerサービスステータス確認)

sudo service docker status

実行ログ

user@MyComputer:~$ sudo service docker stop
 * Docker already stopped - file /var/run/docker-ssd.pid not found.
user@MyComputer:~$ sudo service docker start
 * Starting Docker: docker                                                                                       [ OK ]
user@MyComputer:~$ sudo service docker status
 * Docker is running

再びDockerの検証を行います。

sudo docker run hello-world

実行ログ

user@MyComputer:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833
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/

無事、検証できました。

まとめ

ちょいちょい変わっているので、たまにやるとハマります。
定期的に追っておかないといけないなぁと思いました。

それにしても、便利なものが有料化すると方式を変えないといけないので、代替案を常にもってないとダメそうです。

8
10
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
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?