---
title: Docker Machine + DigitalOceanでサーバー構築
tags: ["DigitalOcean", "Docker", "Docker Machine"]
categories: ["Dev", "Infrastructure", "Docker", "DockerMachine"]
date: 2015-05-03T20:17:15Z
updated: 2015-05-03T20:17:15Z
---

[Docker Machine](https://docs.docker.com/machine/)を使うとローカルのDocker環境構築するのも簡単だったし、クラウドにデプロイするのも超簡単だった。


インストールは[ドキュメント](https://docs.docker.com/machine/#installation)の通り。
Macの場合は

```
($ brew install docker)
$ brew install docker-machine
```

で。docker-machineでセットアップすると、あたかもdockerホストで作業しているかのようにローカルPC上で`docker`コマンドを使える。ホストがBoot2DockerでもAmazon EC2でもGoogle Compute Engineでも透過的に扱える！

一番安価なDigitalOceanの例をメモする。

DigitalOcean側のセットアップは[この記事](http://qiita.com/spesnova/items/c1a8b7c1a079834aed2d)の通り。

イメージ (Dockerホスト)作成。リージョンは一番近いシンガポール(`sgp1`)にしてみた。`do1`はイメージ名。

```
$ docker-machine create -d digitalocean --digitalocean-region sgp1 --digitalocean-access-token (アクセストークン) do1
INFO[0001] Creating SSH key...
INFO[0002] Creating Digital Ocean droplet...
INFO[0146] "do1" has been created and is now the active machine.
INFO[0146] To point your Docker client at it, run this in your shell: eval "$(docker-machine env do1)"
```
メモリサイズはデフォルトで512mbである。変えたいときは`--digitalocean-size 1gb`のように指定する。

`docker-machine ls`で`do1`が起動しているのを確認できる。

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

[Dropletsページ](https://cloud.digitalocean.com/droplets)をみてもイメージができているのがわかる。


![image](https://qiita-image-store.s3.amazonaws.com/0/1852/bdc0d75b-1d86-229b-a7a8-c3c327a02b9e.png)


`docker-machine env do1`を`eval`すると、`docker`コマンドがDigitalOcean(`do1`)を向くようになる。

```
$ eval "$(docker-machine env do1)"
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
```

早速、簡単なDockerイメージを実行してみる。

```
$ docker run -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from nginx
3cb35ae859e7: Pull complete
41b730702607: Pull complete
97d05af69c46: Pull complete
55516e2f2530: Pull complete
7ed37354d38d: Pull complete
e7e840eed70b: Pull complete
0b5e8be9b692: Pull complete
439e7909f795: Pull complete
ee8776c93fde: Pull complete
50c46b6286b9: Pull complete
e59ba510498b: Pull complete
42a3cf88f3f0: Already exists
nginx: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.
Digest: sha256:a0270163dd1bbb456d77996eb77114b0abd6f82208bd3a30527a8bf523879b8b
Status: Downloaded newer image for nginx:latest
```

IPは`docker-machine ip do1`でわかるから、

```
$ open http://`docker-machine ip do1`
```

で、Nginxのホームページが表示される。


![image](https://qiita-image-store.s3.amazonaws.com/0/1852/841d75e8-203f-55d4-dbd1-69b25b5e2844.png)

`docker-machine ssh do1`でDockerホストにログインもできる。

```
$ docker-machine ssh do1
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 Sun May  3 16:00:07 EDT 2015

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

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

オプションの詳細は[こちら](https://docs.docker.com/machine/#digital-ocean)。

Docker Machine良いね。

[このページ](http://qiita.com/spesnova/items/073dd64a8a1d580d9ae9)がめちゃくちゃ助かる。

[このリンク](https://www.digitalocean.com/?refcode=0a359cb71f9c)からDigitalOceanのアカウントを作ってくれると、$10クーポンがつく。（そして私には$25クーポンがつく...w）
