docker-machine 是docker官方提供的docker管理工具。
經過docker-machine能夠輕鬆的作到:nginx
在Windows平臺和MAC平臺安裝和運行dockergit
搭建和管理多個docker 主機github
搭建swarm集羣docker
環境win下面安裝的virtualbox,virtualbox安裝的centos7,網絡模式NAT+hostonlyshell
ip:192.168.56.102(hostonly)json
一、安裝docker-machine:ubuntu
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
二、查看docker-machine版本:centos
# docker-machine version [root@docker ~]# docker-machine version docker-machine version 0.13.0, build 9ba6da9
三、在centos7環境下建立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環境支持的是virtualbox驅動,才發現環境安裝支持virtualbox驅動ssh
因而採用generic驅動,具體介紹查看官網: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
[root@localhost ~]# ssh-keygen [root@localhost ~]# ssh-copy-id root@192.168.56.102
將密碼發給本身,而後從新繼續建立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
因而終於建立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
[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)
[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 ~]#
[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
[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
[root@docker ~]# ssh-keygen [root@docker ~]# ssh-copy-id root@192.168.56.102
[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
執行環境變量,進入到machine環境:
[root@docker ~]# docker-machine env default [root@docker ~]# eval $(docker-machine env default)
[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環境上爲遠程主機192.168.56.102建立的machine
[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
[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
如今查看遠程主機是否建立了容器:
[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
報錯緣由:
[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
能夠發現,爲遠程主機建立容器成功
[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
[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
能夠看見兩主機的鏡像同步,也是容器也是同步的
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST
執行上面將machine的環境變量取消就能夠返回原來的環境了:
[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環境就繼續執行machine環境變量就行,這種方式很好的隔離了本地和遠程鏡像和容器