7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ubuntu20.04@wsl2でDockerを使えるようにする(WindowsのDockerDesktopを使いたくない)

Last updated at Posted at 2021-11-29

WindowsでDockerが使いたいけど、Docker Desktopは使いたくない

Ubuntu20.04@wsl2でDocker利用したい(Windows上のDockerDesktopを使いたくない)のでDocker EngineをUbuntu20.04@wsl2にインストールします。

お役に立てたら、LGTMよろしくお願いします。

Ubuntu20.04@wsl2でsystemd(systemctl)を自動起動する

こちらの記事を参考にsystemdをPID1で起動できるようにする
実は↑が一番重要。あとは、公式を参考に実施すれば比較的すんなりいきます。

Repositoryをセットアップ

$ sudo apt-get update
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$ echo \
  "deb [arch=amd64 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
# amd64のところはマニュアルだと$(dpkg --print-architecture)。うまくいかなかったので直接amd64としていするとなぜか行けた。

Docker Engineをインストール

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
# ちなみにPID1でsystemdが立ちあがっていないとここでインストールできない

インストールは完了したので、helloしてみる

docker run hello-world
# 実行結果
docker: Got permission denied while trying to connect to the Docker daemon socket…

実行権限がないと怒られる。

作業中のユーザーに権限を与える

公式を参考に、

$ sudo gpasswd -a $(whoami) docker
$ sudo chgrp docker /var/run/docker.sock
$ sudo service docker restart

一旦Ubuntuからログアウトしてwslを再起動(wsl --shutdown)して、もう一度Ubuntuにログイン。
その上で再度helloしてみる

$ docker run hello-world
# 実行結果。中段に下記が出ていればOK
Hello from Docker!
This message shows that your installation appears to be working correctly.

以降は自動でDockerも立ち上がるようになっており、
これでUbuntu20.04@wsl2上でDockerが使えるようになります。

7
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?