Muji Blog

いろんなトピックを備忘録的に発信していきます。トピック例:{画像処理、信号処理、心理学}

WSLでdockerの環境構築

はじめに

dockerをwslから動かそうと試み、丸一日溶けたので記録しておきます。

また、わざと"WSL"と書きましたが、前提条件は以下です。

  • Windows OS ビルド 19041以上
  • WSL2であること
  • 管理者権限でコマンドプロンプトを立ち上げていること
  • docker のバージョンが17.12.1であること
  • docker hub にアカウント登録していること

上記の通り、実際はWSL2でないといけないようです。
ですが、WSLで1日頑張り時間を費やしてしまいましたし、ほかにもWSLで頑張ってググり、困っている方がいるかと思うので、わざとタイトルはこうしました。

ちなみに、WSL1でも最後のhello-worldまでは成功します。
そのあと、apt-getを使ってパッケージを入れようとすると全く動かなくなります。

step1: windowsのアップデート

以下のページからアップデートできます。
www.microsoft.com

step2: WSL2への変更

以下の記事を参考に進めました。
dev.classmethod.jp
ちなみに、私はすでにWSLでUbuntuが入っていたため、新しくUbuntuをインストールすることなく以下を実行して、すでにあるUbuntuをWSL2で動作させるようにしました。

wsl --set-version Ubuntu 1

step3: dockerのインストール

wslを立ち上げ、以下を実行します。

sudo apt install docker.io=17.12.1-0ubuntu1

さらに、ユーザーのグループにdockerをセカンダリグループとして追加します。

sudo usermod -aG docker $USER

docker daemonの起動

以下を実行します。

sudo cgroupfs-mount && sudo service docker start

以下のコマンドで、起動していることを確認しましょう

sudo service docker status

以下のように表示されていればOKです。

*docker running

以下を実行して、うまく起動しているかを確認しましょう。

sudo docker run hello-world

以下のように表示されていればOKです。

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/