主頁 > 知識庫 > docker之docker-machine用法詳解

docker之docker-machine用法詳解

熱門標(biāo)簽:激光標(biāo)記地圖標(biāo)注 百度地圖底圖標(biāo)注 中國地圖標(biāo)注上各個省 新鄉(xiāng)牧野400電話申請 高德地圖標(biāo)注足跡怎么打標(biāo) 撫州市城區(qū)地圖標(biāo)注 企業(yè)辦理400電話收費(fèi)標(biāo)準(zhǔn) 電銷智能機(jī)器人試用 智能電銷機(jī)器人真的有用么

docker-machine 是docker官方提供的docker管理工具。

通過docker-machine可以輕松的做到:

  在Windows平臺和MAC平臺安裝和運(yùn)行docker

  搭建和管理多個docker 主機(jī)

  搭建swarm集群

環(huán)境win下面安裝的virtualbox,virtualbox安裝的centos7,網(wǎng)絡(luò)模式NAT+hostonly

ip:192.168.56.102(hostonly)

1、安裝docker-machine:

curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

2、查看docker-machine版本:

# docker-machine version
[root@docker ~]# docker-machine version
docker-machine version 0.13.0, build 9ba6da9

3、在centos7環(huán)境下創(chuàng)建machine:

[root@localhost ~]# docker-machine create -d virtualbox default
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"

但是卻報錯了,以為virtualbox安裝的centos7環(huán)境支持的是virtualbox驅(qū)動,才發(fā)現(xiàn)環(huán)境安裝支持virtualbox驅(qū)動

使用virtualbox驅(qū)動需要安裝virtualbox,而Ubuntu的解決方法為:

于是采用generic驅(qū)動,具體介紹查看官網(wǎng):https://docs.docker.com/machine/drivers/generic/

[root@localhost ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --

generic-ssh-user=root vm
Running pre-create checks...
Creating machine...
(vm) Importing SSH key...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Error creating machine: Error detecting OS: Too many retries waiting for SSH to be available. Last error: Maximum number of retries (60) exceeded
  --generic-ip-address=192.168.56.102:這里的ip是指本機(jī),如果需要為其他遠(yuǎn)程docker主機(jī)安裝可以改為其他docker主機(jī)ip(這里是本地創(chuàng)建docker-machine)

依然報錯,這是由于docker-machine為本機(jī)創(chuàng)建machine時也需要進(jìn)行ssh認(rèn)證:

[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id root@192.168.56.102

將密碼發(fā)給自己,然后重新繼續(xù)創(chuàng)建machine:

[root@localhost ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=root vm
Running pre-create checks...
Creating machine...
(vm) Importing SSH key...
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 centos...
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 vm

于是終于創(chuàng)建machine成功了

查看docker-machine:

[root@localhost ~]# docker-machine ls 
NAME ACTIVE DRIVER STATE  URL       SWARM DOCKER  ERRORS
vm  -  generic Running tcp://192.168.56.102:2376   v17.09.0-ce 

查看vm的環(huán)境變量:

[root@localhost ~]# docker-machine env vm
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.56.102:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/vm"
export DOCKER_MACHINE_NAME="vm"
# Run this command to configure your shell: 
# eval $(docker-machine env vm)

加載環(huán)境變量:

[root@localhost ~]# eval $(docker-machine env vm)

利用ssh登錄到machine中:

[root@localhost ~]# docker-machine ssh --help
Usage: docker-machine ssh [arg...]
Log into or run a command on a machine with SSH.
Description:
Arguments are [machine-name] [command]
[root@localhost ~]# docker-machine ssh vm
Last login: Sat Nov 4 17:55:53 2017 from 192.168.56.102
[root@vm ~]# 

現(xiàn)在在本地環(huán)境創(chuàng)建一個容器啟動:

[root@localhost ~]# docker run -d --name=nginx nginx
[root@localhost ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS    NAMES
6e62975fab90  nginx    "nginx -g 'daemon ..." About a minute ago Up 59 seconds  80/tcp    nginx

然后ssh遠(yuǎn)程到docker-machine中:

[root@localhost ~]# docker-machine ssh vm   
Last login: Sat Nov 4 18:13:27 2017 from 192.168.56.102
[root@vm ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS    NAMES
6e62975fab90  nginx    "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp    nginx

可以看見docker主機(jī)和docker-machine主機(jī)里面的容器id相同

利用docker-machine能夠安裝docker和創(chuàng)建容器

上面是本地為自己創(chuàng)建machine,現(xiàn)在為遠(yuǎn)程的docker主機(jī)創(chuàng)建docker-machine:
環(huán)境:centos7,192.168.101.14,vmware下面安裝的docker以及docker-machine,為192.168.56.102這臺docker主機(jī)創(chuàng)建machine:
(兩個ip不同進(jìn)行了轉(zhuǎn)發(fā)所以可以訪問(前面是vm下面的nat的靜態(tài)ip,后面是virtualbox的兩張網(wǎng)卡(nat和host only)))

1、首先將192.168.101.14和主機(jī)192.168.56.102進(jìn)行ssh連接認(rèn)證:

[root@docker ~]# ssh-keygen
[root@docker ~]# ssh-copy-id root@192.168.56.102

2、創(chuàng)建machine:

[root@docker ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=root default
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Importing SSH key...
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 centos...
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 default

執(zhí)行環(huán)境變量,進(jìn)入到machine環(huán)境:

[root@docker ~]# docker-machine env default
[root@docker ~]# eval $(docker-machine env default)

3、查看創(chuàng)建的machine:

[root@docker ~]# docker-machine ls
NAME  ACTIVE DRIVER STATE  URL       SWARM DOCKER  ERRORS
default -  generic Running tcp://192.168.56.102:2376   v17.09.0-ce 

可以看見在192.168.101.14環(huán)境上為遠(yuǎn)程主機(jī)192.168.56.102創(chuàng)建的machine

4、創(chuàng)建容器:

[root@docker ~]# docker run -d --name=nginx nginx(本地沒有nginx鏡像)
b1f08986f6d5dbb1ede699e915bde734bab278fbe70f93af06ec2267fae2fef3
[root@docker ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS    NAMES
b1f08986f6d5  nginx    "nginx -g 'daemon ..." 4 seconds ago  Up 3 seconds  80/tcp    nginx
5、ssh到machine:
[root@docker ~]# docker-machine ssh default
Last login: Sat Nov 4 18:51:49 2017 from 192.168.56.1
[root@default ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS    NAMES
b1f08986f6d5  nginx    "nginx -g 'daemon ..." 23 seconds ago  Up 22 seconds  80/tcp    nginx

現(xiàn)在查看遠(yuǎn)程主機(jī)是否創(chuàng)建了容器:

[root@localhost ~]# docker ps -a
could not read CA certificate "/root/.docker/machine/machines/default/ca.pem": open /root/.docker/machine/machines/default/ca.pem: no such file or directory

報錯原因:

由于剛剛在192.168.56.102為自己設(shè)置了machine,保留了之前machine的環(huán)境變量,雖然現(xiàn)在刪除了,但是設(shè)置了環(huán)境變量,將剛剛設(shè)置的環(huán)境變量取消:

[root@localhost ~]# unset DOCKER_TLS_VERIFY
[root@localhost ~]# unset DOCKER_CERT_PATH
[root@localhost ~]# unset DOCKER_MACHINE_NAME
[root@localhost ~]# unset DOCKER_HOST

然后重新查看:

[root@localhost ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS    NAMES
b1f08986f6d5  nginx    "nginx -g 'daemon ..." 8 minutes ago  Up 8 minutes  80/tcp    nginx

可以發(fā)現(xiàn),為遠(yuǎn)程主機(jī)創(chuàng)建容器成功

現(xiàn)在192.168.101.14上面存在鏡像centos_nginx:v4,而遠(yuǎn)程主機(jī)192.168.56.102沒有該鏡像,現(xiàn)在創(chuàng)建容器,看是否遠(yuǎn)程主機(jī)能夠創(chuàng)建成功?

[root@docker ~]# docker pull registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc:v4
[root@docker ~]# docker images
REPOSITORY           TAG     IMAGE ID   CREATED    SIZE
nginx            latest    b72d63324dbb  13 hours ago  108MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4     6c5128aaff05  2 days ago   464MB

然后在遠(yuǎn)程主機(jī)查看:

[root@localhost ~]# docker images
REPOSITORY           TAG     IMAGE ID   CREATED    SIZE
nginx            latest    b72d63324dbb  13 hours ago  108MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4     6c5128aaff05  2 days ago   464MB

可以看見兩主機(jī)的鏡像同步,也是容器也是同步的

vm下面的docker可以為virtualbox下面的docker創(chuàng)建容器
而更多的是本地可以為云等其他環(huán)境創(chuàng)建容器,通過docker-machine

在這之前192.168.101.14的images都不見了,那是因為設(shè)置machine環(huán)境變量:

unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST

執(zhí)行上面將machine的環(huán)境變量取消就可以返回原來的環(huán)境了:

[root@docker ~]# docker images
REPOSITORY           TAG     IMAGE ID   CREATED    SIZE
centos_init          v1     383ff3502443  26 hours ago  448MB
centos_nginx          v8     6f792dc07c35  2 days ago   464MB
centos_nginx          v7     9e875385d6be  2 days ago   464MB
centos_nginx          v6     959fdf4d4288  2 days ago   464MB
centos_nginx          v5     5c1131306686  2 days ago   464MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4     6c5128aaff05  2 days ago   464MB
192.168.101.14:5000/centos_nginx     v4     6c5128aaff05  2 days ago   464MB
centos_nginx          v4     6c5128aaff05  2 days ago   464MB
centos_nginx          v3     0e49a2c0562f  2 days ago   464MB
centos_nginx          v2     2031faf8894a  2 days ago   464MB
centos_nginx          v1     78d18f16e757  3 days ago   464MB
registry           latest    2ba7189700c8  9 days ago   33.3MB
ubuntu            latest    747cb2d60bbe  3 weeks ago   122MB
centos            latest    196e0ce0c9fb  7 weeks ago   197MB

而如果需要返回machine環(huán)境就繼續(xù)執(zhí)行machine環(huán)境變量就行,這種方式很好的隔離了本地和遠(yuǎn)程鏡像和容器

到此這篇關(guān)于docker之docker-machine用法詳解的文章就介紹到這了,更多相關(guān)docker machine 用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

標(biāo)簽:延安 邯鄲 臨汾 西安 南通 海西 辛集 忻州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《docker之docker-machine用法詳解》,本文關(guān)鍵詞  docker,之,docker-machine,用法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《docker之docker-machine用法詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于docker之docker-machine用法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章