參考:
https://github.com/docker/machine/
https://docs.docker.com/machine/overview/
http://lizhenliang.blog.51cto.com/7876557/1730028
https://www.oschina.net/news/50591/docker-0-10
http://www.csdn.net/article/2015-08-11/2825438
概述
官方概述3點:
- Install and run Docker on Mac or Windows
- Provision and manage multiple remote Docker hosts
- Provision Swarm clusters
如果是windows和macos的用戶,便默認(rèn)已經(jīng)帶有docker-machine了,linux用戶用它主要來做集群管理,管理docker等一系列的操作工具。
docker machine 是一個簡化的Docker安裝的命令工具,通過簡單命令來進行響應(yīng)平臺的docker安裝,比如virtualbox,microsoft azure。舉個如果你有多個操作系統(tǒng),ubuntu windows centos macos,那么我都需要按照官網(wǎng)文檔上的來一步一步的學(xué)習(xí)安裝,不同的操作系統(tǒng)安裝步驟也都不一樣,所以有了machine以后,它通過ssh來鏈接到主機,我們只需要幾個簡單的命令就可以讓主機安裝docker,這對集群來說是非常方便的。
簡單來說docker解決了一下幾個問題
- Docker Machine簡化了部署的復(fù)雜度,無論是在本機的虛擬機上還是在公有云平臺,只需要一條命令便可搭建好Docker主機
- Docker Machine提供了多平臺多Docker主機的集中管理
- Docker Machine 使應(yīng)用由本地遷移到云端變得簡單,只需要修改一下環(huán)境變量即可和任意Docker主機通信部署應(yīng)用
安裝docker-machine
安裝過程請參考:https://github.com/docker/machine/
在安裝過程中,可能會遇到,curl下載不下來的情況,所以我們可以自己去git上去下載,選擇自己得操作系統(tǒng)版本
我們可以使用windows翻墻或者用代理的方式去下載好machine傳入給linux操作系統(tǒng)
后面發(fā)現(xiàn),之所以慢,原來。。。。
在我翻墻或者使用迅雷下載以后
這個時候我們使用 docker-machine 命令就可以查看到此工具的幫助信息了
附加(安裝docker-machine bash自動完成腳本)
- 命令自動完成
- 在shell提示符中顯示活動的機器
- 一個函數(shù)包裝器,它添加了docker-machine use子命令來切換活動的計算機
https://github.com/docker/machine/tree/master/contrib/completion/bash
Docker machine運行原理
這張圖是官網(wǎng)介紹圖,docker-machine是更方便集群管理的,就和你得服務(wù)器管理員一樣,無論是做監(jiān)控還是做操作,通過docker-machine便可以去管理一片docker主機,在使用過程中,我們可以去使用virtualbox來安裝多個虛擬機器來安裝docker,并且使用docker-machine來管理這些機器
命令解析
使用Docker Machine和本地虛擬機的docker主機
https://docs.docker.com/machine/get-started/
如果是linux的用戶,在我們已經(jīng)確保服務(wù)器含有 VirtualBox 軟件后(如果沒有裝virtualbox我們使用<code>#apt install virtualbox</code>), 我們可以直接安裝docker提供的一個 Boot2Docker,Boot2Docker是一個專門用于運行Docker容器的輕量級Linux發(fā)行版,這樣可以保證我們的本地電腦資源盡可能的少被我們不需要的程序浪費掉。
Docker-machine在創(chuàng)建一個虛擬主機的時候會直接去下載boot2docker。
- 初探運行一個包含docker的虛擬主機
如果遇到如下問題
rror with pre-create check: "Get https://github-
cloud.s3.amazonaws.com/releases/14930729/2a7d9b36-1aba-11e7-951b-a7c27f5bb4fd.iso?X-
Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-
Credential=AKIAISTNZFOVBIJMK3TQ%2F20170414%2Fus-east-1%2Fs3%2Faws4_request&X-
Amz-Date=20170414T013059Z&X-Amz-Expires=300&X-Amz-
Signature=b4fb1737c6bc10503dec6f45e0bf6de269336fdf1f939eca6657fa207cb4c87b&X-Amz-
SignedHeaders=host&actor_id=0&response-content-
disposition=attachment%3B%20filename%3Dboot2docker.iso&response-content-
type=application%2Foctet-stream: dial tcp 52.216.64.200:443: getsockopt: connection refused"
我們可以手動的去下載此鏡像,然后放到docker-machine能找到的地方
為了確保你不重復(fù)工作,請下載最新的boot2docker版本
安裝Boot2Docker
https://github.com/boot2docker/boot2docker/releases首秀測試
root@ubuntu-docker:/home# docker-machine create -d virtualbox mh-keystore
Running pre-create checks...
Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"
我的ubuntu-docker是運行在windows10宿主機上的Vmware里面的一個Ubuntu16的操作系統(tǒng)。下面是我查詢到的一些問題,解決方案就是在vmware當(dāng)中開啟虛擬化引擎就可以了
https://github.com/docker/machine/issues/2256
接著測試,如果出現(xiàn)以下結(jié)果,就說明virtualbox安裝成功了
root@ubuntu-docker:~/.docker/machine/cache# docker-machine create -d virtualbox mh-keystore
Running pre-create checks...
Creating machine...
(mh-keystore) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/mh-keystore/boot2docker.iso...
(mh-keystore) Creating VirtualBox VM...
(mh-keystore) Creating SSH key...
(mh-keystore) Starting the VM...
(mh-keystore) Check network to re-create if needed...
(mh-keystore) Found a new host-only adapter: "vboxnet0"
(mh-keystore) 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 mh-keystore
這樣我們就創(chuàng)建了一個新的含有docker虛擬主機的環(huán)境
不得不說我的環(huán)境確實奇葩(windows10宿主機>Vmware(Ubuntu Linux)>VirtualBox(boot2docker))
docker-machine的詳細(xì)使用
在我們已經(jīng)搭建docker-machine要使用的virtual-box和boot2docker后,我們已經(jīng)成功的運行了一個虛擬的主機操作系統(tǒng)并且已經(jīng)內(nèi)置了docker
-
1.查看已經(jīng)啟動的機器
image.png - 2.查看主機上的環(huán)境
root@ubuntu-docker:/# docker-machine env mh-keystore
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/mh-keystore"
export DOCKER_MACHINE_NAME="mh-keystore"
# Run this command to configure your shell:
# eval $(docker-machine env mh-keystore)
- 3.鏈接到主機的shell,如果沒有任何的報錯,就說明鏈接成功了
#eval "$(docker-machine env my-machine)"
- 4.查看主機的IP地址
$ docker run -d -p 8000:80 nginx
案例,在主機my-host中創(chuàng)建一個nginx的webserver