前言

你可以用docker machine来做什么?
1,在mac和windows平台上安装和运行docker
2,提供和管理多个远程docker主机
3,提供swarm集群管理

安装

安装很简单,直接从github下载编译好的二进制文件即可,也可以参考官网详细介绍
下载地址:https://github.com/docker/machine/releases

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

mac客户端已经包含了machine,其实无需单独安装。

1
2
curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine && 
chmod +x /usr/local/bin/docker-machine

查看machine安装版本,可以正常输出版本信息即可。

1
docker-machine -v

 

使用

1,创建主机

1,创建主机节点,不同的服务器,可能支持的驱动不同,所以要按需处理

Centos

直接选择virtualbox类型的驱动来创建主机:

1
docker-machine create -d virtualbox test
ECS-Centos

需要使用阿里云提供的驱动ECS Driver来创建, 参见:驱动下载和安装文档

安装驱动:

1
2
3
4
5
6
7
8
9
10
11
mkdir docker-machine

cd docker-machine

wget https://docker-machine-drivers.oss-cn-beijing.aliyuncs.com/docker-machine-driver-aliyunecs_linux-amd64.tgz

mv docker-machine-driver-aliyunecs_linux-amd64.tgz driver-aliyunecs.tgz

tar -zxvf driver-aliyunecs.tgz

cp bin/docker-machine-driver-aliyunecs.linux-amd64 /usr/local/bin/docker-machine-driver-aliyunecs

使用阿里云驱动aliyunecs,创建创建主机时,aliyunecs-access-key-idaliyunecs-access-key-secret 是必填参数,可以直接在aliyunecs后追加,也可以设置到环境变量当中。

设置环境变量:

1
2
export ECS_ACCESS_KEY_ID='<Your access key ID>'
export ECS_ACCESS_KEY_SECRET='<Your secret access key>'

创建主机:

1
docker-machine create -d aliyunecs testali

命令参数参考: 点我查看命令详细介绍  

Mac

使用xhyve驱动,初次使用得先安装:

1
2
3
4
5
brew install docker-machine-driver-xhyve

sudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

sudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

如果mac上没有装brew工具,可以通过下面的命令安装brew:

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

常用参数(更多命令查看:“docker-machine create -d xhyve -help”):

1
2
3
4
配置docker主机镜像文件地址:--xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso
配置Docker的仓库镜像:--engine-registry-mirror https://registry.docker-cn.com
配置主机内存:--virtualbox-memory 2048
配置主机CPU:--virtualbox-cpu-count 2

创建主机:

1
docker-machine create -d xhyve --xhyve-boot2docker-url ~/.docker/machine/cache/boot2docker.iso --engine-registry-mirror https://registry.docker-cn.com test
2,查询主机
1
docker-machine ls

输出如下即可:

1
2
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
test - xhyve Running tcp://192.168.64.2:2376 v18.06.1-ce
3,连接主机

尝试登录ssh连接到docker-machine主机:

1
docker-machine ssh test

a,查看主机的docker版本

1
docker -v

b,尝试运行容器,以portainer为例

1
docker run -d -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer

c,先使用命令:exit 退出主机,然后查看IP,并尝试在主机浏览器上访问启动的portainer容器

1
docker-machine ip test

浏览器访问:http://ip:9000 ,能正常访问即可!  

报错处理

做什么事情不遇到些错误是不可能的,这次是踩了不少坑,记录下来,方便下次审阅。

错误1:

我是在阿里云ECS上使用的时候提示的错误:

1
_VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path_ 

解决办法:
配置安装源,安装VirtualBox

1
vi /etc/yum.repos.d/virtualbox.repo

内容如下:

1
2
3
4
5
6
7
[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

使用命令查看VirtualBox版本,选择指定版本进行安装

1
yum search VirtualBox

输出:

1
2
3
4
5
_=========================== 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_

安装命令:

1
2
yum install -y VirtualBox-5.2
sudo /sbin/vboxconfig

错误2:
在执行命令“sudo /sbin/vboxconfig”时,提示如下:

1
2
3
4
5
6
_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.2.2.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.2.2.el7.x86_64

解决方法:

1
2
yum update kernel -y
yum install kernel-headers kernel-devel gcc make -y

重启服务器:

1
init 6

错误3:
在执行命令“docker-machine create -d virtualbox test”时,提示错误:

1
2
3
_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"_

解决方法:
阿里云ECS不支持,无法开启,可以更换为阿里云提供的驱动aliyunecs

错误4:
在执行命令“docker-machine create -d aliyunecs testali”时,提示错误:

1
2
Driver "aliyunecs" not found. 
Do you have the plugin binary "docker-machine-driver-aliyunecs" accessible in your PATH?

解决方法:
找不到对应名称的文件,所以,在拷贝到/usr/local/bin目录之前,先修改文件名称,去掉.linux-amd64,如下:

1
docker-machine-driver-aliyunecs.linux-amd64 >> docker-machine-driver-aliyunecs 

错误5:
执行创建主机的时候提示:

1
2
Error setting machine configuration from flags provided: testali | aliyunecs driver 
requires the --aliyunecs-access-key-id option_

解决方法:
aliyunecs-access-key-idaliyunecs-access-key-secret是必填参数,可以直接在aliyunecs后追加,也可以设置到环境变量当中