azukipochette's weblog

memory dump (mini)

Docker on VMWare Workstation と WSL で快適生活

前回の続編です。

今回は、WSL (Windows Subsystem for Linux) から VMWare Worksation 上で動作する Docker-machine を操作する方法です。 なお、WSL 上で動作する Linux ディストリビューションは複数ありますが、私は、最近リリースされた Ubuntu 18.04 を使ってます。

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

Ubuntu に対して Docker をインストールします。

Docker のインストール方法は公式に手順があるので、素直に従います。

apt-get install で docker-ce が見つからないと言われたら...

ちゃんとリポジトリを追加したのに docker-ce がないといわれるぞ!言われた場合は、もう少しちゃんとマニュアルを読みましょう。

現時点で Ubuntu 18.04 向けの docker-ce は stable には無いので、edge か test にする必要があります。test はちょっとアレなので、edge ぐらいがいいのではないでしょうか。 というわけで、以下のコマンドを代わりに実行します。

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

.bashrc を変更する

~/.bashrc に以下の行を追加します。

MACHINE_IP=`'/mnt/c/Program Files/Docker Toolbox/docker-machine.exe' ls 2>/dev/null | grep default | awk '{ print $5}'`

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="$MACHINE_IP"
export DOCKER_CERT_PATH="/mnt/c/Users/$USER/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"

なお、docker-machine コマンドで作成する環境の名前は default を想定しています。 別の名前にしている場合は、grep の引数と DOCKER_MACHINE_NAME の値を適宜変更してください。

内容は読むとわかりますが、docker-machine コマンドを実行して、出力結果から Grep で default 環境の行を取り出し、さらに IP アドレス部分を AWK で取り出して変数を作成、それを DOCKER_HOST 環境変数に設定することにより、接続できるようにしています。これで、毎回環境変数を更新する必要はありませんね (ヤッタ!)。

更新後は、ターミナルを再起動するか、以下のコマンドを実行して .bashrc を再読み込みします。

source ~/.bashrc

設定がうまく動いているかどうかを確認する

bash 上で以下のコマンドを実行します

docker version

以下のように出力結果に Server が表示されていれば、あとは普通に docker コマンドが使えます。

Client:
 Version:      18.05.0-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:16:13 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.05.0-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.10.1
  Git commit:   f150324
  Built:        Wed May  9 22:20:42 2018
  OS/Arch:      linux/amd64
  Experimental: false

試しに hello-world コンテナでも実行してみます。

docker run hello-world

以下のような出力が得られれば成功です。

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/engine/userguide/

それでは、良い Docker on VMWare workstation 生活を。Enjoy!