【Docker】docker-machineのGenericドライバーを使ってDockerホストを作成する

Vagrantで立てた2つのvmで、vm1からvm2に向かってdocker-composeを使ってdockerを入れる。

今回はvmからvmへ[Genericプロドライバー](https://docs.docker.com/machine/drivers/generic/)を使う方法をやってみたかったのですが、普通にやるのであれば[Virtualboxプロバイダー](https://docs.docker.com/machine/drivers/virtualbox/)を使うのが簡単だと思います。

# Vagrantfile

```
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  config.ssh.forward_agent = true

  config.vm.define "vm1" do |d|
    d.vm.box = "ubuntu/trusty64"
    d.vm.network :private_network, ip: "192.168.33.10", virtualbox__intnet: "intnet"
  end

  config.vm.define "vm2" do |d|
    d.vm.box = "ubuntu/trusty64"
    d.vm.network :private_network, ip: "192.168.33.20", virtualbox__intnet: "intnet"

    d.vm.provider "virtualbox" do |vb|
      # Don't boot with headless mode
      vb.gui = true

      # Use VBoxManage to customize the VM. For example to change memory:
      vb.customize ["modifyvm", :id, "--memory", "2048"]
    end
  end
end
```

# SSH

vm1からvm2へSSHでパスワードなしでログインできるようにしておきます。

```
$ ssh-keygen
$ ssh-copy-id -i ~/.ssh/id_rsa.pub vagrant@192.168.33.20
$ ssh-add ~/.ssh/id_rsa
$ ssh vagrant@192.168.33.20 # 確認
```

# docker-machineのインストール

```
$ 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ホストを作成する

```
$ docker-machine create --driver generic --generic-ip-address=192.168.33.20 --generic-ssh-user vagrant --generic-ssh-key ~/.ssh/id_rsa vm
```

対象ホストにdockerのインストールが始まる。

# 確認

Runningしてる。

```
vagrant@vagrant-ubuntu-trusty-64:~$ docker-machine ls 
NAME   ACTIVE   DRIVER    STATE     URL                        SWARM   DOCKER    ERRORS
vm    *        generic   Running   tcp://192.168.33.20:2376           v1.12.3   
```

`docker-machine inspect`で情報を確認。

```
vagrant@vagrant-ubuntu-trusty-64:~$ docker-machine inspect vm 
{
    "ConfigVersion": 3,
    "Driver": {
        "IPAddress": "192.168.33.20",
        "MachineName": "vm",
        "SSHUser": "vagrant",
        "SSHPort": 22,
        "SSHKeyPath": "/home/vagrant/.docker/machine/machines/vm/id_rsa",
        "StorePath": "/home/vagrant/.docker/machine",
        "SwarmMaster": false,
        "SwarmHost": "",
        "SwarmDiscovery": "",
        "EnginePort": 2376,
        "SSHKey": "/home/vagrant/.ssh/id_rsa"
    },
    "DriverName": "generic",
    "HostOptions": {
        "Driver": "",
        "Memory": 0,
        "Disk": 0,
        "EngineOptions": {
            "ArbitraryFlags": [],
            "Dns": null,
            "GraphDir": "",
            "Env": [],
            "Ipv6": false,
            "InsecureRegistry": [],
            "Labels": [],
            "LogLevel": "",
            "StorageDriver": "",
            "SelinuxEnabled": false,
            "TlsVerify": true,
            "RegistryMirror": [],
            "InstallURL": "https://get.docker.com"
        },
        "SwarmOptions": {
            "IsSwarm": false,
            "Address": "",
            "Discovery": "",
            "Master": false,
            "Host": "tcp://0.0.0.0:3376",
            "Image": "swarm:latest",
            "Strategy": "spread",
            "Heartbeat": 0,
            "Overcommit": 0,
            "ArbitraryFlags": [],
            "Env": null,
            "IsExperimental": false
        },
        "AuthOptions": {
            "CertDir": "/home/vagrant/.docker/machine/certs",
            "CaCertPath": "/home/vagrant/.docker/machine/certs/ca.pem",
            "CaPrivateKeyPath": "/home/vagrant/.docker/machine/certs/ca-key.pem",
            "CaCertRemotePath": "",
            "ServerCertPath": "/home/vagrant/.docker/machine/machines/vm/server.pem",
            "ServerKeyPath": "/home/vagrant/.docker/machine/machines/vm/server-key.pem",
            "ClientKeyPath": "/home/vagrant/.docker/machine/certs/key.pem",
            "ServerCertRemotePath": "",
            "ServerKeyRemotePath": "",
            "ClientCertPath": "/home/vagrant/.docker/machine/certs/cert.pem",
            "ServerCertSANs": [],
            "StorePath": "/home/vagrant/.docker/machine/machines/vm"
        }
    },
    "Name": "vm"
}
```

# アクティブにする

vm1側からdockerコマンドでvm2に立てたDockerホストを使うための設定をします。

`docker-machine env`の出力を`eval`することで設定できます。

```
vagrant@vagrant-ubuntu-trusty-64:~$ docker-machine env vm
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.33.20:2376"
export DOCKER_CERT_PATH="/home/vagrant/.docker/machine/machines/vm"
export DOCKER_MACHINE_NAME="vm"
# Run this command to configure your shell: 
# eval $(docker-machine env vm)
```

```
vagrant@vagrant-ubuntu-trusty-64:~$ eval "$(docker-machine env vm 2> /dev/null)"
vagrant@vagrant-ubuntu-trusty-64:~$ docker-machine active
vm
```

これで`docker pull`したり`docker run`したり何でも。

# 参考

http://qiita.com/spesnova/items/073dd64a8a1d580d9ae9

http://qiita.com/pepepe/items/74cb0ee931d455760ce1

http://docs.docker.jp/machine/install-machine.html