Docker Machine を使って DigitalOcean に Dockerホストを立てる


## 概要
Docker公式オーケストレーションツールの 1 つ Docker Machine を使って DigitalOcean に Docker がインストールされた Droplet を作成し、`hello world` を出力するコンテナを走らせてみるという話

## 必要なもの

* Access Tokens (Writable) 


![_2015-02-28_12_25_33](https://cloud.githubusercontent.com/assets/261700/6424642/0a2174de-bf45-11e4-990d-531b1004b0b0.png)

![_2015-02-28_12_26_53](https://cloud.githubusercontent.com/assets/261700/6424647/38862af4-bf45-11e4-8e27-1330c50a0c6d.png)

![_2015-02-28_12_27_44](https://cloud.githubusercontent.com/assets/261700/6424653/49b849ec-bf45-11e4-8182-e3eba201ae7b.png)

![_2015-02-28_12_28_34](https://cloud.githubusercontent.com/assets/261700/6424661/6dce24f0-bf45-11e4-86d4-0c94d1d3b14f.png)

## Hello World

`docker-machine create` コマンドで Dockerホストを作成する。

（token はダミー）

```bash
$ docker-machine create \
    --driver digitalocean \
    --digitalocean-access-token 
    f851029140a409712047b22d4c2696277af113d02feebf5477fb27 \
    staging
INFO[0000] Creating SSH key...
INFO[0002] Creating Digital Ocean droplet...
INFO[0005] Waiting for SSH...
INFO[0074] Configuring Machine...
INFO[0141] "staging" has been created and is now the active machine.
INFO[0141] To point your Docker client at it, run this in your shell: $(docker-machine env staging)
```

マシンリストを見てみる

```
$ docker-machine ls
NAME      ACTIVE   DRIVER         STATE     URL                          SWARM
dev                virtualbox     Running   tcp://192.168.99.100:2376
staging   *        digitalocean   Running   tcp://104.236.194.241:2376
```

SSH ログイン

```
$ docker-machine ssh staging
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-43-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Feb 27 22:33:28 EST 2015

  System load:  0.25               Processes:              70
  Usage of /:   11.4% of 19.56GB   Users logged in:        0
  Memory usage: 12%                IP address for eth0:    104.236.194.241
  Swap usage:   0%                 IP address for docker0: 172.17.42.1

  Graph this data and manage this system at:
    https://landscape.canonical.com/

root@staging:~#
```

SSH 経由でのコマンド実行

```
$ docker-machine ssh staging "hostname && uname -a"
staging
Linux staging 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
```

マシンのストップ

```
$ docker-machine stop staging
$ docker-machine ls
NAME      ACTIVE   DRIVER         STATE     URL                          SWARM
dev                virtualbox     Running   tcp://192.168.99.100:2376
staging   *        digitalocean   Stopped   tcp://104.236.194.241:2376
# DO 上では => Droplet が Status Off になっている
```

マシンのスタート

```
$ docker-machine start staging
$ docker-machine ls
NAME      ACTIVE   DRIVER         STATE     URL                          SWARM
dev                virtualbox     Running   tcp://192.168.99.100:2376
staging   *        digitalocean   Running   tcp://104.236.194.241:2376
```

Hello World

```
$ docker $(docker-machine config staging) run busybox echo hello world
Unable to find image 'busybox' locally
511136ea3c5a: Pull complete
df7546f9f060: Pull complete
ea13149945cb: Pull complete
4986bf8c1536: Pull complete
busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Status: Downloaded newer image for busybox:latest
hello world
```

```
$ docker $(docker-machine config staging) ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                     PORTS               NAMES
fd02ad4af931        busybox:latest      "echo hello world"   8 seconds ago       Exited (0) 6 seconds ago                       reverent_pasteur
```

マシンの削除

```
$ docker-machine rm staging
$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev             virtualbox   Running   tcp://192.168.99.100:2376
```

## REF

* [Docker Machine 公式ドキュメント](https://docs.docker.com/machine/)
* [Docker Machine リファレンス](http://qiita.com/spesnova/items/073dd64a8a1d580d9ae9)