Docker Machine を使って VirtualBox に Dockerホストを立てる（２０１６年５月版）

Docker-machineのインストールメモ

ローカルPC（Mac版）のVirtualBoxでDocker-machineを動かすために色々調べました。
過去のQIITAの記事の最新版としての意味もあります。

[Docker Machine を使って VirtualBox に Dockerホストを立てる](http://qiita.com/spesnova/items/038af6a8a4e401e3d2aa)

#0.前提
ローカルPC（MAC版）には、事前にDockerを入れておきます。

参照先：
https://www.docker.com/products/docker-toolbox

#1.Docker-Machineをインストール
同ローカルPCに、Docker-Machineをインストール
VirtualBox上のゲストOSに入れません。

参照先：
・インストール方法の説明箇所
https://docs.docker.com/machine/#osx-and-linux
・Docker-Machineのファイル置き場所（最新場所の確認）
https://github.com/docker/machine/releases/
（執筆時は最新が０．７版）

```
$ curl -L https://github.com/docker/machine/releases/download/v0.7.0/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine  
$ chmod +x /usr/local/bin/docker-machine

$ docker-machine --version
docker-machine version 0.7.0, build a650a40
$ 
```

#2.VirtualBox上にDockerホストを立てて、起動

```
$ docker-machine create --driver virtualbox dev-test
Running pre-create checks...
(dev-test) Image cache directory does not exist, creating it at /Users/qiita/.docker/machine/cache...
(dev-test) No default Boot2Docker ISO found locally, downloading the latest release...
(dev-test) Latest release for github.com/boot2docker/boot2docker is v1.11.1
(dev-test) Downloading /Users/qiita/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.11.1/boot2docker.iso...
(dev-test) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(dev-test) Copying /Users/qiita/.docker/machine/cache/boot2docker.iso to /Users/qiita/.docker/machine/machines/dev-test/boot2docker.iso...
(dev-test) Creating VirtualBox VM...
(dev-test) Creating SSH key...
(dev-test) Starting the VM...
(dev-test) Check network to re-create if needed...
(dev-test) Found a new host-only adapter: "vboxnet2"
(dev-test) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env dev-test
$ 
```

```
$ docker-machine ls
NAME       ACTIVE   DRIVER         STATE     URL                         SWARM   DOCKER    ERRORS
dev-test   -        virtualbox     Running   tcp://192.168.99.100:2376           v1.11.1   
$ 
```

#3.VirtualBoxのDockerホストとDockerコンテナの確認

起動の前に、環境変数まわりを確認

```
$ docker-machine env dev-test
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/qiita/.docker/machine/machines/dev-test"
export DOCKER_MACHINE_NAME="dev-test"
# Run this command to configure your shell: 
# eval $(docker-machine env dev-test)
$ 
```

オプション形式もあります

```
$ docker-machine config dev-test
--tlsverify
--tlscacert="/Users/qiita/.docker/machine/certs/ca.pem"
--tlscert="/Users/qiita/.docker/machine/certs/cert.pem"
--tlskey="/Users/qiita/.docker/machine/certs/key.pem"
-H=tcp://192.168.99.100:2376
$ 
```

この形式を利用して

```
$ docker $(docker-machine config dev-test) ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$ 
```

VirtualBox上で稼働したVM（dev-test）上のDockerコンテナの稼働状況を確認できます。

そこで、VirtualBox上のコンテナにHelloWorldを起動させます。

```
$ docker $(docker-machine config dev-test) run busybox echo hello world
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
385e281300cc: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:4a887a2326ec9e0fa90cce7b4764b0e627b5d6afcb81a3f73c85dc29cea00048
Status: Downloaded newer image for busybox:latest
hello world
$
```

起動した結果を確認します

```
$ docker $(docker-machine config dev-test) ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
2d06e005e871        busybox             "echo hello world"   14 seconds ago      Exited (0) 14 seconds ago                       kickass_easley
$ 
```