Docker技术入门与实战 第二版-学习笔记-10-Docker Machine 项目-1-cli
Docker Machine 是 Docker 官方编排(Orchestration)项目之一,负责在多种平台上快速安装 Docker 环境
Docker Machine是一种工具,它允许你在虚拟主机上安装Docker引擎,并使用docker-machine命令管理主机。
因为之前的内容都是在一个主机host上运行的,但是我们在真正使用的时候肯定是会有多个主机host的情况的,所以首先我们会遇见的一个问题就是怎么在多个host上将docker安装并配置好
docker machine的出现就能够让我们批量地去安装和配置好docker,host可能是本地虚拟机或云平台等
对于 Docker Machine 来说,术语 Machine
就是运行 docker daemon 的主机。“创建 Machine” 指的就是在 host 上安装和部署 docker
Machine drivers支持驱动
通过 -d选项可以选择支持的驱动类型
- Amazon Web Services
- Microsoft Azure
- Digital Ocean
- Exoscale
- Google Compute Engine
- Generic
- Microsoft Hyper-V
- OpenStack
- Rackspace
- IBM Softlayer
- Oracle VirtualBox
- VMware vCloud Air
- VMware Fusion
- VMware vSphere
- VMware Workstation (unofficial plugin, not supported by Docker)
- Grid 5000 (unofficial plugin, not supported by Docker)
- Scaleway (unofficial plugin, not supported by Docker)
其实就是虚拟化平台和云平台
操作命令
userdeMBP:~ user$ docker-machine --help Usage: docker-machine [OPTIONS] COMMAND [arg...] Create and manage machines running Docker. 运行Docker来创建并管理机器 Version: 0.16.0, build 702c267f Options: --debug, -D Enable debug mode 启用调试模式 --storage-path, -s "/Users/user/.docker/machine"
Configures storage path [$MACHINE_STORAGE_PATH] 配置存储路径 --tls-ca-cert CA to verify remotes against [$MACHINE_TLS_CA_CERT] 用于验证远程控制的CA证书 --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的客户端证书 --tls-client-key Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
客户端TLS认证中使用的私钥 --github-api-token Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
用于Github API请求的令牌 --native-ssh Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
使用本机(基于go) 的SSH实现 --bugsnag-api-token BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
错误障碍API令牌崩溃报告 --help, -h show help 显示帮助信息 --version, -v print the version day 打印版本 Commands: active Print which machine is active 打印当前huoyue 的机器 config Print the connection config for machine 打印机器的连接配置信息 create Create a machine 创建一个机器 env Display the commands to set up the environment for the Docker client
显示为Docker客户端设置环境的命令 inspect Inspect information about a machine 检查机器的信息 ip Get the IP address of a machine 得到一个机器的IP地址 kill Kill a machine 杀死一个机器 ls List machines 列举所有的机器 provision Re-provision existing machines 重建现有的机器 regenerate-certs Regenerate TLS Certificates for a machine为机器重新生成TLS证书 restart Restart a machine 重启机器 rm Remove a machine 移除机器 ssh Log into or run a command on a machine with SSH. 使用SSH登录或运行机器上的命令 scp Copy files between machines 在机器间互相复制文件 mount Mount or unmount a directory from a machine with SSHFS.
使用SSHFS从机器上挂载或不挂载一个目录 start Start a machine 开启一个机器 status Get the status of a machine 获得机器的状态 stop Stop a machine 停止机器 upgrade Upgrade a machine to the latest version of Docker 更新机器到Docker的最新版本 url Get the URL of a machine得到机器的URL version Show the Docker Machine version or a machine docker version
显示Docker机器的版本或机器中Docker的版本 help Shows a list of commands or help for one command 显示命令列表或某个命令的帮助 Run 'docker-machine COMMAND --help' for more information on a command.
1)create
格式:
docker-machine create [OPTIONS] [arg...]
选项:
userdeMBP:~ user$ docker-machine create --help Usage: docker-machine create [OPTIONS] [arg...] Create a machine 创建一个机器 Description: Run 'docker-machine create --driver name --help' to include the create flags for that driver in the help text. 可以运行“docker-machine create --driver name --help”命令去在帮助文本中包含指定驱动程序的创建标志 Options: --driver, -d "virtualbox" Driver to create machine with. [$MACHINE_DRIVER]
用来创建机器的驱动,默认为"virtualbox" --engine-env [--engine-env option --engine-env option]
Specify environment variables to set in the engine 指定设置在引擎中的环境变量 --engine-insecure-registry [--engine-insecure-registry option --engine-insecure-registry option] Specify insecure registries to allow with the created engine
指定不安全的注册中心,以便与创建的引擎一起使用 --engine-install-url "https://get.docker.com"
Custom URL to use for engine installation [$MACHINE_DOCKER_INSTALL_URL]
用于引擎安装的自定义URL --engine-label [--engine-label option --engine-label option]
Specify labels for the created engine 为创建的引擎指定标签 --engine-opt [--engine-opt option --engine-opt option]
Specify arbitrary flags to include with the created engine in the form flag=value
在表单flag=value中指定要与创建的引擎一起包含的任意标志 --engine-registry-mirror [--engine-registry-mirror option --engine-registry-mirror option] Specify registry mirrors to use [$ENGINE_REGISTRY_MIRROR] 指定要使用的注册表镜像 --engine-storage-driver
Specify a storage driver to use with the engine 指定要与引擎一起使用的存储驱动程序 --swarm Configure Machine to join a Swarm cluster 配置机器以加入Swarm集群 --swarm-addr
addr to advertise for Swarm (default: detect and use the machine IP)
为Swarm做广告的addr(默认:检测和使用机器IP) --swarm-discovery Discovery service to use with Swarm 发现用于Swarm的服务 --swarm-experimental Enable Swarm experimental features 启用Swarm的实验特性 --swarm-host "tcp://0.0.0.0:3376"
ip/socket to listen on for Swarm master 用于监听Swarm主机的ip/socket --swarm-image "swarm:latest"
Specify Docker image to use for Swarm [$MACHINE_SWARM_IMAGE] 指定为Swarm使用的Docker镜像 --swarm-join-opt [--swarm-join-opt option --swarm-join-opt option]
Define arbitrary flags for Swarm join 为Swarm连接定义任意标志 --swarm-master Configure Machine to be a Swarm master 配置机器成为Swarm主机 --swarm-opt [--swarm-opt option --swarm-opt option]
Define arbitrary flags for Swarm master 为Swarm主机定义任意标志 --swarm-strategy "spread"
Define a default scheduling strategy for Swarm 为Swarm定义一个默认的调度策略 --tls-san [--tls-san option --tls-san option]
Support extra SANs for TLS certs 为TLS证书支持额外的SANs
举例:
1》driver = vitrualbox
一步步运行所出现的问题
1.
userdeMacBook-Pro:~ user$ docker-machine create demo
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"
出现这样错误的原因是使用docker-machine之前需要先将VirtualBox安装好,与virtualBox相关的内容可见本博客的virtualbox+vagrant学习-1-环境安装及vagrantfile的简单配置-Mac系统
2.发现一直在waiting
因为下载速度过慢,所以使用--engine-registry-mirror=https://hes89po0.mirror.aliyuncs.com来使用阿里云的加速器
3.boot2docker.iso
目前的 Docker实现是建立在 Linux CGroup 等技术之上,因此无法在 MacOS X上原生使用,所以需要boot2docker.iso
⚠️什么是boot2docker.iso:
由于docker要使用LXC,namespace,cgroup等Linux内核相关技术,而mac又是基于unix的,所以要使用boot2docker来使用docker,boot2docker实际上是一个Linux的轻量级发行版,一共24M大小,完全运行于内存中。 另外boot2docker需要运行在virtual Box上,所以请先下载virtualBox
userdeMacBook-Pro:~ user$ docker-machine create --engine-registry-mirror=https://hes89po0.mirror.aliyuncs.com -d virtualbox default Running pre-create checks... (default) Unable to get the local Boot2Docker ISO version: Did not find prefix "-v" in version string (default) Default Boot2Docker ISO is out-of-date, downloading the latest release... (default) Latest release for github.com/boot2docker/boot2docker is v18.09.0 (default) Downloading /Users/user/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso...
因为检测到本地没有boot2docker.iso,会到https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso处下载,这样下载的速度是比较慢的,你可以自己到该地址下载到本地,然后将下载得到的boot2docker.iso放到/Users/user/.docker/machine/cache下
因为第一次没有运行成功,再运行时发现又去下载boot2docker.iso,明明之前已经下载好了,解决办法是使用--virtualbox-boot2docker-url= /Users/user/.docker/machine/cache/boot2docker.iso指明其所在地址
4.终于成功了:
userdeMacBook-Pro:~ user$ docker-machine create --engine-registry-mirror=https://hes89po0.mirror.aliyuncs.com --virtualbox-boot2docker-url=/Users/user/.docker/machine/cache/boot2docker.iso -d virtualbox default Running pre-create checks... (default) Boot2Docker URL was explicitly set to "/Users/user/.docker/machine/cache/boot2docker.iso" at create time, so Docker Machine cannot upgrade this machine to the latest version. Creating machine... (default) Boot2Docker URL was explicitly set to "/Users/user/.docker/machine/cache/boot2docker.iso" at create time, so Docker Machine cannot upgrade this machine to the latest version. (default) Downloading /Users/user/.docker/machine/cache/boot2docker.iso from /Users/user/.docker/machine/cache/boot2docker.iso... (default) Creating VirtualBox VM... (default) Creating SSH key... (default) Starting the VM... (default) Check network to re-create if needed... (default) Found a new host-only adapter: "vboxnet1" (default) 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 default
查看:
userdeMacBook-Pro:~ user$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
可见virtualBox虚拟机上也生成了该机器:
2》driver = generic
运行这个驱动的过程出现了很多的错误,所以单独写了一篇,可见本博客的docker-machine create -d generic 运行的波折过程及遇见的问题
2)active
格式:
docker-machine active [OPTIONS] [arg...]
⚠️[arg...]是机器的名字,可以指定多个机器,下面都一样
选项[OPTIONS]:
--timeout, -t "10" Timeout in seconds, default to 10s 超时秒数,默认为10秒
查看哪个机器是“active”(如果DOCKER_HOST环境变量指向某个机器,则认为该机器是活动的)。
但是一开始进行查看的时候什么都没有查出来:
userdeMBP:~ user$ echo $DOCKER_HOST userdeMBP:~ user$ docker-machine active Error getting active host: timeout
所以就进行了手动设置:
userdeMBP:~ user$ export DOCKER_HOST=tcp://192.168.99.100:2376 userdeMBP:~ user$ echo $DOCKER_HOST tcp://192.168.99.100:2376 userdeMBP:~ user$ docker-machine active default
3)config
格式:
docker-machine config [OPTIONS] [arg...]
选项[OPTIONS]:
--swarm Display the Swarm config instead of the Docker daemon 显示集群配置而不是Docker守护进程配置
打印机器的连接配置
举例:
userdeMacBook-Pro:~ user$ docker-machine config default
--tlsverify
--tlscacert="/Users/user/.docker/machine/machines/default/ca.pem"
--tlscert="/Users/user/.docker/machine/machines/default/cert.pem"
--tlskey="/Users/user/.docker/machine/machines/default/key.pem"
-H=tcp://192.168.99.100:2376
4)env
格式:
docker-machine env [OPTIONS] [arg...]
选项:
--swarm Display the Swarm config instead of the Docker daemon 显示集群配置而不是Docker守护进程 --shell Force environment to be configured for a specified shell: [fish, cmd, powershell, tcsh, emacs], default is auto-detect 强制环境为指定的shell配置:[fish, cmd, powershell, tcsh, emacs],默认是自动检测 --unset, -u Unset variables instead of setting them 未设置变量而不是设置它们 --no-proxy Add machine IP to NO_PROXY environment variable 将机器IP添加到NO_PROXY环境变量
显示设置Docker客户机环境的命令
1.举例:
userdeMBP:~ user$ docker-machine env default Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "10.240.203.10:2376": dial tcp 10.240.203.10:2376: i/o timeout You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'. Be advised that this will trigger a Docker daemon restart which might stop running containers.
发现出现了问题,查看当前的机器:
userdeMBP:~ user$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default virtualbox Timeout vm - generic Running tcp://10.240.203.48:2376 Unknown Unable to query docker version: Cannot connect to the docker engine endpoint
userdeMBP:~ user$ docker-machine rm vm About to remove vm WARNING: This action will delete both local reference and remote instance. Are you sure? (y/n): y Successfully removed vm
然后关闭docker-machine:
userdeMBP:~ user$ docker-machine stop Stopping "default"... Machine "default" was stopped.
再打开:
userdeMBP:~ user$ docker-machine start 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.
分配IP地址:
userdeMBP:~ user$ docker-machine env Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.100:2376": x509: certificate is valid for 10.240.203.10, not 192.168.99.100 You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'. Be advised that this will trigger a Docker daemon restart which might stop running containers.
此时查看还是没有成功的:
userdeMBP:~ user$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 Unknown Unable to query docker version: Get https://192.168.99.100:2376/v1.15/version: x509: certificate is valid for 10.240.203.10, not 192.168.99.100
进行TLS证书的重新生成:
userdeMBP:~ user$ 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... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon...
然后就解决了:
userdeMBP:~ user$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
此时就能够运行env命令了:
userdeMBP:~ user$ docker-machine env default export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/user/.docker/machine/machines/default" export DOCKER_MACHINE_NAME="default" # Run this command to configure your shell: # eval $(docker-machine env default)
查看环境变量并设置:
userdeMBP:~ user$ env | grep DOCKER //什么都没有返回 userdeMBP:~ user$ eval $(docker-machine env default) //将命令传给shell执行,所以下面的值就被设置成了环境变量 userdeMBP:~ user$ env | grep DOCKER DOCKER_HOST=tcp://192.168.99.100:2376 DOCKER_MACHINE_NAME=default DOCKER_TLS_VERIFY=1 DOCKER_CERT_PATH=/Users/user/.docker/machine/machines/default
如果你想要取消设置:
userdeMBP:~ user$ docker-machine env -u default Error: Expected no machine name when the -u flag is present //不需要机器名 userdeMBP:~ user$ docker-machine env -u unset DOCKER_TLS_VERIFY unset DOCKER_HOST unset DOCKER_CERT_PATH unset DOCKER_MACHINE_NAME # Run this command to configure your shell: # eval $(docker-machine env -u) userdeMBP:~ user$ eval $(docker-machine env -u) //就可以将上面的四样设置都取消 userdeMBP:~ user$ env | grep DOCKER //然后就没有东西返回了
2.--shell的使用
上面描述的输出是针对shell bash和zsh的(如果不确定使用哪个shell,很可能是bash)。然而,这些并不是Docker机器支持的唯一shell。Docker Machine检测环境中可用的shell并列出它们。Docker支持bash、cmd、powershell和emacs。
如果你在Windows上并使用PowerShell或cmd.exe, 调用docker-machine env命令Docker Machine现在可以自动检测shell。如果自动检测不起作用,你仍然可以使用docker-machine env的--shell标志覆盖它。
比如PowerShell:
userdeMBP:~ user$ docker-machine env --shell powershell default $Env:DOCKER_TLS_VERIFY = "1" $Env:DOCKER_HOST = "tcp://192.168.99.100:2376" $Env:DOCKER_CERT_PATH = "/Users/user/.docker/machine/machines/default" $Env:DOCKER_MACHINE_NAME = "default" # Run this command to configure your shell: # & docker-machine env --shell powershell default | Invoke-Expression
然后的就是PowerShell版本的shell语句
如cmd.exe:
userdeMBP:~ user$ docker-machine env --shell cmd default SET DOCKER_TLS_VERIFY=1 SET DOCKER_HOST=tcp://192.168.99.100:2376 SET DOCKER_CERT_PATH=/Users/user/.docker/machine/machines/default SET DOCKER_MACHINE_NAME=default REM Run this command to configure your shell: REM @FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO @%i
3.--no-proxy的使用——将创建的机器从代理中排除
在需要HTTP代理进行internet访问的网络环境中,将docker-machine与本地VM provider(如virtualbox或vmwarefusion)一起使用时,这非常有用。
userdeMBP:~ user$ docker-machine env --no-proxy default export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/user/.docker/machine/machines/default" export DOCKER_MACHINE_NAME="default" export NO_PROXY="192.168.99.100" # Run this command to configure your shell: # eval $(docker-machine env --no-proxy default)
5)inspect
docker-machine inspect [OPTIONS] [arg...]
选项:
--format, -f Format the output using the given go template.使用给定的go模板格式化输出
检查机器信息
默认情况下,这将以JSON格式呈现关于机器的信息。如果指定了格式,则为每个结果执行给定的模板。
Go的文本/模板包描述了格式的所有细节。
除了文本/模板语法之外,还有一些额外的函数、json和prettyjson,可以使用它们将输出格式化为json(下面有文档说明)。
举例:
userdeMBP:~ user$ docker-machine inspect default { "ConfigVersion": 3, "Driver": { "IPAddress": "192.168.99.100", "MachineName": "default", "SSHUser": "docker", "SSHPort": 55036, "SSHKeyPath": "/Users/user/.docker/machine/machines/default/id_rsa", "StorePath": "/Users/user/.docker/machine", "SwarmMaster": false, "SwarmHost": "tcp://0.0.0.0:3376", "SwarmDiscovery": "", "VBoxManager": {}, "HostInterfaces": {}, "CPU": 1, "Memory": 1024, "DiskSize": 20000, "NatNicType": "82540EM", "Boot2DockerURL": "/Users/user/.docker/machine/cache/boot2docker.iso", "Boot2DockerImportVM": "", "HostDNSResolver": false, "HostOnlyCIDR": "192.168.99.1/24", "HostOnlyNicType": "82540EM", "HostOnlyPromiscMode": "deny", "UIType": "headless", "HostOnlyNoDHCP": false, "NoShare": false, "DNSProxy": true, "NoVTXCheck": false, "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": [ "https://hes89po0.mirror.aliyuncs.com" ], "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": "/Users/user/.docker/machine/certs", "CaCertPath": "/Users/user/.docker/machine/certs/ca.pem", "CaPrivateKeyPath": "/Users/user/.docker/machine/certs/ca-key.pem", "CaCertRemotePath": "", "ServerCertPath": "/Users/user/.docker/machine/machines/default/server.pem", "ServerKeyPath": "/Users/user/.docker/machine/machines/default/server-key.pem", "ClientKeyPath": "/Users/user/.docker/machine/certs/key.pem", "ServerCertRemotePath": "", "ServerKeyRemotePath": "", "ClientCertPath": "/Users/user/.docker/machine/certs/cert.pem", "ServerCertSANs": [], "StorePath": "/Users/user/.docker/machine/machines/default" } }, "Name": "default" }
在大多数情况下,你可以相当直接地从JSON中选择任何字段,使用--format标志:
userdeMBP:~ user$ docker-machine inspect --format='{{.Driver.IPAddress}}' default 192.168.99.100
格式:
docker-machine ip [arg...]
得到机器的IP地址
举例:
userdeMBP:~ user$ docker-machine ip default 192.168.99.100
7)kill
格式:
docker-machine kill [arg...]
杀死机器,即强制停止机器,机器状态status从running变成stopped
8)ls
格式:
docker-machine ls [OPTIONS] [arg...]
选项:
--quiet, -q Enable quiet mode 启用安静模式 --filter [--filter option --filter option] Filter output based on conditions provided 根据提供的条件过滤输出 --timeout, -t "10" Timeout in seconds, default to 10s 超时时间,默认为10秒 --format, -f Pretty-print machines using a Go template 使用Go模版好看地打印机器信息
1.--timeout
ls命令试图并行地到达每个主机。如果给定主机在不到10秒内没有响应,ls命令将声明该主机处于超时状态。在某些情况下(连接不良、负载高或在进行故障排除时),你可能希望增加或减少此值。为此,可以使用-t标志和一个以秒为单位的数值来指定超时时间。
userdeMBP:~ user$ docker-machine ls -t 12 NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
2.--filter
过滤标志(--filter)格式是键值对。如果有多个过滤器,则传递多个标志,比如:--filter "foo=bar" --filter "bif=baz"
目前支持的过滤器有:
- driver (driver name)驱动名
- swarm (swarm master’s name)集群主机名
- state (
Running|Paused|Saved|Stopped|Stopping|Starting|Error
)机器状态信息 - name名称(驱动程序返回的机器名称,支持golang风格的正则表达式)
- label标签(使用--engine-label选项创建的机器,可以使用label=<key>[=<value>]进行过滤)
举例:
userdeMBP:~ user$ docker-machine ls --filter name=foo0 NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS userdeMBP:~ user$ docker-machine ls --filter name=default NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS default - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
$ docker-machine ls --filter label=com.class.app=foo1 --filter label=com.class.app=foo2 NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS foo1 - virtualbox Running tcp://192.168.99.105:2376 v1.9.1 foo2 * virtualbox Running tcp://192.168.99.107:2376 v1.9.1
3.--format
相应的表:
Placeholder | Description |
---|---|
.Name | Machine name |
.Active | Is the machine active? |
.ActiveHost | Is the machine an active non-swarm host? |
.ActiveSwarm | Is the machine an active swarm master? |
.DriverName | Driver name |
.State | Machine state (running, stopped...) |
.URL | Machine URL |
.Swarm | Machine swarm name |
.Error | Machine errors |
.DockerVersion | Docker Daemon version |
.ResponseTime | Time taken by the host to respond |
举例:
userdeMBP:~ user$ docker-machine ls --format "{{.Name}}: {{.DriverName}}" default: virtualbox
如果想以表的形式表示:
userdeMBP:~ user$ docker-machine ls --format "table {{.Name}} {{.DriverName}}" NAME DRIVER default virtualbox
9)mount
格式:
docker-machine mount [OPTIONS] [arg...]
选项:
--unmount, -u Unmount instead of mount 卸载而不是挂载
使用sshfs将目录从机器挂载到本地主机。
参数的符号是machinename:/path/to/dir;你还可以提供一个替代挂载点(默认是相同的dir路径)。
userdeMBP:~ user$ mkdir foo userdeMBP:~ user$ docker-machine ssh default mkdir foo userdeMBP:~ user$ docker-machine mount default:/home/docker/foo foo You must have a copy of the sshfs binary locally to use the mount feature
出问题,解决办法,在本地安装sshfs
安装:
brew install Caskroom/cask/osxfuse
brew install sshfs
然后再运行就成功了:
userdeMBP:~ user$ docker-machine mount default:/home/docker/foo foo
该命令的意思是将机器default的/home/docker/foo文件夹挂载到本地主机当前目录的foo文件夹,然后本地的foo文件夹变成了如下图所示:
然后再运行:
userdeMBP:~ user$ touch foo/bar
在本地的foo文件夹中生成bar文件,然后我们就能够看见机器上的foo中也出现了该bar文件,则说明挂载成功
docker@default:~$ cd foo docker@default:~/foo$ ls //一开始是什么都没有的 docker@default:~/foo$ ls bar
要再次卸载目录,可以使用相同的选项,但是使用-u标志。你还可以直接调用fuserunmount(或fusermount -u)命令。
userdeMBP:~ user$ docker-machine mount -u default:/home/docker/foo foo You must have a copy of the fusermount binary locally to use the unmount option
但是运行有错,然后就打算直接使用fusermount -u:
userdeMBP:~ user$ fusermount -u default:/home/docker/foo foo -bash: fusermount: command not found
也没能成功,后面查看资料发现使用umount即可:
userdeMBP:~ user$ umount default:/home/docker/foo foo umount: default:/home/docker/foo: not currently mounted
然后本地的foo文件夹就变成了原样,卸载成功:
10)provision
格式:
docker-machine provision [arg...]
重建现有的机器
在创建的机器上重新运行配置。
有时,在创建的机器上重新运行机器的配置流程可能会有所帮助。这样做的原因可能包括初始供应过程中的失败,或者偏离所需的系统状态(包括最初指定的集群或引擎配置)。
机器配置过程将包括:
- 将实例上的主机名设置为机器地址所在的名称,例如default。
- 如果docker还没有出现,则安装它。
- 生成一组证书(通常使用默认的自签名CA),并配置守护进程接受TLS上的连接。
- 将生成的证书复制到服务器和本地配置目录。
- 根据创建时指定的选项配置Docker引擎。
- 如果适用则配置和激活集群。
举例:
$ docker-machine provision default Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon...
11)regenerate-certs
格式:
docker-machine regenerate-certs [OPTIONS] [arg...]
选项:
--force, -f Force rebuild and do not prompt 强制重建且不提示 --client-certs Also regenerate client certificates and CA. 还可以重新生成客户机证书和CA
重新生成机器的TLS证书
具体例子可见上面的env部分
12)restart
格式:
docker-machine restart [arg...]
重启机器
其实就相当于同时运行:
docker-machine stop
docker-machine start
但一些云驱动程序试图实现一个聪明的重启,以保持相同的IP地址。
13)rm
格式:
docker-machine rm [OPTIONS] [arg...]
选项:
--force, -f Remove local configuration even if machine cannot be removed, also implies an automatic yes (`-y`) 删除本地配置,即使机器无法删除,也意味着自动yes (' -y ') -y Assumes automatic yes to proceed with remove, without prompting further user confirmation 假设自动yes继续删除,而不提示进一步的用户确认
移除一个或多个机器
例子:
$ docker-machine rm baz About to remove baz Are you sure? (y/n): y //会询问是否真的要删除 Successfully removed baz $ docker-machine rm -y foo //使用-y则不会询问 About to remove foo Successfully removed foo
14)ssh
格式:
docker-machine ssh [arg...]
登录机器或者是运行相应的命令
1.login
docker-machine ssh machinename
2.在机器上运行一个命令,如:
userdeMBP:~ user$ docker-machine ssh default free total used free shared buff/cache available Mem: 1013116 59720 706076 229024 247320 706516 Swap: 1205164 0 1205164
如果使用下一节详细介绍的“外部”SSH类型,则可以在生成的命令中包含传递到SSH二进制文件的其他参数(除非它们与Docker机器生成的命令的任何默认参数冲突)。例如,以下命令将端口8080从默认机器转发到你的主机上的本地主机:
$ docker-machine ssh default -L 8080:localhost:8080
15)start
格式:
docker-machine start [arg...]
开启一个机器
$ docker-machine start default
16)status
格式:
docker-machine status [arg...]
得到机器的状态
举例:
userdeMBP:~ user$ docker-machine status default Running
17)stop
格式:
docker-machine stop [arg...]
停止机器,机器的状态会从running变为stopped
18)upgrade
格式:
docker-machine upgrade [arg...]
将机器升级到Docker的最新版本。升级如何进行取决于在创建的实例上使用的底层分布。
例如,如果机器使用Ubuntu作为底层操作系统,它运行一个类似于sudo apt-get upgrade docker-engine的命令,因为机器希望Ubuntu机器能够使用这个包。另一个例子是,如果机器的操作系统使用boot2docker,这个命令将下载最新的boot2docker ISO,并将机器现有的ISO替换为最新的ISO。
$ docker-machine upgrade default
⚠️:如果你使用的是自定义boot2docker ISO(使用--virtualbox-boot2docker-url或等效标志指定),那么在该机器上运行升级将完全用可用的最新“普通”boot2docker ISO替换指定的ISO。
19)url
格式:
docker-machine url [arg...]
得到机器的URL
userdeMBP:~ user$ docker-machine url default tcp://192.168.99.100:2376