WSL上でDockerが動くという噂

最近ちょっとDockerに興味がある。
インフラエンジニアとしてはDockerのおかげで商売上がったりだ!
という人もいるかもしれないが、あまりに簡単で楽ちんだったので、
コンテナ型の仮想化ってのも悪くないなという印象だ。

1サーバを1プロセスとして簡単に立てられ、
いらなくなったら停止してポイっと捨てられるのも手間がかからなくて便利。
プログラマの人たちは同じ環境で開発できるので重宝しているようだ。

そんなDocker。
LinuxやMacではターミナルがあるからそのままゴニョゴニョやれば良いのだが、
WindowsではHyper-Vが必要なため、自動的にHomeエディションではNGとなる。

別にProを持っていれば問題はないし、無くてもアップグレードすれば問題ない。
だがしかし、そういう制約があるとやっぱり嫌なのである。
なんとか回避策がないかとあれこれ調べていた。

ゲストOSとしてLinuxをぶち込むのが手っ取り早くはあるが、
起動までの時間と初期セットアップの手間を考えるともう少し楽に触りたい。

そんな中、WSLにて動くという噂を聞きつけたので試してみた。


WSLにDockerをインストールする


今回の「WSLにてDockerを動かす」というのは、
Windows本体にDockerをインストールせずに、WSL上だけで動かすという意味合いであり、
「WindowsにインストールしたDockerをWLS上から操作する」という意味ではないので注意。


さっそく始めていきたいと思うが、前提として使用するWLSのUbuntuは
昔のいわゆるBash on Ubuntu on Linuxではなく、
Windowsストアからダウンロードした新しいWLSの方である。
Winsowsのバージョンも前回無事にアップデート完了したので、
最新版であるWindows10のバージョン1809だ。

やり方は公式ページのUbuntuへのインストール方法を参考にした。
古いバージョンが入っている場合は消せと書かれているが、特に無いのでそこはスキップ。

まずはアプデから実施。

 $ sudo apt-get update


次に必要なパッケージをインストール。

 $ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common


次にDocker公式のGPGキーを追加する。

 $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


鍵の追加後はフィンガープリントを確認しろとあるので、指示に従う。
公式ページに掲載されているものと同一であれば問題なし。

 $ sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ 不明 ] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]


次は環境によって左右されるが、この場合は"x86_64 / amd64"を参考にすれば良さそうだ。

 $ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"



ここまできたらやっとDockerのインストールに入る。
※結果から言うとUbuntuでのインストール方法ではWSLでは動かなかったが、
 Ubuntuでやることもあるかもしれないのでそのまま載せておく。WSLの対処は後半で。


まずはやはりアプデから実施。

 $ sudo apt-get update


次にDockerをインストールするのだが、そのままやれば最新版になる。

 $ sudo apt-get install docker-ce docker-ce-cli containerd.io


インストール完了後、バージョンの確認。

 $ sudo docker version
Client:
Version: 18.09.3
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:53:11 2019
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?


「Cannot connect to the Docker daemon」??
起動していないのかな?

 $ sudo service docker status
* Docker is not running


動いていないようなので、起動させてみる。

 $ sudo service docker start
* Starting Docker: docker


再度確認。

 $ sudo service docker status
* Docker is not running


なぜ!!!

色々調べてみると、WSLを管理者権限で起動させないとだめらしい。
一度閉じて管理者権限で起動後再度、service startさせてみた結果のステータス。

 $ sudo service docker status
* Docker is running


やった!runningになったぞ!
再度バージョンを確認してみる。

 $ sudo docker version
Client:
Version: 18.09.3
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:53:11 2019
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 18.09.3
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 05:59:55 2019
OS/Arch: linux/amd64
Experimental: false


よしよし、Serverまでちゃんと見えたな。
いざhello worldを試してみよう!

 $ sudo docker run hello-world
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:297: getting the final child's pid from pipe caused \"EOF\"": unknown.
ERRO[0007] error waiting for container: context canceled


なぜ!!!!!

謎なエラーが出て上手くいかない・・・。
念の為、コンテナを確認してみた。

 $ sudo docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e98af1d49028 hello-world "/hello" 3 minutes ago Created lucid_goodall
388abb7fdbbd hello-world "/hello" 4 minutes ago Created friendly_gates


お?Createdまでは進んでいるな。
その後の処理ができなかったのか。

暫く調査をしていると最新版のDockerはWSLでは動かないとか。
まじかよ・・・。


Dockerバージョン17.09.0で試す


調査を進めていると、バージョン17.09.0ならWSLでも動かせるらしい情報をゲット。
先駆者の検証に感謝しつつ、以下サイトを参考にさせてもらった。
ありがとうございます!

参考:どうしても Docker on Ubuntu 18.04 on WSL したかった


では例にならって進めていく。
まずは忘れないうちに管理者権限でWSLを起動させておく。

次に最新バージョンをアンインストールしておく。
最新が入っていると旧バージョンを入れた際にエラーになるので予め削除。

 $ sudo apt-get remove docker-ce docker-ce-cli containerd.io
$ sudo apt-get autoremove


無事アンインストールが終わったらバージョン17.09.0をWSL上にダウンロードする。

 $ curl -O https://download.docker.com/linux/debian/dists/stretch/pool/stable/amd64/docker-ce_17.09.0~ce-0~debian_amd64.deb


ダウンロードが完了したらそのままインストール。

 $ sudo dpkg -i docker-ce_17.09.0\~ce-0\~debian_amd64.deb


インストール完了後、確認するとやっぱり動いていないので起動させる。

 $ sudo service docker status
* Docker is not running
$
$ sudo service docker start
* Starting Docker: docker
$
$ sudo service docker status
* Docker is running


起動後、バージョン確認。

 $ sudo docker version
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:42:09 2017
OS/Arch: linux/amd64

Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:40:48 2017
OS/Arch: linux/amd64
Experimental: false


おお、これなら上手くいきそうな予感。
恐る恐る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/


やったー!!!無事に起動できたぞ!!!

念の為alpineも起動させてみる。

 $ sudo docker run -it alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
8e402f1a9c57: Pull complete
Digest: sha256:644fcb1a676b5165371437feaa922943aaf7afcfa8bfee4472f6860aad1ef2a0
Status: Downloaded newer image for alpine:latest
/ #
/ # cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.9.2
PRETTY_NAME="Alpine Linux v3.9"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
/ #
/ # exit


こちらも問題なし!

今後問題は出てくるかもしれないが、とりあえず動いたのでOKとする。


今回は以上で。



ITエンジニアになる!
チャレンジDocker仮想化環境構築入門


人気記事