1
1

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

Posted at

記事内容はタイトル通りです。

環境

  • Windows11
  • WSL2
  • Ubuntu
    $ cat /etc/os-release
    PRETTY_NAME="Ubuntu 22.04.3 LTS"
    NAME="Ubuntu"
    VERSION_ID="22.04"
    VERSION="22.04.3 LTS (Jammy Jellyfish)"
    
  • Docker
    $ docker --version
    Docker version 26.1.3, build b72abbb
    

Docker Engineに対応しているのはUbuntu20.04以降のバージョンです。
WSLはデフォルトディストリビューションとしてUbuntuのLTS版を起動するのであまり気にする必要はありません。

前提条件

  • WSL1ではなく、WSL2を利用していること
    • WSL1は完全なLinuxカーネルを利用できないため、Linuxカーネル上で動作するDocker Engineを利用することができません
  • Dockerがインストールされていないこと

インストール手順

Docker Engineをインストールする方法は以下の4つです

  1. Docker Desktop for Linuxにバンドルされているものを使う
  2. Dockerのaptリポジトリからインストールする
  3. 手動でインストールする
  4. 公式が提供しているインストールスクリプトを使う

今回は一番一般的と思われる2.の方法でインストールします。

1. aptリポジトリをセットアップする

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

2. Dockerパッケージ群をインストールする

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • docker-ce: オープンソース版のDocker CE(Communitiy Edition)
  • docker-ce-cli: Dockerのコマンドラインインタフェース. コンテナ作成・起動・削除等を行う
  • containerd.io: Dockerの動作に必要なコンテナランタイム
  • docker-buildx-plugin: Dockerのビルド機能を拡張するためのプラグイン
  • docker-compose-plugin: Docker Composeを使うためのプラグイン

パッケージのインストール後に動作確認をしておきます。

$ docker --version
Docker version 26.1.3, build b72abbb
$ docker run 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/

上のような文面が表示されていれば成功です。

インストール後にやっておきたい設定

rootユーザ以外でdockerコマンドを利用できるようにする

※ rootユーザを使う場合、この設定は不要です

Docker daemonはUnixソケット経由で操作します。Unixソケットはrootユーザが所有者であるため、root以外のユーザはsudoを使ってアクセスする必要があります。

しかし、毎回sudo docker ... と入力し、その度にパスワードを入力するのは面倒です。
この手間を回避するためにはdockerグループを作成し、そのグループにユーザを追加してあげる必要があります。

  1. dockerグループを作成する
    $ sudo groupadd docker
    
  2. dockerグループにユーザを追加する
     sudo usermod -aG docker $USER
    
  3. ターミナルからログアウトする
  4. dockerグループの変更を反映する
    $ newgrp docker
    

これでsudoなしでdockerコマンドを利用できるようになります。

$ docker run hello-world

参考資料

1
1
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
1
1