WSL2上のUbuntuにDockerをインストールする方法

この記事では『Docker』について、

  • WSL2上のUbuntuにDockerをインストールする方法
  • sudo権限なしでDockerコマンドを実行する方法
  • Dockerが正常にインストールされたことを確認する方法
  • Docker Composeプラグインが正常にインストールされたことを確認する方法

などを分かりやすく説明するように心掛けています。ご参考になれば幸いです。

WSL2上のUbuntuにDockerをインストールする方法

これから「WSL2上のUbuntuにDockerをインストールする方法」を説明します。

Dockerは下記の手順でインストールすることができます。

Dockerをインストールする手順

  • 【事前準備】古いバージョンのDockerをアンインストールする
  • APTが参照するリポジトリのパッケージリストを更新する
  • Dockerをインストールする際に必要なパッケージをインストールする
  • Dockerの公式GPGキーを追加する
  • DockerリポジトリをAPTのソースリストに追加する
  • APTが参照するリポジトリのパッケージリストを再度更新する
  • Dockerの関連パッケージをインストールする

ではこれから上記の手順について順番に説明します。

【事前準備】古いバージョンのDockerをアンインストールする

最新版のDockerをインストールする前に、古いバージョンのDockerの関連パッケージ(docker, docker-engine, docker.io)がインストールされている場合は、次のコマンドを実行して、アンインストールしてください。

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

このコマンドを入力して、「E: Unable to locate package docker」のように「Dockerが見つからなかった」という内容が表示されればOKです。

もし、古いバージョンのDockerの関連パッケージ(docker, docker-engine, docker.io)が見つかって、アンインストールされても、「/var/lib/docker」フォルダに保存されているデータ(イメージやコンテナなど)は削除されません。完全にアンインストールしたい方は、「/var/lib/docker」フォルダの中身も削除してください。

これで、新しいバージョンのDockerをインストールする準備が整います。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
[sudo] password for user01:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package 'docker.io' is not installed, so not removed
E: Unable to locate package docker
E: Unable to locate package docker-engine

APTが参照するリポジトリのパッケージリストを更新する

次のコマンドを入力して、APT(パッケージ管理システム)が参照するリポジトリのパッケージリストを更新します。

sudo apt-get update

具体的には、「sudo apt-get update」コマンドを実行すると、APTは「/etc/apt/sources.list」ファイル「/etc/apt/sources.list.d」フォルダ内のファイルに定義されているリポジトリのURLから最新のパッケージ情報を取得し、「/var/lib/apt」フォルダにあるパッケージ情報を更新します。そのパッケージ情報を元に、APTはパッケージのインストールやアップグレードを行うことができます。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ sudo apt-get update
[sudo] password for user01:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
(省略)
Get:42 http://archive.ubuntu.com/ubuntu jammy-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 26.6 MB in 10s (2699 kB/s)
Reading package lists... Done

Dockerをインストールする際に必要なパッケージをインストールする

次のコマンドを入力して、Dockerをインストールする際に必要なパッケージをインストールします。行末のバックスラッシュ(\)マークの前にあるスペースを忘れないように注意しましょう。

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

このコマンドを実行すると、APTは「/var/lib/apt」フォルダにあるパッケージ情報から上記のパッケージを検索して、インストールします。また、必要に応じて依存パッケージも自動的にインストールします。

インストールしているパッケージ

  • ca-certificates
    • このパッケージには多くの主要な証明書認定局(CA)から発行されたルート証明書(Root Certificate)が含まれています。このパッケージがインストールされていない状態で、HTTPS通信しようとすると、CA証明書エラーが発生します。
  • curl
    • このパッケージをインストールすると、curlコマンドを使用して、インターネット上のリソースにアクセスしたり、データのダウンロードやアップロードができるようになります。この後に説明する「4. Dockerの公式GPGキーを追加する」でcurlコマンドを使用しています。
  • gnupg
    • GNU Privacy Guard(GnuPG)と呼ばれる暗号化とデジタル署名のソフトウェアを提供するパッケージです。このパッケージをインストールすると、gpgコマンドを使用して、「鍵の生成、インポート、エクスポート」や「ファイルやメッセージの暗号化、復号化、署名、検証」などができるようになります。この後に説明する「4. Dockerの公式GPGキーを追加する」でcurlコマンドを使用しています。
  • lsb-release
    • このパッケージをインストールすると、lsb_releaseコマンドを使用して、Linuxディストリビューションの情報を取得することができます。この後に説明する「4. Dockerの公式GPGキーを追加する」でlsb_releaseコマンドを使用しています。
  • apt-transport-https
    • APTがHTTPS経由でリポジトリを利用できるようにするために必要です。DockerのリポジトリはHTTPSで提供されているため、このパッケージが必要になります。しかし、「apt ver.1.6以降(Ubuntu 18.04 LTS以降に相当)」では、HTTPSのリポジトリも正式にサポートするようになったので、apt-transport-httpsパッケージをインストールする必要はありませんが、明示的にインストールしておくことで、将来の互換性を確保できます。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ sudo apt-get install \
>     ca-certificates \
>     curl \
>     gnupg \
>     lsb-release \
>     apt-transport-https
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
lsb-release is already the newest version (11.1.0ubuntu4).
lsb-release set to manually installed.
ca-certificates is already the newest version (20211016ubuntu0.22.04.1).
ca-certificates set to manually installed.
gnupg is already the newest version (2.2.27-3ubuntu2.1).
gnupg set to manually installed.
The following additional packages will be installed:
  libcurl4
The following NEW packages will be installed:
  apt-transport-https
The following packages will be upgraded:
  curl libcurl4
2 upgraded, 1 newly installed, 0 to remove and 76 not upgraded.
Need to get 486 kB of archives.
After this operation, 175 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 apt-transport-https all 2.4.8 [1506 B]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 curl amd64 7.81.0-1ubuntu1.10 [194 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libcurl4 amd64 7.81.0-1ubuntu1.10 [290 kB]
Fetched 486 kB in 3s (164 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 24112 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_2.4.8_all.deb ...
Unpacking apt-transport-https (2.4.8) ...
Preparing to unpack .../curl_7.81.0-1ubuntu1.10_amd64.deb ...
Unpacking curl (7.81.0-1ubuntu1.10) over (7.81.0-1ubuntu1.6) ...
Preparing to unpack .../libcurl4_7.81.0-1ubuntu1.10_amd64.deb ...
Unpacking libcurl4:amd64 (7.81.0-1ubuntu1.10) over (7.81.0-1ubuntu1.6) ...
Setting up apt-transport-https (2.4.8) ...
Setting up libcurl4:amd64 (7.81.0-1ubuntu1.10) ...
Setting up curl (7.81.0-1ubuntu1.10) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

補足

今回インストールしているパッケージ(ca-certificates, curl, gnupg, lsb-release, apt-transport-https)はデフォルトでインストールされていることがあります。

インストールされているパッケージの一覧は下記のコマンドで取得することができます。

sudo apt list --installed

ちなみに、2023年4月にインストールした際には、「apt-transport-httpsパッケージ」以外はすべてデフォルトでインストールされていました。

しかし、Dockerをインストールする際の手順では、これらのパッケージを明示的にインストールしておくことが推奨されています。

「sudo apt-get install」コマンドでパッケージを明示的にインストールすることで、これらのパッケージがインストールされていない場合でも、必要なパッケージが確実に揃うことが保証されます。

【補足】インストールされているパッケージの保存場所を知る方法

インストールされているパッケージの保存場所は以下のコマンドを実行すれば分かります。

dpkg -L <パッケージ名>

Dockerの公式GPGキーを追加する

次のコマンドを入力して、Dockerの公式GPGキーを追加します。エラーが表示されなければ成功しています。

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

このコマンドを実行すると、インストールしたDockerパッケージの検証が可能になります。

前半のcurlコマンド「curl -fsSL https://download.docker.com/linux/ubuntu/gpg」では、Ubuntuで利用できるDockerの公式GPGキーをURLからダウンロードしています。ダウンロードされたGPGキーは、ASCII形式でエンコードされています。

後半のgpgコマンド「sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg」では、ダウンロードしたGPGキー(ASCII形式)をバイナリ形式に変換し、変換した結果を「/usr/share/keyrings/docker-archive-keyring.gpg」ファイルに保存しています。

このコマンドは、Dockerパッケージをインストールする際に、改ざんなどからシステムを保護するための重要なステップです。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
udo gpg > sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

DockerリポジトリをAPTのソースリストに追加する

次のコマンドを入力して、DockerリポジトリをAPTのソースリスト(「/etc/apt/sources.list.d」フォルダ)に追加します。

 echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
  https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

このコマンドではDockerリポジトリの情報を「/etc/apt/sources.list.d」フォルダに書き込むことで、APTのパッケージソースリストに追加しています。このコマンドを実行すると、「/etc/apt/sources.list.d」フォルダに「docker.list」ファイルが追加されます。

このコマンドは複雑なので、各コマンドの意味を下記に示します。

各コマンドの意味

  • echo
    • echoコマンドです。引数として与えられた文字列を出力します。
    • 今回のコマンドでは、Dockerリポジトリの情報を出力しています。
  • deb
    • DockerリポジトリがDebianパッケージ形式であることを示しています。
  • arch=$(dpkg --print-architecture)
    • システムのアーキテクチャを指定しています。
    • 「$(dpkg --print-architecture)」では、システムのアーキテクチャに合わせたパッケージを適用するために、現在のアーキテクチャを取得しています(amd64など)
  • signed-by=/usr/share/keyrings/docker-archive-keyring.gpg
    • Dockerリポジトリが先ほどダウンロードしたGPGキーによって署名されていることを示しています。
  • https://download.docker.com/linux/ubuntu
    • DockerリポジトリのURLです。
  • $(lsb_release -cs)
    • 現在のUbuntuバージョンを取得して、リポジトリのURLに適用しています。
  • tee /etc/apt/sources.list.d/docker.list
    • このコマンドの前にある「|(パイプ)」によって、echoコマンドの出力(Dockerリポジトリの情報)をteeコマンドに渡しています。
    • teeコマンドにより、Dockerリポジトリの情報を「docker.list」ファイルに書き込んできます。
  • > /dev/null
    • 「>(リダイレクト)」を使って、コマンドの標準出力を「/dev/null」にリダイレクトしています。これによって、teeコマンドがコンソールに出力する内容が表示されなくなります。

補足

このコマンドでは、安定版(stable)のリポジトリを追加しています。最新版(nightly)やテスト版(test)の各リポジトリを追加する場合は、コマンド内のstableの文字に続けてnightlyやtestの文字を加えてください。

【参考情報】上記コマンドの実行結果

echo \
>   "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
>   https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
>   sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

APTが参照するリポジトリのパッケージリストを再度更新する

Dockerのリポジトリ情報を「/etc/apt/sources.list.d/docker.list」ファイルに書き込んだので、次のコマンドを入力して、APT(パッケージ管理システム)が参照するリポジトリのパッケージリストを再度更新します。

sudo apt-get update

このコマンドを入力すると、「/var/lib/apt」フォルダにDockerのリポジトリ情報が追加・更新されるので、「apt-get install」コマンドでDockerパッケージをインストールすることができるようになります。

補足

「apt-get updateコマンド」はリポジトリを追加した時や削除した時には必ず実行しましょう。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ sudo apt-get update
Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:4 https://download.docker.com/linux/ubuntu jammy InRelease [48.9 kB]
Get:5 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages [15.5 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [108 kB]
Fetched 401 kB in 5s (84.3 kB/s)
Reading package lists... Done

Dockerの関連パッケージをインストールする

次のコマンドを入力して、Dockerの関連パッケージ(docker-ce, docker-ce-cli, containerd.io, docker-compose-plugin)をインストールします。

sudo apt-get install \
    docker-ce \
    docker-ce-cli \
    containerd.io \
    docker-compose-plugin

このコマンドを実行すると、APTは「/var/lib/apt」フォルダにあるパッケージ情報から上記のパッケージを検索して、インストールします。また、必要に応じて依存パッケージも自動的にインストールしています。

インストールしているパッケージについて

  • docker-ce
    • Docker Engineです。
    • Dockerの古いバージョンでは「docker」や「docker-engine」と呼ばれていました。
  • docker-ce-cli
    • 「docker run」や「docker build」などのDockerコマンドを実行するコマンドラインツールです。
  • containerd.io
    • Docker Engineで使用されるコンテナランタイムです。
    • Dockerイメージのダウンロードや管理やDockerコンテナのライフサイクル管理(開始、停止、再起動など)等、コンテナの実行に必要な機能を提供しています。
  • docker-compose-plugin
    • Docker Composeは、複数のDockerコンテナを定義し、管理・実行するためのツールです。あらかじめ用意した定義ファイル(compose.yml)を使用して、複数のコンテナを一括で作成・起動することができます(Docker だけでは、1度に1つずつのコンテナしか操作できません)。
    • docker-compose-pluginパッケージは、Docker Composeの機能をDocker CLIに統合するためのプラグインです。これにより、Docker CLIで直接Docker Composeの機能を利用できるようになります。

【参考情報】上記コマンドの実行結果

user01@DESKTOP-NBUADBQ:~$ sudo apt-get install \
>     docker-ce \
>     docker-ce-cli \
>     containerd.io \
>     docker-compose-plugin
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  dbus-user-session docker-buildx-plugin docker-ce-rootless-extras libltdl7 libslirp0 pigz slirp4netns
Suggested packages:
  aufs-tools cgroupfs-mount | cgroup-lite
The following NEW packages will be installed:
  containerd.io dbus-user-session docker-buildx-plugin docker-ce docker-ce-cli docker-ce-rootless-extras docker-compose-plugin libltdl7 libslirp0 pigz slirp4netns
0 upgraded, 11 newly installed, 0 to remove and 76 not upgraded.
Need to get 109 MB of archives.
After this operation, 396 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 pigz amd64 2.6-1 [63.6 kB]
(省略)
Get:11 https://download.docker.com/linux/ubuntu jammy/stable amd64 docker-compose-plugin amd64 2.17.2-1~ubuntu.22.04~jammy [10.9 MB]
Fetched 109 MB in 4s (28.6 MB/s)
Selecting previously unselected package pigz.
(Reading database ... 24116 files and directories currently installed.)
Preparing to unpack .../00-pigz_2.6-1_amd64.deb ...
Unpacking pigz (2.6-1) ...
Selecting previously unselected package containerd.io.
Preparing to unpack .../01-containerd.io_1.6.20-1_amd64.deb ...
Unpacking containerd.io (1.6.20-1) ...
Selecting previously unselected package dbus-user-session.
Preparing to unpack .../02-dbus-user-session_1.12.20-2ubuntu4.1_amd64.deb ...
Unpacking dbus-user-session (1.12.20-2ubuntu4.1) ...
Selecting previously unselected package docker-buildx-plugin.
Preparing to unpack .../03-docker-buildx-plugin_0.10.4-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-buildx-plugin (0.10.4-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../04-docker-ce-cli_5%3a23.0.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce-cli (5:23.0.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../05-docker-ce_5%3a23.0.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce (5:23.0.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-ce-rootless-extras.
Preparing to unpack .../06-docker-ce-rootless-extras_5%3a23.0.3-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-ce-rootless-extras (5:23.0.3-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package docker-compose-plugin.
Preparing to unpack .../07-docker-compose-plugin_2.17.2-1~ubuntu.22.04~jammy_amd64.deb ...
Unpacking docker-compose-plugin (2.17.2-1~ubuntu.22.04~jammy) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../08-libltdl7_2.4.6-15build2_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-15build2) ...
Selecting previously unselected package libslirp0:amd64.
Preparing to unpack .../09-libslirp0_4.6.1-1build1_amd64.deb ...
Unpacking libslirp0:amd64 (4.6.1-1build1) ...
Selecting previously unselected package slirp4netns.
Preparing to unpack .../10-slirp4netns_1.0.1-2_amd64.deb ...
Unpacking slirp4netns (1.0.1-2) ...
Setting up dbus-user-session (1.12.20-2ubuntu4.1) ...
Setting up docker-buildx-plugin (0.10.4-1~ubuntu.22.04~jammy) ...
Setting up containerd.io (1.6.20-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /lib/systemd/system/containerd.service.
Setting up docker-compose-plugin (2.17.2-1~ubuntu.22.04~jammy) ...
Setting up libltdl7:amd64 (2.4.6-15build2) ...
Setting up docker-ce-cli (5:23.0.3-1~ubuntu.22.04~jammy) ...
Setting up libslirp0:amd64 (4.6.1-1build1) ...
Setting up pigz (2.6-1) ...
Setting up docker-ce-rootless-extras (5:23.0.3-1~ubuntu.22.04~jammy) ...
Setting up slirp4netns (1.0.1-2) ...
Setting up docker-ce (5:23.0.3-1~ubuntu.22.04~jammy) ...
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.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

これで、WSL2上のUbuntuにDockerをインストールすることができました。

次に、下記の方法について順番に説明します。

  • sudo権限なしでDockerコマンドを実行する方法
  • Dockerが正常にインストールされたことを確認する方法
  • Docker Composeプラグインが正常にインストールされたか確認する方法

sudo権限なしでDockerコマンドを実行する方法

現時点では、Dockerを実行するためにはsudo権限が必要になります。sudoなしで「docker versionコマンド」を実行すると、権限拒否されてしまいます。

sudoなしでもdockerコマンドを実行できるように、以下のコマンドを入力して、Linuxユーザーをdockerグループに追加します。

sudo usermod -aG docker $USER

この設定は、WSL2のUbuntuからログアウト・ログインすることで有効になります。ログアウトするにはWSL2上のUbuntuのターミナルで「exitコマンド」を実行します。その後、WSL2のUbuntuをもう一度起動させます。

再起動後、sudoなしで「docker versionコマンド」を実行すると、以下のように表示され、sodoなしでも実行できていることが分かります。

user01@DESKTOP-NBUADBQ:~$ docker version
Client: Docker Engine - Community
 Version:           23.0.3
 API version:       1.42
 Go version:        go1.19.7
 Git commit:        3e7cbfd
 Built:             Tue Apr  4 22:05:48 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.3
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.7
  Git commit:       59118bf
  Built:            Tue Apr  4 22:05:48 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.20
  GitCommit:        2806fc1057397dbaeefbea0e4e17bddfbd388f38
 runc:
  Version:          1.1.5
  GitCommit:        v1.1.5-0-gf19387a
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Dockerが正常にインストールされたことを確認する方法

以下のコマンドを入力して、Dockerが正常にダウンロードされたかを確認します。

docker run hello-world

このコマンドを実行して、以下のような出力が出たら(「Hello from Docker!」が出たら)、Dockerが正常にインストールされています。

user01@DESKTOP-NBUADBQ:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:4e83453afed1b4fa1a3500525091dbfca6ce1e66903fd4c01ff015dbcb1ba33e
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/

補足

コマンドでは、Docker Hubからhello-worldのイメージを取得して、dockerコンテナを実行しています。

「docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.」が表示された場合

user01@DESKTOP-NBUADBQ:~$ 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: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.」が表示された場合には、以下のコマンドを実行して、Dockerを起動してください。

sudo service docker start

上記のコマンドでDockerを起動した後に、再度「docker run hello-world」コマンドを入力してください。

Docker Composeが正常にインストールされたことを確認する方法

以下のコマンドを入力して、Docker Composeプラグインが正常にインストールされたか確認しましょう。

docker compose version

このコマンドを実行して、以下のように、Docker Composeのバージョンが出力されれば、Docker Composeプラグインが正常にインストールされています。

user01@DESKTOP-NBUADBQ:~$ docker compose version
Docker Compose version v2.17.2

本記事のまとめ

この記事では『Docker』について、以下の内容を説明しました。

  • WSL2上のUbuntuにDockerをインストールする方法
  • sudo権限なしでDockerコマンドを実行する方法
  • Dockerが正常にインストールされたことを確認する方法
  • Docker Composeプラグインが正常にインストールされたことを確認する方法

お読み頂きありがとうございました。