docker-machine是什么?

ocker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。

Docker 与 Docker Machine 的区别

Docker 是一个 Client-Server 架构的应用,Docker是大家对Docker Engine简称。

Docker包括以下三个部分:

  • 1、Docker daemon
  • 2、一套与 Docker daemon 交互的 REST API
  • 3、一个命令行客户端

    下图很清晰的展示了它们之间的关系:

Docker Machine 则是一个安装和管理 Docker 的工具。它有自己的命令行工具:docker-machine。

docker-machine安装

[root@master ~]# curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 390 0 --:--:-- 0:00:01 --:--:-- 390
100 26.7M 100 26.7M 0 0 1618k 0 0:00:16 0:00:16 --:--:-- 3622k
[root@master ~]# chmod +x /tmp/docker-machine
[root@master ~]# cp /tmp/docker-machine /usr/local/bin/docker-machine
[root@master ~]# ll /usr/local/bin/docker-machine
-rwxr-xr-x 1 root root 28034848 6月 12 15:24 /usr/local/bin/docker-machine
[root@master ~]# docker-machine -v
docker-machine version 0.14.0, build 89b8332

docker-machine命令

[root@master ~]# docker-machine --help
Usage: docker-machine [OPTIONS] COMMAND [arg...] Create and manage machines running Docker. Version: 0.14.0, build 89b8332 Author:
Docker Machine Contributors - <https://github.com/docker/machine> Options:
--debug, -D Enable debug mode
--storage-path, -s "/root/.docker/machine" Configures storage path [$MACHINE_STORAGE_PATH]
--tls-ca-cert CA to verify remotes against [$MACHINE_TLS_CA_CERT]
--tls-ca-key Private key to generate certificates [$MACHINE_TLS_CA_KEY]
--tls-client-cert Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
--tls-client-key Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
--github-api-token Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
--native-ssh Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
--bugsnag-api-token BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
--help, -h show help
--version, -v print the version Commands:
active Print which machine is active
config Print the connection config for machine
create Create a machine
env Display the commands to set up the environment for the Docker client
inspect Inspect information about a machine
ip Get the IP address of a machine
kill Kill a machine
ls List machines
provision Re-provision existing machines
regenerate-certs Regenerate TLS Certificates for a machine
restart Restart a machine
rm Remove a machine
ssh Log into or run a command on a machine with SSH.
scp Copy files between machines
mount Mount or unmount a directory from a machine with SSHFS.
start Start a machine
status Get the status of a machine
stop Stop a machine
upgrade Upgrade a machine to the latest version of Docker
url Get the URL of a machine
version Show the Docker Machine version or a machine docker version
help Shows a list of commands or help for one command docker-machine active
显示当前的活动主机 docker-machine config
显示连接主机的配置 docker-machine create
创建一个主机 docker-machine env
设置当前的环境与哪个主机通信 docker-machine inspect
查看主机的详细信息 docker-machine ip
查看主机的IP docker-machine kill
强制关闭一个主机 docker-machine ls
查看所有的主机信息 docker-machine provision
重新配置现在主机 docker-machine regenerate-certs
为主机重新生成证书 docker-machine restart
重启主机 docker-machine rm
删除主机 docker-machine ssh
以SSH的方式连接到主机上 docker-machine scp
远程复制 docker-machine status
查看主机的状态 docker-machine stop
停止一个正在运行的主机 docker-machine upgrade
升级主机的docker服务到最新版本 docker-machine version
查看docker-machine版本

常用实例操作

[root@master ~]# docker-machine create -d virtualbox testhost
[root@master ~]# docker-machine create --driver virtualbox testhost
#两个命令的作用相同,创建一个名为testhost的主机,驱动方式是virtualbox [root@master ~]# docker-machine create -d virtualbox testhost
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"
#报错提示没有发现VBoxManage。因此,需要手工安装,具体安装操作如下。 [root@master ~]# vim /etc/yum.repos.d/virtualbox.repo
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc [root@master ~]# yum search VirtualBox #查找具体安装版本
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirrors.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
============== N/S matched: VirtualBox ===================================
VirtualBox-4.3.x86_64 : Oracle VM VirtualBox
VirtualBox-5.0.x86_64 : Oracle VM VirtualBox
VirtualBox-5.1.x86_64 : Oracle VM VirtualBox
VirtualBox-5.2.x86_64 : Oracle VM VirtualBox
[root@master ~]# yum install -y VirtualBox-5.2 #安装 [root@master ~]# /sbin/vboxconfig #重新加载virtualbox服务
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
kernel-devel kernel-devel-3.10.0-693.el7.x86_64
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
kernel-devel kernel-devel-3.10.0-693.el7.x86_64 There were problems setting up VirtualBox. To re-start the set-up process, run
/sbin/vboxconfig
as root.
#如果内核版本不一致,会出现上面的报错,需要安装相同的内核版本 [root@master ~]# rpm -ivh kernel-devel-3.10.0-693.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:kernel-devel-3.10.0-693.el7 ################################# [100%]
[root@slave1 ~]# yum install gcc make perl -y
[root@slave1 ~]# rpm -qa kernel\*
kernel-tools-3.10.0-693.el7.x86_64
kernel-devel-3.10.0-693.el7.x86_64
kernel-tools-libs-3.10.0-693.el7.x86_64
kernel-3.10.0-693.el7.x86_64
kernel-headers-3.10.0-862.3.2.el7.x86_64
[root@slave1 ~]# /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services. [root@centos7 ~]# docker-machine create --driver virtualbox testhost
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"
#这个报错不用解释了吧,没有开启虚拟化功能,直接打开 [root@centos7 ~]# docker-machine create --driver virtualbox default
Running pre-create checks...
(default) No default Boot2Docker ISO found locally, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v18.05.0-ce
(default) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.05.0-ce/boot2docker.iso...
(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet0"
(default) 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 default
#可以先安装个默认的虚拟机
[root@centos7 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.100:2376 v18.05.0-ce
[root@centos7 ~]# docker-machine status
Running
#接下来配置环境变量,方便后面直接操作主机
[root@centos7 ~]# docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)
[root@centos7 ~]# eval $(docker-machine env default)
[root@centos7 ~]# docker-machine ssh default
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.05.0-ce, build HEAD : b5d6989 - Thu May 10 16:35:28 UTC 2018
Docker version 18.05.0-ce, build f150324 docker@default:~$ docker --version
Docker version 18.05.0-ce, build f150324
#正常进入虚拟机

实例

两台服务器

  • 本地主机:192.168.22.177
  • 远程主机:192.168.22.175

    可能大家还是不太明白,这个docker-machine到底有什么用?docker容器的好处都知道了。

    而docker-machine可以在本地布署相应环境的同时完成远程docker主机相同环境的布署,减少重复的操作。

1、配置免密登录

[root@test001 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7Ue0bc08pEs+PonJqy7/hyxUhNO4uEegVizX4EJC8J8 root@centos7.3
The key's randomart image is:
+---[RSA 2048]----+
| .oo ...o + |
| . o..= = o |
| . .=.o + . . |
| .oo. + o oo+ |
| .E S o ooo.+|
| . + .o.. .|
| o + =+. |
| . . B.+. |
| +++oo.. |
+----[SHA256]-----+
[root@centos7 ~]# ssh-copy-id 192.168.22.175
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.22.175 (192.168.22.175)' can't be established.
ECDSA key fingerprint is SHA256:p6+FPeTxTUx37cwJWJP8cUE9NhcUHSvAppVPyj4aj8c.
ECDSA key fingerprint is MD5:89:6d:f7:46:11:45:2e:fd:21:87:42:bd:62:06:fe:fd.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.22.175's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.22.175'"
and check to make sure that only the key(s) you wanted were added. [root@test001 ~]# ssh 192.168.22.175
Last login: Tue Apr 24 06:51:06 2018 from 192.168.22.170
[root@test002 ~]# ip add |grep 192.168.22
inet 192.168.22.175/24 brd 192.168.22.255 scope global ens32

2、创建远程主机

远程主机需要安装有docker环境
[root@test001 ~]# docker-machine create -d generic --generic-ip-address=192.168.22.175 --generic-ssh-user=root --engine-registry-mirror http://ef017c13.m.daocloud.io dockerhost
Running pre-create checks...
Creating machine...
(dockerhost) No SSH key specified. Assuming an existing key at the default location.
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 dockerhost
[root@test001 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dockerhost - generic Running tcp://192.168.22.175:2376 v1.13.1

3、配置环境变量

[root@centos7 ~]# docker-machine env dockerhost
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.22.175:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/dockerhost"
export DOCKER_MACHINE_NAME="dockerhost"
# Run this command to configure your shell:
# eval $(docker-machine env dockerhost)
[root@test001 ~]# eval $(docker-machine env dockerhost)
[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:32:42 2018 from 192.168.22.177
[root@dockerhost ~]# docker --version
Docker version 1.13.1, build 94f4240/1.13.1

4、运行一个容器

[root@test001 ~]# docker run -d nginx:1.13
Unable to find image 'nginx:1.13' locally
Trying to pull repository docker.io/library/nginx ...
sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35: Pulling from docker.io/library/nginx
f2aa67a397c4: Pull complete
3c091c23e29d: Pull complete
4a99993b8636: Pull complete
Digest: sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
Status: Downloaded newer image for docker.io/nginx:1.13
72efb659ec38d263519c894bf0b5eb3d5ca35af1e3d0e9522abbcc19d8739403
[root@test001 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx 1.13 ae513a47849c 6 weeks ago 109 MB
[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:58:51 2018 from 192.168.22.177
[root@dockerhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx 1.13 ae513a47849c 6 weeks ago 109 MB

©著作权归作者所有:来自作者民工哥的原创作品,如需转载,请注明出处,否则将追究法律责任

容器技术|Docker三剑客之docker-machine的更多相关文章

  1. Docker(五):Docker 三剑客之 Docker Machine

    上篇文章Docker(四):Docker 三剑客之 Docker Compose介绍了 Docker Compose,这篇文章我们来了解 Docker Machine . Docker Machine ...

  2. Docker 三剑客之 Docker Swarm

    上一篇:Docker 三剑客之 Docker Compose 阅读目录: Docker Machine 创建 Docker 主机 Docker Swarm 配置集群节点 Docker Service ...

  3. Docker 三剑客之 Docker Swarm(基于 overlay 组网通信)

    相关文章:Docker 三剑客之 Docker Swarm 这一篇主要是对 Docker Swarm 的完善,增加基于 overlay 组网通信,以便 Docker 容器可以跨主机访问. 不同主机间的 ...

  4. Docker三剑客之Docker Machine

    一.什么是Docker Machine Docker Machine 是Docker官方编排项目之一,使用go语言编写的,使用不同引擎在多种平台上快速的安装Docker环境,开源地址:https:// ...

  5. GIS+=地理信息+容器技术(4)——Docker执行

    -------------------------------------------------------------------------------------- Blog:    http ...

  6. docker——三剑客之Docker Machine

    Docker Machine是Docker官方三剑客项目之一,负责使用Docker的第一步,在多种平台上快速安装Docker环境.它支持多种平台,让用户在很短时间内搭建一套Docker主机集群. Ma ...

  7. Docker(六):Docker 三剑客之 Docker Swarm

    实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...

  8. docker——三剑客之Docker swarm

    Docker Swarm是Docker官方的三剑客项目之一,提供Docker容器集群服务,是Docker官方对容器云生态进行支持的核心方案.使用它,用户可以将多个Docker主机封装为单个大型的虚拟D ...

  9. Docker(四):Docker 三剑客之 Docker Compose

    前两篇文章我们介绍了 Dockerfile 的使用Docker(二):Dockerfile 使用介绍,我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就 ...

  10. Docker三剑客之Docker Swarm

    一.什么是Docker Swarm Swarm是Docker公司推出的用来管理docker集群的平台,几乎全部用GO语言来完成的开发的,代码开源在https://github.com/docker/s ...

随机推荐

  1. git 入门教程之撤销更改

    撤销更改 相信你已经了解了 git 的基本概念,也清楚了工作区,暂存区和版本库的关系,现在让我们用所学的知识继解决实际问题吧! 背景 正常看得见的目录是我们最为熟悉的工作区,在工作中不可能总是100% ...

  2. python 正则表达式中反斜杠(\)的麻烦和陷阱

    这里是一点小心得:由于下面两个原因,在正则表达式中使用反斜杠就会产生了一个双重转换的问题. (1).python自身处理字符串时,反斜杠是用于转义字符 (2).正则表达式也使用反斜杠来转义字符     ...

  3. Spring从认识到细化了解

    目录 Spring的介绍 基本运行环境搭建 IoC 介绍: 示例使用: 使用说明: 使用注意: Bean的实例化方式 Bean的作用范围的配置: 补充: DI: 属性注入: 补充: IoC的注解方式: ...

  4. SQL server分离和附加数据库

    下文是参考网友的文章结合自身的经验来总结的 应用于将数据库更改到同一计算机或不同计算机的不同SQL server实例中,或者要移动数据库文件存储的位置时. 一.分离数据库 将数据库从SQL serve ...

  5. MySQL8.0关于caching_sha2_password Plugin的一个Bug

    今天在调试使用ansible进行标准化安装MySQL8.0时,发现关于caching_sha2_password plugin的一个bug. 在搭建主从复制时,按照手册说明需要创建用户: create ...

  6. 基数排序python实现

    基数排序python实现 基数排序 基数排序(英语:Radix sort)是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较.由于整数也可以表达字符串(比如名字或 ...

  7. [Hive_5] Hive 的 JDBC 编程

    0. 说明 Hive 的 JDBC 编程 1. hiveserver2 介绍 hiveserver2 是 Hive 的 JDBC 接口,用户可以连接此端口来连接 Hive 服务器 JDBC 驱动类为 ...

  8. web测试笔记

    WEB兼容性测试 一.客户端兼容性 1.浏览器的兼容性测试 a.内核角度 Tridnt内核:代表作IE.腾讯.遨游.世界之窗等 Gecko内核:代表作Firefox webkit内核:代表作Safar ...

  9. ASP.NET -- WebForm -- .aspx与.aspx.cs文件

    ASP.NET -- WebForm --  .aspx与.aspx.cs文件 1. ASP.NET -- WebForm(C#)文件 .aspx文件:是Html页面,页面的布局,样式在该文件中设计. ...

  10. GitHub-分支管理03-多人合作【重点】

    参考博文:廖雪峰Git教程 1. 多人协作 当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信 ...