Create a new Docker Machine with the Hyper-V driver

docker-machine就是docker工具集中提供的用来管理容器化主机的工具,用来管理运行在不同环境的主机,包括:本地虚拟机,远程虚拟机,公有云中的虚拟机都可以通过一个命令统一进行管理。

01. 在本地虚拟化环境中创建容器化主机

以下操作可以在Windows server 2016 操作系统上执行

docker-machine create -d hyperv --hyperv-virtual-switch "DockerNAT" local-docker01

注意以上命令中使用了–hyperv-virtual-switch 后面的 DockerNAT 对应的是Hyper-v 中的虚拟网络名称,我这里使用的是内部网络,你也可以使用外部网络

 

以上命令的运行结果如下:

Running pre-create checks...                                                                                            
(local-docker01) Image cache directory does not exist, creating it at C:\Users\azureuser\.docker\machine\cache...       
(local-docker01) No default Boot2Docker ISO found locally, downloading the latest release...                            
(local-docker01) Latest release for github.com/boot2docker/boot2docker is v17.11.0-ce                                   
(local-docker01) Downloading C:\Users\azureuser\.docker\machine\cache\boot2docker.iso from https://github.com/boot2docke
r/boot2docker/releases/download/v17.11.0-ce/boot2docker.iso...                                                          
(local-docker01) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%                              
Creating machine...                                                                                                     
(local-docker01) Copying C:\Users\azureuser\.docker\machine\cache\boot2docker.iso to C:\Users\azureuser\.docker\machine\
machines\local-docker01\boot2docker.iso...                                                                              
(local-docker01) Creating SSH key...                                                                                    
(local-docker01) Creating VM...                                                                                         
(local-docker01) Using switch "DockerNAT"                                                                               
(local-docker01) Creating VHD                                                                                           
(local-docker01) Starting VM...                                                                                         
(local-docker01) Waiting for host to start...                                                                           
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...                                                                    
                                                                                                                        
This machine has been allocated an IP address, but Docker Machine could not                                             
reach it successfully.                                                                                                  
                                                                                                                        
SSH for the machine should still work, but connecting to exposed ports, such as                                         
the Docker daemon port (usually <ip>:2376), may not work properly.                                                      
                                                                                                                        
You may need to add the route manually, or use another related workaround.                                              
                                                                                                                        
This could be due to a VPN, proxy, or host file configuration issue.                                                    
                                                                                                                        
You also might want to clear any VirtualBox host only interfaces you are not using.                                     
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 local-docker01                                                                                                           

 

02. 查看docker-machine列表并使用ssh连接

通过docker-machine创建的容器化主机会在你的计算机上保存记录,可以通过docker-machine ls这个命令查询到所有已经连接到当前计算机的容器化主机。

docker-machine ls

显示结果如下:

NAME             ACTIVE   DRIVER   STATE     URL                                    SWARM   DOCKER        ERRORS
local-docker01   -        hyperv   Running   tcp://[fe80::215:5dff:fe00:40e]:2376           v17.11.0-ce

 这些主机的记录保存在 C:\Users\{当前用户}\.docker\machine\machines 目录下,如:C:\Users\azureuser\.docker\machine\machines

该目录下保存了使用ssh key-pair连接这些主机所需要的所有证书和密钥文件,需要妥善保存。同时,这样我们也就可以直接ssh远程登录到这些主机上进行操作了

docker-machine ssh local-docker01

 输出如下:

                      ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.11.0-ce, build HEAD : e620608 - Tue Nov 21 18:11:40 UTC 2017
Docker version 17.11.0-ce, build 1caf76c
docker@local-docker01:~$

  

03. 将docker工具链接到远程docker-machine主机

由于docker工具本身是通过rest api调用远程主机的docker daemon的接口实现操作,我们可以通过修改本地docker命令所链接的远程主机地址的方式来实现操作目标的切换,docker-machine给我们提供了简化的操作命令。通过 docker-machine env {主机名称} 就可以获取这些链接参数并配置docker工具。

docker-machine env local-docker01

输出如下:

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://[fe80::215:5dff:fe00:40e]:2376
SET DOCKER_CERT_PATH=C:\Users\azureuser\.docker\machine\machines\local-docker01
SET DOCKER_MACHINE_NAME=local-docker01
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env local-docker01') DO @%i

 执行下面命令,将本地docker命令链接到了local-docker01这台容器主机上

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

后面的docker ps操作所针对的就是这台主机了。

 docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

  

04. docker-machine 的其他命令

docker-machine还提供很多丰富的命令可以帮助你管理容器化主机的生命周期,如:

删除主机 :
docker-machine rm {主机名}
重新生成证书和密钥,如果远程主机的ip地址发生了变化,我们一般需要使用这个命令才能从新连接:
docker-machine regenerate-certs {主机名}
获取远程主机的ip地址:
docker-machine ip {主机名}
posted @ 2017-11-23 14:56  AutoHome7390  阅读(1957)  评论(0编辑  收藏  举报