アプリ開発備忘録

PlayStationMobile、Android、UWPの開発備忘録

WSL2でDocker Server(Engine)を立ち上げて、Windows側から使用する

Ubuntu-22.04 を使用します。今回distribution名はdockerにしました。

Dockerをインストール

まずはDockerの公式ガイドに従い、Docker Engineをインストールします。
https://docs.docker.com/engine/install/ubuntu/

起動時の設定

Boot時にsystemdを起動するようにします。
/etc/wsl.conf

[boot]
command=/usr/libexec/wsl-systemd

dockerグループにユーザーを追加します。

sudo usermod -a -G docker <username>

Docker daemon socketを公開する

デフォルトの設定を確認する

/lib/systemd/system/docker.service を見ます。

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

ここを見ます。この設定を上書きします。

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

設定を上書きする

systemdの設定を上書きするためのdrop-in directoryを作成しunitファイルを作成します。

sudo mkdir /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/start.conf

任意のポートで外部に公開します。
ExecStart= で空を指定しているのは、ここに指定すると上書きではなく元のファイルに追加にってしまうためです。空を指定する事で元の設定をリセットする事ができます。
/etc/systemd/system/docker.service.d/start.conf

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375 --containerd=/run/containerd/containerd.sock

https://docs.docker.com/engine/install/linux-postinstall/#configuring-remote-access-with-systemd-unit-file

ここまでできたら、保存していつでも再度入れられるようにしておきましょう。

wsl --export docker Docker.tar

Windows側で接続する

Chocolatey

Chocolatey Package Managerでdocker cliを入れます。

まずはChocolateyを公式ドキュメントを見てインストールします。
https://docs.chocolatey.org/en-us/choco/setup#install-with-powershell.exe

docker-cli をインストールします。

choco install docker-cli

この状態で docker version コマンドを打つと、Clientだけが表示されるはずです。

Client:
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:09:02 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true
error during connect: This error may indicate that the docker daemon is not running.: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/version": open //./pipe/docker_engine: The system cannot find the file specified.

接続設定

docker用のディストリビューションは起動しておいてください。
DOCKER_HOST 環境変数を設定します。

DOCKER_HOST=tcp://localhost:2375

この状態で docker version コマンドを打つと、ClientとServerが表示されるはずです。

Client:
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:09:02 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:00:51 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad

おわり

これでWindows側からDockerが使用できます。