使用Docker的通用驱动创建Docker主机【Docker】docker-machine

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

今回はvmからvmへGenericプロドライバーを使う方法をやってみたかったのですが、普通にやるのであれば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

Secure Shell (SSH)

我将使VM1无需密码即可通过SSH登录到VM2。

$ 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のインストールが始まる。

确认

正在跑步。

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命令创建的Docker主机进行vm2的设置。

通过执行docker-machine env的输出来进行配置。

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等任何操作。

请只提供一个选项,对以下内容进行汉语本地化的改写:
参考

广告
将在 10 秒后关闭
bannerAds