參考:
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點(diǎn):
- 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 是一個(gè)簡(jiǎn)化的Docker安裝的命令工具,通過簡(jiǎn)單命令來進(jìn)行響應(yīng)平臺(tái)的docker安裝吭产,比如virtualbox侣监,microsoft azure。舉個(gè)如果你有多個(gè)操作系統(tǒng)臣淤,ubuntu windows centos macos橄霉,那么我都需要按照官網(wǎng)文檔上的來一步一步的學(xué)習(xí)安裝,不同的操作系統(tǒng)安裝步驟也都不一樣邑蒋,所以有了machine以后酪劫,它通過ssh來鏈接到主機(jī),我們只需要幾個(gè)簡(jiǎn)單的命令就可以讓主機(jī)安裝docker寺董,這對(duì)集群來說是非常方便的。
簡(jiǎn)單來說docker解決了一下幾個(gè)問題
- Docker Machine簡(jiǎn)化了部署的復(fù)雜度刻剥,無論是在本機(jī)的虛擬機(jī)上還是在公有云平臺(tái)遮咖,只需要一條命令便可搭建好Docker主機(jī)
- Docker Machine提供了多平臺(tái)多Docker主機(jī)的集中管理
- Docker Machine 使應(yīng)用由本地遷移到云端變得簡(jiǎn)單,只需要修改一下環(huán)境變量即可和任意Docker主機(jī)通信部署應(yīng)用
安裝docker-machine
安裝過程請(qǐng)參考:https://github.com/docker/machine/
在安裝過程中造虏,可能會(huì)遇到,curl下載不下來的情況,所以我們可以自己去git上去下載御吞,選擇自己得操作系統(tǒng)版本
我們可以使用windows翻墻或者用代理的方式去下載好machine傳入給linux操作系統(tǒng)
后面發(fā)現(xiàn),之所以慢,原來漓藕。陶珠。。享钞。
在我翻墻或者使用迅雷下載以后
這個(gè)時(shí)候我們使用 docker-machine 命令就可以查看到此工具的幫助信息了
附加(安裝docker-machine bash自動(dòng)完成腳本)
- 命令自動(dòng)完成
- 在shell提示符中顯示活動(dòng)的機(jī)器
- 一個(gè)函數(shù)包裝器揍诽,它添加了docker-machine use子命令來切換活動(dòng)的計(jì)算機(jī)
https://github.com/docker/machine/tree/master/contrib/completion/bash
Docker machine運(yùn)行原理
這張圖是官網(wǎng)介紹圖,docker-machine是更方便集群管理的,就和你得服務(wù)器管理員一樣暑脆,無論是做監(jiān)控還是做操作渠啤,通過docker-machine便可以去管理一片docker主機(jī),在使用過程中添吗,我們可以去使用virtualbox來安裝多個(gè)虛擬機(jī)器來安裝docker沥曹,并且使用docker-machine來管理這些機(jī)器
命令解析
使用Docker Machine和本地虛擬機(jī)的docker主機(jī)
https://docs.docker.com/machine/get-started/
如果是linux的用戶,在我們已經(jīng)確保服務(wù)器含有 VirtualBox 軟件后(如果沒有裝virtualbox我們使用<code>#apt install virtualbox</code>)碟联, 我們可以直接安裝docker提供的一個(gè) Boot2Docker妓美,Boot2Docker是一個(gè)專門用于運(yùn)行Docker容器的輕量級(jí)Linux發(fā)行版,這樣可以保證我們的本地電腦資源盡可能的少被我們不需要的程序浪費(fèi)掉鲤孵。
Docker-machine在創(chuàng)建一個(gè)虛擬主機(jī)的時(shí)候會(huì)直接去下載boot2docker壶栋。
- 初探運(yùn)行一個(gè)包含docker的虛擬主機(jī)
如果遇到如下問題
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"
我們可以手動(dòng)的去下載此鏡像,然后放到docker-machine能找到的地方
為了確保你不重復(fù)工作裤纹,請(qǐng)下載最新的boot2docker版本
安裝Boot2Docker
https://github.com/boot2docker/boot2docker/releases首秀測(cè)試
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是運(yùn)行在windows10宿主機(jī)上的Vmware里面的一個(gè)Ubuntu16的操作系統(tǒng)委刘。下面是我查詢到的一些問題,解決方案就是在vmware當(dāng)中開啟虛擬化引擎就可以了
https://github.com/docker/machine/issues/2256
接著測(cè)試鹰椒,如果出現(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)建了一個(gè)新的含有docker虛擬主機(jī)的環(huán)境
不得不說我的環(huán)境確實(shí)奇葩(windows10宿主機(jī)>Vmware(Ubuntu Linux)>VirtualBox(boot2docker))
docker-machine的詳細(xì)使用
在我們已經(jīng)搭建docker-machine要使用的virtual-box和boot2docker后,我們已經(jīng)成功的運(yùn)行了一個(gè)虛擬的主機(jī)操作系統(tǒng)并且已經(jīng)內(nèi)置了docker
-
1.查看已經(jīng)啟動(dòng)的機(jī)器
image.png - 2.查看主機(jī)上的環(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.鏈接到主機(jī)的shell,如果沒有任何的報(bào)錯(cuò)漆际,就說明鏈接成功了
#eval "$(docker-machine env my-machine)"
- 4.查看主機(jī)的IP地址
$ docker run -d -p 8000:80 nginx
案例淆珊,在主機(jī)my-host中創(chuàng)建一個(gè)nginx的webserver