LoginSignup
4
8

WSL2:docker-cliのインストール手順決定版はこれだ(Ubuntu22.04/systemd有効)

Last updated at Posted at 2023-10-19

このページの手順でできること

  • WSL2(systemd有効)を利用できる
  • docker-cliをインストールできる
  • コンテナを実行できる

早速やってみよう

WSL2のインストール

インストール手順1に従って、WSL2を有効化する。

  • 前提条件
    • Windows10またはWinsows11(22H2以降、最新のUpdate適用済)であること
    • 組織内プロキシによるアクセス制限がないこと(全体公開版の手順では範囲外)
    • 「仮想化」が有効であること(タスクマネージャーから確認可能)

「仮想化」が有効になっていない場合、UEFIの設定から「VT-x」あるいは「Intel Virtualization Technology」を有効にする

image.png

WSL2の有効化

WSL2が既に有効の場合は、省略可能。

# WSL2を有効化する。まだUbuntuをインストールしない。
wsl --install --no-distribution
wsl --update

# Microsoft Storeが使用できない場合、--web-downloadオプションを付ける。
# wsl --install --no-distribution --web-download

WSL2(Ubuntu22.04)のインストール

# Ubuntuをインストール
wsl --install --distribution Ubuntu-22.04

# Microsoft Storeが使用できない場合、--web-downloadオプションを付ける。
# wsl --install --distribution Ubuntu-22.04 --web-download

(任意)dockerのインストールと直接関係ない下準備

(任意)systemdを有効化する

systemdは最初から有効になりました。

以下、systemdが有効になっていない場合の手順
# (任意)systemdを有効化する
echo -e '[boot]\nsystemd=true' | sudo tee /etc/wsl.conf

/etc/wsl.conf の内容は以下のコマンドで確認できる

cat /etc/wsl.conf

想定出力は以下の通り。

[boot]
systemd=true

最後に、WSLを忘れずに再起動すること。再起動後は、以下のコマンドでpidを確認できる

ps -ae | grep systemd

想定出力は以下の通り。systemdのpidが1になっている

      1 ?        00:00:00 systemd
      2 ?        00:00:00 init-systemd(Ub
     40 ?        00:00:00 systemd-journal
     63 ?        00:00:00 systemd-udevd
    123 ?        00:00:00 systemd-resolve
    172 ?        00:00:00 systemd-logind
    463 ?        00:00:00 systemd

(任意)Ubuntuのアップグレードを済ませておく

# (任意)事前準備として、関係ないアップグレードは済ませておく
sudo apt-get update
sudo apt-get upgrade -y

dockerのインストール手順

インストール手順2に従って、dockerをインストールする。

必要なソフトウェアの事前ダウンロード

組織内プロキシによるアクセス制限がかかっている場合、別途http_proxy環境変数が必要
環境変数がなかった場合、aptリポジトリに接続できない

# 必要なソフトウェアの事前ダウンロード
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg

docker用のaptリポジトリを追加する

Ubuntu22.04以降の場合gpg --dearmorは不要になったため、手順を短縮している。3
ただし、以下に示す手順はUbuntu20.04では機能しない。

# docker用のaptリポジトリを追加する
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu jammy stable' | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update

以下のコマンドでaptリポジトリの登録内容を確認できる

登録内容の確認方法と想定出力
cat /etc/apt/keyrings/docker.asc
cat /etc/apt/sources.list.d/docker.list 

想定出力は以下の通り

-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFit2ioBEADhWpZ8/wvZ6hUTiXOwQHXMAlaFHcPH9hAtr4F1y2+OYdbtMuth
(中略)
YT90qFF93M3v01BbxP+EIY2/9tiIPbrd
=0YYh
-----END PGP PUBLIC KEY BLOCK-----
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu jammy stable

dockerをインストール

# dockerをインストール
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

以下のコマンドでdockerの実行状態を確認できる

登録内容の確認方法と想定出力
sudo systemctl status docker

想定出力は以下の通り

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-10-19 22:53:11 JST; 30s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 11405 (dockerd)
      Tasks: 17
     Memory: 33.5M
     CGroup: /system.slice/docker.service
             └─11405 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
(以下省略)

dockerを動かそう

hello worldを実行してみる

以下のコマンドでdockerのコンテナを実行できる

sudo docker pull docker.io/library/hello-world
sudo docker run docker.io/library/hello-world
想定出力
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/

組織内プロキシによるアクセス制限がかかっている場合、systemdとdockerに別途設定が必要
設定を変更しなかった場合、docker.io(dockerhub)に接続できないため docker pullできない

参考資料

Microsoft公式:WSL2のインストール方法

docker公式:dockerのインストール方法

gpgはASCII Armor の形式でよい

  1. Microsoft公式:WSL2のインストール方法

  2. docker公式:dockerのインストール方法

  3. debian公式:apt-keyマニュアル

4
8
2

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