Windows Subsystem for LinuxでDockerの環境を作る

2019/01/11

Windows10にDockerの環境を作りたかったのですが、Docker for WindowsはHomeエディションでは動かせないし、Docker ToolboxはVirtualBoxを使うので起動が遅い。以前にVagrantを使って環境作ったこともありますが、今となってはDocker Toolboxの下位互換的な感じがします。

ということで第三の選択肢であるDocker on WSLを試してみました。

ネットを見たら書いてあることがまちまちで混乱。紐といてみた感じ、

  • もともとはWSLでDockerは動かなかった
  • (恐らく)Windows 10 1803 (April 2018 Update) でWSLがバージョンアップしてDockerが動くようになった
  • Docker 17.09.1 でマウントに MS_SLAVE を使うようになったが、これにWSLが対応していなかった
  • Windows 10 1809 (October 2018 Update) でWSLが MS_SLAVE に対応した

ということのようです。

最終的には以下の組み合わせになりました。

  • Windows10 Home 1809 (October 2018 Update)
  • Ubuntu 18.04.1
  • Docker 18.09.1

October 2018 Updateは現時点では自動配信されておらず、手元の環境はApril 2018 Updateのままだったのでバージョンアップするところからスタート。

Windows 10が無事バージョンアップできたら管理者権限でUbuntuを起動します。まずはバージョンを確認。

$ head -n 2 /etc/os-release
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
$

Dockerをインストールします。公式サイトのドキュメントに従えばOK。

$ sudo apt update
(snip)
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
(snip)
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
$  sudo apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
(snip)
Reading package lists... Done
$ sudo apt-get update
(snip)
Reading package lists... Done
$ sudo apt-get install docker-ce
(snip)
$

Dockerのバージョンを確認しておきます。

$ docker -v
Docker version 18.09.1, build 4c52b90
$

Dockerのdeamonを起動。こ こは Ubuntu を管理者権限で起動しておかないとダメっぽい。

$ sudo cgroupfs-mount
$ sudo usermod -aG docker $USER
$ sudo service docker start
 * Starting Docker: docker                                                                                       [ OK ]
$ sudo service docker status
 * Docker is running
$

hello-world イメージの実行を実行してみます。

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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/

$

上手くいったようです。よかった。

ちなみに、試行錯誤しながら手順をまとめたのでやや自信なし...間違いを見つけたらご指摘いただけると嬉しいです。

参考URL




関連(するかもしれない)記事


おススメ