takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

WSL2 Ubuntu 22.04 に Docker をインストール

検証環境

Windows11 Home Edition (version 22H2)

D:\>wsl --version
WSL バージョン: 1.2.5.0
カーネル バージョン: 5.15.90.1
WSLg バージョン: 1.0.51
MSRDC バージョン: 1.2.3770
Direct3D バージョン: 1.608.2-61064218
DXCore バージョン: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows バージョン: 10.0.22621.1555

# installed ubuntu version
Ubuntu 22.04.2 LTS (Jammy Jellyfish)

古いバージョンのアンインストール

Docker がインストール済みの場合、古いバージョンをアンインストールする

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

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

$ sudo apt-get update -y

$ sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg

$ sudo install -m 0755 -d /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ sudo chmod a+r /etc/apt/keyrings/docker.gpg

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

Docker エンジンのインストール

$ sudo apt-get update -y

$ sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

ユーザーのグループ追加

sudo なしで docker コマンドが実行できるようにユーザーを docker グループへ追加する

$ sudo gpasswd -a <username> docker

ログアウト後、再ログインすると設定が反映される

config ファイルの作成

$ sudo mkdir -p /etc/systemd/system/docker.service.d
$ sudo touch /etc/systemd/system/docker.service.d/options.conf
$ sudo echo '[Service]' | sudo tee /etc/systemd/system/docker.service.d/options.conf
$ sudo echo 'ExecStart=' | sudo tee --append /etc/systemd/system/docker.service.d/options.conf
$ sudo echo 'ExecStart=/usr/bin/dockerd -H unix://' | sudo tee --append /etc/systemd/system/docker.service.d/options.conf

$ sudo touch /etc/docker/daemon.json
$ sudo echo '{"log-driver":"json-file","log-opts":{"max-size":"10m","max-file":"3"}}' | sudo tee /etc/docker/daemon.json
$ sudo chmod 600 /etc/docker/daemon.json

docker デーモンのリスタート

config ファイルを反映させるため docker デーモンをリスタートさせる

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

動作確認

$ docker -v
Docker version 23.0.4, build f480fb1

$ 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 images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   19 months ago   13.3kB

参考サイト

docs.docker.com

takaya030.hatenablog.com