docker-machine 介绍
Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands. You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.
上面引用docker官方文档来对docker-machine做了大概介绍。大致意思:docker-machine是一个可以让你管理已经安装docker引擎机器的虚拟主机工具,你可以利用docker-machine在本机、局域网、数据中心、甚至公有云上创建docker主机!
docker-machine的安装
- 安装docker
- 安装docker-machine
- 最新版本的windows和mac os docker安装包已经集成docker-machine工具,所以直接下载安装包安装即可使用。
当然,如果你想体验最新的docker-machine,你可以访问github上docker-machine的下载页 下载最新的docker-machine
最新的docker-machine安装方式
[windows] 需要配合git bash for windows使用1
2
3$ if [[ ! -d "$HOME/bin" ]]; then mkdir -p "$HOME/bin"; fi && \
curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-Windows-x86_64.exe > "$HOME/bin/docker-machine.exe" && \
chmod +x "$HOME/bin/docker-machine.exe"
[mac]1
2$ curl -L https://github.com/docker/machine/releases/download/v0.12.2/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine && \
chmod +x /usr/local/bin/docker-machine
- linux平台安装
1
2
3curl -L https://github.com/docker/machine/releases/download/v0.12.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
docker-machine的主机创建与查看
- 创建docker主机
1
docker-machine create --driver virtualbox vm-docker1
1.这一行的命令意思是:用virtualbox驱动创建一个名字为vm-docker1的docker主机,所以务必保证你的系统中已经安装virtualbox
2.创建docker主机时docker-machine会自动下载一个名为boot2docker的微型linux镜像,还会常见ssh认证证书方便后续操作
- 查看主机
1
2
3➜ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
vm-docker1 * virtualbox Running tcp://192.168.99.100:2376 v17.06.2-ce
docker-machine常用命令介绍
安装配置docker-machine的命令行补全
创建completion(命令补全)zsh的配置目录
1
2mkdir -p ~/.zsh/completion
curl -L https://raw.githubusercontent.com/docker/machine/v0.12.2/contrib/completion/zsh/_docker-machine > ~/.zsh/completion/_docker-machine配置~/.zshrc文件
1
echo -e "fpath=(~/.zsh/completion $fpath)\nautoload -Uz compinit && compinit -i" >~/.zshrc
测试配置结果(在命令输入docker-machine Tab就能显示其他命令选项)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22➜ ~ docker-machine
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
help -- Shows a list of commands or help for one command
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
scp -- Copy files between machines
ssh -- Log into or run a command on a machine with SSH.
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 versionbash配置docker-machine的命令补全请参考docker官方文档
主机状态配置类
- docker-machine active与docker-machine ls
- docker-machine ls 用于查看主机名,活动状态,docker version url 以及状态
1
2
3
4➜ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
vm-docker1 - virtualbox Running tcp://192.168.99.100:2376 v17.06.2-ce
vm-docker2 * virtualbox Running tcp://192.168.99.101:2376 v17.06.2-ce
docker-machine active 用于查看活动主机,官方这样解释了这个“活动”(DOCKER_HOST环境变量指向那台主机的url,那台主机即为活动主机)
1
2
3
4
5
6
7
8➜ ~ echo $DOCKER_HOST
tcp://192.168.99.101:2376
➜ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
vm-docker1 - virtualbox Running tcp://192.168.99.100:2376 v17.06.2-ce
vm-docker2 * virtualbox Running tcp://192.168.99.101:2376 v17.06.2-ce
➜ ~ docker-machine active
vm-docker2docker-machine config 打印某台docker主机的配置信息 主要包括认证配置和url 加–swarm用于显示swarm的配置信息
1
2
3
4
5
6➜ ~ docker-machine config vm-docker1
--tlsverify
--tlscacert="/Users/zlx/.docker/machine/machines/vm-docker1/ca.pem"
--tlscert="/Users/zlx/.docker/machine/machines/vm-docker1/cert.pem"
--tlskey="/Users/zlx/.docker/machine/machines/vm-docker1/key.pem"
-H=tcp://192.168.99.100:2376docker-machine inspect 以json格式打印某台docker主机的详细配置信息
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85➜ ~ docker-machine inspect vm-docker1
{
"ConfigVersion": 3,
"Driver": {
"IPAddress": "192.168.99.100",
"MachineName": "vm-docker1",
"SSHUser": "docker",
"SSHPort": 57748,
"SSHKeyPath": "/Users/zlx/.docker/machine/machines/vm-docker1/id_rsa",
"StorePath": "/Users/zlx/.docker/machine",
"SwarmMaster": false,
"SwarmHost": "tcp://0.0.0.0:3376",
"SwarmDiscovery": "",
"VBoxManager": {},
"HostInterfaces": {},
"CPU": 1,
"Memory": 1024,
"DiskSize": 20000,
"NatNicType": "82540EM",
"Boot2DockerURL": "",
"Boot2DockerImportVM": "",
"HostDNSResolver": false,
"HostOnlyCIDR": "192.168.99.1/24",
"HostOnlyNicType": "82540EM",
"HostOnlyPromiscMode": "deny",
"UIType": "headless",
"HostOnlyNoDHCP": false,
"NoShare": false,
"DNSProxy": true,
"NoVTXCheck": false,
"ShareFolder": ""
},
"DriverName": "virtualbox",
"HostOptions": {
"Driver": "",
"Memory": 0,
"Disk": 0,
"EngineOptions": {
"ArbitraryFlags": [],
"Dns": null,
"GraphDir": "",
"Env": [],
"Ipv6": false,
"InsecureRegistry": [],
"Labels": [],
"LogLevel": "",
"StorageDriver": "",
"SelinuxEnabled": false,
"TlsVerify": true,
"RegistryMirror": [],
"InstallURL": "https://get.docker.com"
},
"SwarmOptions": {
"IsSwarm": false,
"Address": "",
"Discovery": "",
"Agent": false,
"Master": false,
"Host": "tcp://0.0.0.0:3376",
"Image": "swarm:latest",
"Strategy": "spread",
"Heartbeat": 0,
"Overcommit": 0,
"ArbitraryFlags": [],
"ArbitraryJoinFlags": [],
"Env": null,
"IsExperimental": false
},
"AuthOptions": {
"CertDir": "/Users/zlx/.docker/machine/certs",
"CaCertPath": "/Users/zlx/.docker/machine/certs/ca.pem",
"CaPrivateKeyPath": "/Users/zlx/.docker/machine/certs/ca-key.pem",
"CaCertRemotePath": "",
"ServerCertPath": "/Users/zlx/.docker/machine/machines/vm-docker1/server.pem",
"ServerKeyPath": "/Users/zlx/.docker/machine/machines/vm-docker1/server-key.pem",
"ClientKeyPath": "/Users/zlx/.docker/machine/certs/key.pem",
"ServerCertRemotePath": "",
"ServerKeyRemotePath": "",
"ClientCertPath": "/Users/zlx/.docker/machine/certs/cert.pem",
"ServerCertSANs": [],
"StorePath": "/Users/zlx/.docker/machine/machines/vm-docker1"
}
},
"Name": "vm-docker1"
}
可以查看指定配置项,比如我想查看SSHUser SSHPort1
2
3
4➜ ~ docker-machine inspect --format={{.Driver.SSHUser}} vm-docker1
docker
➜ ~ docker-machine inspect --format={{.Driver.SSHPort}} vm-docker1
57748
- 获取docker主机ip
1
2
3
4➜ ~ docker-machine ip vm-docker1
192.168.99.100
➜ ~ docker-machine ip vm-docker2
192.168.99.101
管理类
docker-machine rm 删除主机 默认删除时会询问是否删除 -f 无论如何强制删除 -y 删除时不询问
1
2
3
4
5➜ ~ docker-machine rm vm-docker2
About to remove vm-docker2
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed vm-docker2docker-machine create 用于创建docker主机 -d用于指定driver类型
主要有这几种1
2amazonec2 digitalocean generic hyperv openstack softlayer vmwarefusion vmwarevsphere
azure exoscale google none rackspace virtualbox vmwarevcloudair
1 | ➜ ~ docker-machine create -d virtualbox vm-docker2 |
docker-machine start/stop/restart/kill docker主机的启动停止与重启 kill强制关闭
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23➜ ~ docker-machine stop vm-docker2
Stopping "vm-docker2"...
Machine "vm-docker2" was stopped.
➜ ~ docker-machine status vm-docker2
Stopped
➜ ~ docker-machine start vm-docker2
Starting "vm-docker2"...
(vm-docker2) Check network to re-create if needed...
(vm-docker2) Waiting for an IP...
Machine "vm-docker2" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
➜ ~ docker-machine restart vm-docker2
Restarting "vm-docker2"...
(vm-docker2) Check network to re-create if needed...
(vm-docker2) Waiting for an IP...
Waiting for SSH to be available...
Detecting the provisioner...
Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
➜ ~ docker-machine kill vm-docker2
Killing "vm-docker2"...
Machine "vm-docker2" was killed.docker-machine upgrade 升级docker
关于如何升级:官方这样说,它取决于被管理主机的底层os,比如底层os为Ubuntu 那运行docker-machine upgrade 实际是在被管理主机上运行apt-get upgrade docker-engine 如果底层为centos,那实际运行的是 yum update docker-engine,如果底层是boot2docker 那么将会下载最新的boot2docker来升级docker-engine
docker-machine url 获取doker主机的url
1
2➜ ~ docker-machine url vm-docker1
tcp://192.168.99.100:2376docker-machine ssh ssh到docker主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19➜ ~ docker-machine ssh vm-docker1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.06.2-ce, build HEAD : ff16afa - Wed Sep 6 00:17:25 UTC 2017
Docker version 17.06.2-ce, build cec0b72
docker@vm-docker1:~$ ls
log.log
docker@vm-docker1:~$ touch vm-docker1docker-machine scp 用于管理机与docker 主机之间的文件传输
1
2
3
4
5
6➜ ~ docker-machine scp vm-docker1:~/vm-docker1 .
vm-docker1 100% 0 0.0KB/s 00:00
➜ ~ ls
Applications Documents Movies Pictures node_modules
Applications (Parallels) Downloads Music Public vm-docker1
Desktop Library Parallels fontsdocker-machine env 切换docker host主机环境
1
2
3
4
5
6
7查看本地环境所在docker 主机
➜ ~ echo $DOCKER_HOST
tcp://192.168.99.102:2376
切换至vm-docker1
➜ ~ eval $(docker-machine env vm-docker1)
➜ ~ echo $DOCKER_HOST
tcp://192.168.99.100:2376
实例:1
2
3
4
5
6
7
8
9
10
11
12
13
141.切换vm-docker2,查看docker镜像和容器(事先pull好的)
➜ ~ eval $(docker-machine env vm-docker2)
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b27daf6ba5c hub.c.163.com/public/nginx:1.2.1 "/bin/sh -c '/etc/..." 16 minutes ago Up 16 minutes 22/tcp, 443/tcp, 0.0.0.0:80->80/tcp nginx
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/public/nginx 1.2.1 2dc68ff797db 17 months ago 172MB
2.切换至vm-docker1 查看docker镜像和容器
➜ ~ eval $(docker-machine env vm-docker1)
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker-machine 添加本地主机
适用于未安装或者已经安装docker 但没有加入docker-machine管理的本地主机/虚拟机为doker主机,未安装docker的主机会自动下载docker安装
- 配置ssh免密登录
1
2
3
4
5
6
7
8
9
10 ➜ ~ ssh-copy-id root@192.168.1.105
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/zlx/.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.1.105's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.105'"
and check to make sure that only the key(s) you wanted were added.
查看当前docker主机
1
2
3
4➜ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
vm-docker1 - virtualbox Running tcp://192.168.99.100:2376 v17.06.2-ce
vm-docker2 - virtualbox Running tcp://192.168.99.102:2376 v17.06.2-ce添加docker主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14docker-machine create --driver=generic --generic-ip-address=192.168.1.105 --generic-ssh-key=/Users/zlx/.ssh/id_rsa --generic-ssh-user=root centos7-docker
Running pre-create checks...
Creating machine...
(centos7-docker) 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!再次查看docker主机
1
2
3
4
5➜ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
centos7-docker - generic Running tcp://192.168.1.105:2376 v17.07.0-ce
vm-docker1 - virtualbox Running tcp://192.168.99.100:2376 v17.06.2-ce
vm-docker2 - virtualbox Running tcp://192.168.99.102:2376 v17.06.2-ce
- 本文作者: ChuLinx
- 本文链接: http://yoursite.com/2019/11/30/docker三剑客之docker-machine/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!