05 - Docker Machine 概述

Docker Machine可以用来:

  • 在Mac或Windows系统中安装和运行Docker服务
  • 提供和管理多个远程Docker主机
  • 提供Swarm群集

什么是Docker Machine?

Docker Machine是一个可以让你在虚拟机上安装Docker引擎的工具,并且可以通过docker-machine的指令来管理这些虚拟机。可以使用Docker Machine在你的本地Mac或Windows系统中、网络公司、数据中心或提供了像AWS或Digtal Ocean的云端来创建Docker虚拟主机。

使用docker-machine命令可以启动、检测、停止、重启一个被管理的虚拟主机,升级Docker客户端和守护进程和配置一个Docker客户端与你的主机对话。

将机器的CLI指向正在运行的被管理的主机,你就可以直接在该主机上运行docker命令了,如运行:docker-machine env default表示机器的CLI指向了一个名为default的虚拟主机,根据屏幕上的提示信息完成环境变量的配置,即可运行docker psdocker run hello-world等等。

安装Docker Machine

在文章《04 - Windows8中安装及验证Docker》中已经讲述了如何安装Docker Machine及安装Docker Machine的注意事项,在这里就不再赘述!

Docker Machine命令集

使用docker-machine help查看所有帮助信息:

C:\Users\zsl-pc>docker-machine help
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.7.0, build a650a40

…………

Commands:
  active                Print which machine is active
  config                Print the connection config for machine
  create                Create a machine
  env                   Display the commands to set up the environment for the Docker client
  inspect               Inspect information about a machine
  ip                    Get the IP address of a machine
  kill                  Kill a machine
  ls                    List machines
  provision             Re-provision existing machines
  regenerate-certs      Regenerate TLS Certificates for a machine
  restart               Restart a machine
  rm                    Remove a machine
  ssh                   Log into or run a command on a machine with SSH.
  scp                   Copy files between machines
  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
  url                   Get the URL of a machine
  version               Show the Docker Machine version or a machine docker version
  help                  Shows a list of commands or help for one command

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

中文翻译如下:

命令:

active:显示出当前的活动主机;

config:显示主机连接配置;

create:创建一个主机;

env:设置当前的环境是与哪个主机通信

inspect:查看主机的详情信息;

ip:查看主机IP地址;

kill:强制关闭一个主机;

ls:查看所有的主机信息;

provision:重新配置现有主机;

regenerate-certs:为主机重新生成证书;

restart:重启一个主机;

rm:删除一个主机;

ssh:以ssh方式连接到主机上;

scp:远程复制,用过Linux的应该都清楚

status:查看主机状态;

stop:停止一个正在运行的主机;

upgrade:升级主机的Docker服务到最新版本;

url

version:查Docker Machine版本;

help:查看Docker Machine的帮助信息;

使用docker-machine 指令 --help可以查看具体指令帮助信息。

常用Docker Machine指令

  • 创建主机
docker-machine create -d virtualbox test01

也可以写成:

docker-machine create --driver virtualbox test01

创建一个名为test01的主机,驱动方式为virtualbox

  • 启动主机
docker-machine start test01
C:\Users\zsl-pc>docker-machine start mytest
Starting "mytest"...
(mytest) Check network to re-create if needed...
(mytest) Waiting for an IP...
Machine "mytest" 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.

启动名为test01的主机,根据提示需要设置环境

  • 配置环境
C:\Users\zsl-pc>docker-machine env mytest
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\zsl-pc\.docker\machine\machines\mytest
SET DOCKER_MACHINE_NAME=mytest
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env mytest') DO @%i

根据提示还需要再进行下一步配置

@FOR /f "tokens=*" %i IN ('docker-machine env mytest') DO @%i

注意:在使用命令:docker-machine env mytest时可能会出现以下提示:

C:\Users\zsl-pc>docker-machine env mytest
Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates
for host "192.168.99.101:2376": dial tcp 192.168.99.101:2376: connectex: No connection could be made because the target
machine actively refused it.
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
Be advised that this will trigger a Docker daemon restart which will stop running containers.

当出现上述信息时需要重新生成证书信息。

  • 重新生成证书
C:\Users\zsl-pc>docker-machine regenerate-certs mytest
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...
  • 查看IP
C:\Users\zsl-pc>docker-machine ip
192.168.99.100
  • 列表主机
C:\Users\zsl-pc>docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   *        virtualbox   Running   tcp://192.168.99.102:2376           v1.12.3
mytest    -        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.3
  • 查看当前活动主机
C:\Users\zsl-pc>docker-machine active
mytest

上述的这些指令是在docker-machine使用相对较多实用性也很强的指令。其他的只用简单测试下便知晓其作用。

你可能感兴趣的:(05 - Docker Machine 概述)