简介与安装

Docker Machine项目基于 Go 语言实现,Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在虚拟机中安装 Docker。我们还可以通过 docker-machine命令来管理这些虚拟机和 Docker。使用的场景应该是比较少的在生产中。

安装方法详见 https://docs.docker.com/machine/install-machine/:

 

1
2
3
4
5
6
7
8
9
#安装
curl -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine
chmod +x /usr/local/bin/docker-machine
#命令补全
base=https://raw.githubusercontent.com/docker/machine/v0.16.0
for i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash
do
  sudo wget "$base/contrib/completion/bash/${i}" -P /etc/bash_completion.d
done

创建本地虚拟主机+安装docker

由于在虚拟机上面做的实验,在虚拟机上面再开启虚拟化,行不通,可以放弃。

如果有环境的话,由于docker machine要使用到virtualbox的驱动,所以必须先安装这个虚拟化工具。

1
2
3
4
5
6
7
yum update
yum install binutils qt gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms
wget http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc
rpm --import oracle_vbox.asc
cd /etc/yum.repos.d/
wget http://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo
yum install VirtualBox-6.0.x86_64

安装完成之后需要reboot一下,然后使用/sbin/vboxconfig来配置。其他设置请参考:https://www.tecmint.com/install-virtualbox-on-redhat-centos-fedora/

完成之后,就可以使用 docker-machine create -d virtualbox test来创建本地环境的虚拟机,同时会自动安装上docker工具。

https://cloud.tencent.com/developer/article/1481575

在远程主机上面安装docker

部署密钥

由于需要在远程主机上面安装软件,必须要有相应的权限,所以需要配置免密码登陆,就是使用公私钥来登陆了。使用 ssh-keygen -t rsa 来生成密钥,再使用ssh-copy-id把公钥复制到另一台机器上,比如我这台是192.168.137.55,添加完密钥之后,直接ssh + ip查看是否有成功登陆了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@localhost ~]# 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:
。。。
[root@localhost ~]# ll .ssh/ -ah
total 12K
drwx------. 2 root root   38 Oct  5 03:30 .
dr-xr-x---. 7 root root 4.0K Oct  5 03:30 ..
-rw-------. 1 root root 1.7K Oct  5 03:30 id_rsa
-rw-r--r--. 1 root root  408 Oct  5 03:30 id_rsa.pub
[root@localhost ~]# ssh-copy-id root@192.168.137.55
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.137.55's password:
Permission denied, please try again.
root@192.168.137.55's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.137.55'"
and check to make sure that only the key(s) you wanted were added.

部署远端docker

完成上述操作之后,再运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# docker-machine create -d generic --generic-ip-address 192.168.137.55 remote
Running pre-create checks...
Creating machine...
(remote) 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 remote

注意,create 命令本是要创建虚拟主机并安装 Docker,因为本例中的目标主机已经存在,所以仅安装 Docker。

-d 是 —driver 的简写形式,主要用来指定使用什么驱动程序来创建目标主机。Docker Machine 支持在云服务器上创建主机,就是靠使用不同的驱动来实现了。本例中使用 generic 就可以了。

remote这个只是一个名字而也。

稍等几分钟,另一台机器上面就会完成docker工具的安装。我们可以通过 Docker Machine 的 ls 命令查看当前可管理的主机列表:

1
2
3
[root@localhost ~]# docker-machine ls
NAME     ACTIVE   DRIVER    STATE     URL                         SWARM   DOCKER     ERRORS
remote   -        generic   Running   tcp://192.168.137.55:2376           v19.03.2

管理远端的docker

有2个方法。

docker-machine env

运行docker-machine env + name就会显示出一些指令,其实就是设置环境变量。但实际上并没有执行,只是显示而也,如果要执行,直接运行eval $(docker-machine env remote)即可。

1
2
3
4
5
6
7
[root@localhost ~]# docker-machine env remote
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.137.55:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/remote"
export DOCKER_MACHINE_NAME="remote"
# Run this command to configure your shell:
# eval $(docker-machine env remote)

docker-machine ssh

直接ssh即可。

1
2
3
4
[root@localhost ~]# docker-machine ssh remote
Last login: Sat Oct  5 03:56:47 2019 from 192.168.137.66
[root@remote ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

2个方法,个人还是觉得ssh的方法会比较简单一些。

参考资料

Docker Machine 详解

夜河资源网提供的所有内容仅供学习与交流。通过使用本站内容随之而来的风险以及法律责任与本站无关,所承担的法律责任由使用者承担。
一、如果您发现本站侵害了相关版权,请附上本站侵权链接和您的版权证明一并发送至邮箱:yehes#qq.com(#替换为@)我们将会在五天内处理并断开该文章下载地址。
二、本站所有资源来自互联网整理收集,全部内容采用撰写共用版权协议,要求署名、非商业用途和相同方式共享,如转载请也遵循撰写共用协议。
三、根据署名-非商业性使用-相同方式共享 (by-nc-sa) 许可协议规定,只要他人在以原作品为基础创作的新作品上适用同一类型的许可协议,并且在新作品发布的显著位置,注明原作者的姓名、来源及其采用的知识共享协议,与该作品在本网站的原发地址建立链接,他人就可基于非商业目的对原作品重新编排、修改、节选或者本人的作品为基础进行创作和发布。
四、基于原作品创作的所有新作品都要适用同一类型的许可协议,因此适用该项协议, 对任何以他人原作为基础创作的作品自然同样都不得商业性用途。
五、根据二〇〇二年一月一日《计算机软件保护条例》规定:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可不经软件著作权人许可,无需向其支付报酬!
六、鉴此,也望大家按此说明转载和分享资源!本站提供的所有信息、教程、软件版权归原公司所有,仅供日常使用,不得用于任何商业用途,下载试用后请24小时内删除,因下载本站资源造成的损失,全部由使用者本人承担!