DEV Community

Borikatsu
Borikatsu

Posted on • Edited on

Docker Desctop使わずにWSL2でDockerを使う

Docker Desktop を利用するには支払いが必要ですか

個人利用なら Docker Desktop は無料で利用できるが、ビジネス目的での利用だと少し制限がある。

別に個人で利用している分には Docker Desctop を使い続ければいいのだが、使わずに WSL 上の Ubuntu にインストールして動かすこともできるらしかったので試してみた。
(いつか個人利用でも有償になった時のために)


目次


さっそくインストールしてみよう

Install Docker Engine on Ubuntu
↑ Docker が公式で出している Ubuntu OS に Docker Engine をインストールする方法。
書いてある通りに進めていけば Docker 動くようになるとのこと。めちゃ簡単。

Install using the Apt repository
↑ インストールするための準備コマンドが記載されている項目。

若干ライブラリが足りてなかったりするので、
https://dev.to/felipecrs/simply-run-docker-on-wsl2-3o8
を参考に以下のコマンドを実施。

# Add Docker's official GPG key:
$ sudo apt-get update
$ sudo apt-get install ca-certificates curl gnupg lsb-release
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

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

$ sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

そして、最新版で問題ないので以下のコマンドでインストール。

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

ちなみにバージョン指定してインストールする方法は以下。

## バージョン情報の確認
$ apt-cache madison docker-ce | awk '{ print $3 }'
5:24.0.0-1~ubuntu.22.04~jammy
5:23.0.6-1~ubuntu.22.04~jammy
...

## インストールしたいバージョンを指定してインストール
$ VERSION_STRING=5:24.0.0-1~ubuntu.22.04~jammy
$ sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

起動設定

まず Docker のデーモンを立ち上げる上での管理グループ & ユーザーを作成する。
※Docker Desctop をインストールしたことがあるなら既に作成されているはずなのでこの手順は不要。

# Ensures docker group exists
$ sudo groupadd docker

# Ensures you are part of it
$ sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

Ubuntu にログインするとデーモンが立ち上がるように bash に設定を追記

$ vi ~/.profile
if service docker status 2>&1 | grep -q "is not running"; then
    wsl.exe -d "${WSL_DISTRO_NAME}" -u root -e /usr/sbin/service docker start >/dev/null 2>&1
fi
Enter fullscreen mode Exit fullscreen mode

できあがり

$ /usr/sbin/service docker status
 * Docker is running

$ docker --version
Docker version 24.0.6, build ed223bc

$ docker compose version
Docker Compose version v2.21.0
Enter fullscreen mode Exit fullscreen mode

Ubuntu にログインして、Docker デーモンが起動していること、バージョン情報が表示されるので正常インストールされていることが確認できた。

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay