背景说明
Docker Machine是Docker官方Orchestration项目之一,负责在多种平台上快速安装Docker环境。
解决方案
帮助命令
[root@vm1 ~]# docker-machine help
Usage: docker-machine [OPTIONS] COMMAND [arg...]
Create and manage machines running Docker.
Version: 0.16.2, build bd45ab13
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
Run 'docker-machine COMMAND --help' for more information on a command.
[root@vm1 ~]#
创建主机
[root@vm1 ~]# docker-machine create hostvm
Running pre-create checks...
Creating machine...
(hostvm) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/hostvm/boot2docker.iso...
(hostvm) Creating VirtualBox VM...
(hostvm) Creating SSH key...
(hostvm) Starting the VM...
(hostvm) Check network to re-create if needed...
(hostvm) 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 hostvm
[root@vm1 ~]#
环境变量中指定HTTP代理服务:—engine-env HTTP_PROXY=http://proxy.com:3128 不带验证的注册仓库服务: —engine-insecure-registry registry.private.com 配置 Docker 的仓库镜像: —engine-registry-mirror https://registry.docker-cn.com 配置 Docker 的默认 DNS: —engine-opt dns=114.114.114.114 配置主机内存: —virtualbox-memory 2048 配置主机 CPU: —virtualbox-cpu-count 2
查看主机
[root@vm1 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
manage - virtualbox Stopped Unknown
[root@vm1 ~]#
登录主机
[root@vm1 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
manage * virtualbox Running tcp://192.168.99.100:2376 v19.03.12
[root@vm1 ~]# docker-machine ssh manage
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@manage:~$
docker@manage:~$ docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:42:53 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
主机信息
[root@vm1 ~]# docker-machine inspect deephash-master
{
"ConfigVersion": 3,
"Driver": {
"IPAddress": "192.168.99.101",
"MachineName": "deephash-master",
"SSHUser": "docker",
"SSHPort": 41846,
"SSHKeyPath": "/root/.docker/machine/machines/deephash-master/id_rsa",
"StorePath": "/root/.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": [
"https://wf45d3ay.mirror.aliyuncs.com"
],
"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": "/root/.docker/machine/certs",
"CaCertPath": "/root/.docker/machine/certs/ca.pem",
"CaPrivateKeyPath": "/root/.docker/machine/certs/ca-key.pem",
"CaCertRemotePath": "",
"ServerCertPath": "/root/.docker/machine/machines/deephash-master/server.pem",
"ServerKeyPath": "/root/.docker/machine/machines/deephash-master/server-key.pem",
"ClientKeyPath": "/root/.docker/machine/certs/key.pem",
"ServerCertRemotePath": "",
"ServerKeyRemotePath": "",
"ClientCertPath": "/root/.docker/machine/certs/cert.pem",
"ServerCertSANs": [],
"StorePath": "/root/.docker/machine/machines/deephash-master"
}
},
"Name": "deephash-master"
}
[root@vm1 ~]#
主机状态
[root@vm1 ~]# docker-machine status deephash-master
Running
[root@vm1 ~]#
停止主机
[root@vm1 ~]# docker-machine stop manage
Stopping "manage"...
Machine "manage" was stopped.
[root@vm1 ~]#
删除主机
[root@vm1 ~]# docker-machine rm test
About to remove test
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed test
[root@vm1 ~]#
杀死主机
[root@vm1 ~]# docker-machine kill hostvm
Killing "hostvm"...
Machine "hostvm" was killed.
[root@vm1 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
hostvm - virtualbox Stopped Unknown
manage - virtualbox Stopped Unknown
[root@vm1 ~]#
文件拷贝
当前主机到虚拟主机
[root@vm1 ~]# docker-machine scp VirtualBox-6.1-6.1.34_150636_el7-1.x86_64.rpm manage:/home/docker/1.txt
VirtualBox-6.1-6.1.34_150636_el7-1.x86_64.rpm 100% 97MB 17.6MB/s 00:05
[root@vm1 ~]#
当拷贝文件夹时添加-r选项
虚拟机到虚拟主机
[root@vm1 ~]# docker-machine scp manage:/home/docker/1.txt deephash-master:/home/docker
当拷贝文件夹时添加-r选项
环境变量
[root@vm1 ~]# docker-machine env manage
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/manage"
export DOCKER_MACHINE_NAME="manage"
# Run this command to configure your shell:
# eval $(docker-machine env manage)
[root@vm1 ~]# eval $(docker-machine env manage)
[root@vm1 ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.1-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.130-boot2docker
Operating System: Boot2Docker 19.03.12 (TCL 10.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 985.4MiB
Name: manage
ID: 4CCR:V53C:PQ42:VYGX:NIA6:6Q7V:KEHN:SHEU:TE5S:3AR6:3PKW:NTNZ
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
[root@vm1 ~]# docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.40
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:49:57 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:49:35 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
[root@vm1 ~]#
升级版本
[root@vm1 ~]# docker-machine ssh deephash-master
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@deephash-master:~$ docker --version
Docker version 19.03.12, build 48a66213fe
docker@deephash-master:~$ exit
logout
[root@vm1 ~]# docker-machine upgrade deephash-master
[root@vm1 ~]# docker-machine ssh deephash-master
( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@deephash-master:~$ docker --version
Docker version 19.03.12, build 48a66213fe
docker@deephash-master:~$