Docker Machine

Docker Machine是Docker官方Orchestration项目之一,负责在多种平台上快速安装Docker环境。

Docker Machine是一个工具,它允许你在虚拟宿主机上安装Docker Engine引擎,并使用docker-machine命令管理宿主机。你可以使用机器在本地的Mac或Windows上创建和使用Docker宿主机。

4933701-b408317e4ddfe0ee.png
Mac或Windows上运行Docke

docker-machine是一个虚拟机管理工具,通过创建一个安装好的docker虚拟机并设置对应的环境变量,使本地docker工具获得透明远程操作虚拟机的能力,从而使本身不支持docker的windows和mac能够直接使用docker命令。

4933701-2c89fe9176d7569f.png
远程系统上配置Docker宿主机

Docker与Docker Machine

常说的Docker实际上指的是Docker Engine引擎,它是一个客户端/服务器应用程序,由Docker守护进程、REST API指定与守护进程交互的接口、CLI命令行接口与守护进程通信(通过封装REST API),Docker Engine从CLI中接收docker命令。

4933701-8eb84ce910481f73.png
Docker Engine

Docker Machine是一个用于配置和管理Docker Engine宿主机的工具,通常在本地系统上安装。Docker Machine有自己的CLI命令行客户端工具docker-machine和Docker Engine客户端工具docker。可以使用Docker Machine在一个或多个虚拟系统上安装Docker Engine。这些虚拟系统可以是本地也可以是远程的。Dockerized宿主机本身可以认为是被管理的machine。

4933701-7d76b919cceda17b.png
Docker Machine

docker-machine帮助

$ docker-machine --help
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.14.0, build 89b8332

Author:
  Docker Machine Contributors - <https://github.com/docker/machine>

Options:
  --debug, -D                                           Enable debug mode
  --storage-path, -s "C:\Users\junchow\.docker\machine" Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                                         CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                                          Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                                     Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key                                      Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                                    Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                                          Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --bugsnag-api-token                                   BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --help, -h                                            show help
  --version, -v                                         print the version

Commands:
  active                终端查看活动的docker宿主机
  config                终端查看连接docker宿主机的配置信息
  create                终端创建一个docker宿主机
  env                   终端查看连接某docker宿主机的环境变量
  inspect               终端查看docker宿主机的更新信息
  ip                    终端查看docker宿主机地址
  kill                  终端杀死docker宿主机进程
  ls                    终端查看当前已经创建或正在运行的虚拟机
  provision             Re-provision existing machines
  regenerate-certs      终端为docker宿主机重新生成TLS认证信息
  restart               终端重启docker宿主机,若宿主机状态显示为timeout可重启。
  rm                    终端删除docker宿主机
  ssh                   终端使用SSH远程连接docker宿主机
  scp                   终端使用SCP远程传输文件给docker宿主机
  mount                 Mount or unmount a directory from a machine with SSHFS.
  start                 终端开启docker宿主机
  status                终端查看docker宿主机的状态
  stop                  终端停止docker宿主机
  upgrade               终端更新宿主机上docker版本为最新
  url                   终端查看docker主机的URL
  version               终端查看docker-machine的版本
  help                  帮助
 command

Run 'docker-machine COMMAND --help' for more information on a command.

docker-machine命令

version 终端查看docker-machine的版本

$ docker-machine -v
$ docker-machine version
$ docker-machine -version
$ docker-machine --version
docker-machine version 0.14.0, build 89b8332

create 终端创建一个docker宿主机

$ docker-machine create node
Running pre-create checks...
Creating machine...
(node) Copying C:\Users\junchow\.docker\machine\cache\boot2docker.iso to C:\Users\junchow\.docker\machine\machines\node\boot2docker.iso...
(node) Creating VirtualBox VM...
(node) Creating SSH key...
(node) Starting the VM...
(node) Check network to re-create if needed...
(node) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
(node) 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 node

rm 终端删除docker宿主机

$ docker-machine rm node
About to remove node
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed node

ls 终端查看当前已经创建或正在运行的虚拟机

超时停止状态Timeout

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL   SWARM   DOCKER   ERRORS
default            virtualbox   Timeout

正常运行状态Running

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.5

active 终端查看活动的docker宿主机

$ docker-machine active
default

status 终端查看docker宿主机的状态

$ docker-machine status
Running

$ docker-machine status default
Running

stop 终端停止docker宿主机

$ docker-machine stop default
Stopping "default"...
Machine "default" was stopped.

start 终端开启docker宿主机

$ docker-machine start default
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

restart 终端重启docker宿主机

若宿主机状态显示为timeout可重启

$ docker-machine restart default
Restarting "default"...
(default) Check network to re-create if needed...
(default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
(default) Waiting for an IP...
Waiting for SSH to be available...
Detecting the provisioner...
Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

kill 终端杀死docker宿主机进程

若重启失败可杀死docker宿主机进程或虚拟机进程后重新启动。

$ docker-machine kill default
Killing "default"...
Machine "default" was killed.

config 终端查看连接docker宿主机的配置信息

$ docker-machine config
--tlsverify
--tlscacert="C:\\Users\\junchow\\.docker\\machine\\machines\\default\\ca.pem"
--tlscert="C:\\Users\\junchow\\.docker\\machine\\machines\\default\\cert.pem"
--tlskey="C:\\Users\\junchow\\.docker\\machine\\machines\\default\\key.pem"
-H=tcp://192.168.99.100:2376

env 终端查看连接某docker宿主机的环境变量

$ docker-machine env
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\junchow\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env') DO @%i

inspect 终端查看docker宿主机的更新信息

$ docker-machine inspect
{
    "ConfigVersion": 3,
    "Driver": {
        "IPAddress": "192.168.99.100",
        "MachineName": "default",
        "SSHUser": "docker",
        "SSHPort": 51111,
        "SSHKeyPath": "C:\\Users\\junchow\\.docker\\machine\\machines\\default\\id_rsa",
        "StorePath": "C:\\Users\\junchow\\.docker\\machine",
        "SwarmMaster": false,
        "SwarmHost": "tcp://0.0.0.0:3376",
        "SwarmDiscovery": "",
        "VBoxManager": {},
        "HostInterfaces": {},
        "CPU": 1,
        "Memory": 1024,
        "DiskSize": 20000,
        "NatNicType": "82540EM",
        "Boot2DockerURL": "",
        "Boot2DockerImportVM": "",
        "HostDNSResolver": false,
        "HostOnlyCIDR": "192.168.99.1/24",
        "HostOnlyNicType": "82540EM",
        "HostOnlyPromiscMode": "deny",
        "UIType": "headless",
        "HostOnlyNoDHCP": false,
        "NoShare": false,
        "DNSProxy": true,
        "NoVTXCheck": true,
        "ShareFolder": ""
    },
    "DriverName": "virtualbox",
    "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": "",
            "Agent": false,
            "Master": false,
            "Host": "tcp://0.0.0.0:3376",
            "Image": "swarm:latest",
            "Strategy": "spread",
            "Heartbeat": 0,
            "Overcommit": 0,
            "ArbitraryFlags": [],
            "ArbitraryJoinFlags": [],
            "Env": null,
            "IsExperimental": false
        },
        "AuthOptions": {
            "CertDir": "C:\\Users\\junchow\\.docker\\machine\\certs",
            "CaCertPath": "C:\\Users\\junchow\\.docker\\machine\\certs\\ca.pem",
            "CaPrivateKeyPath": "C:\\Users\\junchow\\.docker\\machine\\certs\\ca-key.pem",
            "CaCertRemotePath": "",
            "ServerCertPath": "C:\\Users\\junchow\\.docker\\machine\\machines\\default\\server.pem",
            "ServerKeyPath": "C:\\Users\\junchow\\.docker\\machine\\machines\\default\\server-key.pem",
            "ClientKeyPath": "C:\\Users\\junchow\\.docker\\machine\\certs\\key.pem",
            "ServerCertRemotePath": "",
            "ServerKeyRemotePath": "",
            "ClientCertPath": "C:\\Users\\junchow\\.docker\\machine\\certs\\cert.pem",
            "ServerCertSANs": [],
            "StorePath": "C:\\Users\\junchow\\.docker\\machine\\machines\\default"
        }
    },
    "Name": "default"
}

ip 终端查看docker宿主机地址

$ docker-machine ip
192.168.99.100

url 终端查看docker主机的URL

$ docker-machine url
tcp://192.168.99.100:2376

upgrade 终端更新宿主机上docker版本为最新

$ docker-machine upgrade
Waiting for SSH to be available...
Detecting the provisioner...
Upgrading docker...
Stopping machine to do the upgrade...
Upgrading machine "default"...
Copying C:\Users\junchow\.docker\machine\cache\boot2docker.iso to C:\Users\junchow\.docker\machine\machines\default\boot2docker.iso...
Starting machine back up...
(default) Check network to re-create if needed...
(default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
(default) Waiting for an IP...
Restarting docker...

ssh 终端使用SSH远程连接docker宿主机

$ docker-machine ssh default
   ( '>')   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@default:~$

scp 终端使用SCP远程传输文件给docker宿主机

$ docker-machin scp default
Usage: docker-machine scp [OPTIONS] [arg...]

Copy files between machines

Description:
   Arguments are [[user@]machine:][path] [[user@]machine:][path].

Options:

   --recursive, -r      Copy files recursively (required to copy directories)
   --delta, -d          Reduce amount of data sent over network by sending only the differences (uses rsync)
   --quiet, -q          Disables the progress meter as well as warning and diagnostic messages from ssh
Improper number of arguments

regenerate-certs 终端为docker宿主机重新生成TLS认证信息

$ docker-machine regenerate-certs default
Regenerate TLS machine certs?  Warning: this is irreversible. (y/n): y
Regenerating TLS certificates
Waiting for SSH to be available...
Detecting the provisioner...
Host is not running

# 重启失败,杀死虚拟机所有相关进程(VirtualBox)后重试。
$ docker-machine restart default
Restarting "default"...
Starting "default"...
(default) Check network to re-create if needed...
(default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
Error setting up host only network on machine start: C:\Program Files\Oracle\VirtualBox\VBoxManage.exe modifyvm default --nic2 hostonly --nictype2 82540EM --nicpromisc2 deny --hostonlyadapter2 VirtualBox Host-Only Ethernet Adapter #2 --cableconnected2 on failed:
VBoxManage.exe: error: Code E_FAIL (0x80004005) - Unspecified error (extended info not available)
VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Write)" at line 525 of file VBoxManageModifyVM.cpp

注意事项

可以明显发现,其中一直有一个错误存在:

Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
Error setting up host only network on machine start
4933701-31282e17a58aab94.png
VirtualBox版本
4933701-89adaeb8a8c7fe97.png
VirutalBox进程

说明虚拟机创建或者访问本地网络适配器出现了问题:windows的docker安装需要依赖虚拟机,点开虚拟机找到默认虚拟机,打开网卡,分别指定不同的网络模式然后重启你的虚拟机,这个时候使用quick startterminal还是会报错,图形界面也会报错。

重新下载 VirtualBox https://www.virtualbox.org/wiki/Downloads 的最新版本尝试下。

Docker Machine是一个开源项目,它提供了一种简便的方法来在各个操作系统上安装和管理Docker引擎。它的主要目的是在不同的计算机上安装、配置和管理Docker引擎,从而让用户更便捷地进行容器化应用的开发和部署。 使用Docker Machine,我们可以通过简单的命令来创建和管理虚拟机,这些虚拟机可以托管我们的Docker容器。Docker Machine提供了一些预定义的虚拟机驱动程序,如VirtualBox、VMware、AWS等,但也允许用户自定义驱动程序来满足自己的需求。 Docker Machine的可执行文件是一个命令行工具,通过它我们可以执行各种操作,包括创建虚拟机、启动、停止、删除等。我们可以使用该工具快速地在本地或远程主机上安装和配置Docker引擎,而无需手动进行复杂的设置。 对于开发者和系统管理员而言,Docker Machine的执行文件非常实用。它可以大大简化搭建和管理Docker环境的过程,特别是在多台机器上进行容器化应用开发和部署时。通过Docker Machine,我们可以轻松地创建和配置多个虚拟机,将它们连接到Docker集群,从而使得容器的管理更加便捷和高效。 总之,Docker Machine的可执行文件为我们提供了一个简单而强大的工具,使得Docker容器的部署和管理变得更加容易。无论是在本地还是远程主机上,使用Docker Machine都能够为我们提供一种方便的方式来管理Docker引擎和容器化应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值