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 {主機名}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM