背景说明

Docker Machine是Docker官方Orchestration项目之一,负责在多种平台上快速安装Docker环境。

解决方案

帮助命令

  1. [root@vm1 ~]# docker-machine help
  2. Usage: docker-machine [OPTIONS] COMMAND [arg...]
  3. Create and manage machines running Docker.
  4. Version: 0.16.2, build bd45ab13
  5. Author:
  6. Docker Machine Contributors - <https://github.com/docker/machine>
  7. Options:
  8. --debug, -D Enable debug mode
  9. --storage-path, -s "/root/.docker/machine" Configures storage path [$MACHINE_STORAGE_PATH]
  10. --tls-ca-cert CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  11. --tls-ca-key Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  12. --tls-client-cert Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  13. --tls-client-key Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  14. --github-api-token Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  15. --native-ssh Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  16. --bugsnag-api-token BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  17. --help, -h show help
  18. --version, -v print the version
  19. Commands:
  20. active Print which machine is active
  21. config Print the connection config for machine
  22. create Create a machine
  23. env Display the commands to set up the environment for the Docker client
  24. inspect Inspect information about a machine
  25. ip Get the IP address of a machine
  26. kill Kill a machine
  27. ls List machines
  28. provision Re-provision existing machines
  29. regenerate-certs Regenerate TLS Certificates for a machine
  30. restart Restart a machine
  31. rm Remove a machine
  32. ssh Log into or run a command on a machine with SSH.
  33. scp Copy files between machines
  34. mount Mount or unmount a directory from a machine with SSHFS.
  35. start Start a machine
  36. status Get the status of a machine
  37. stop Stop a machine
  38. upgrade Upgrade a machine to the latest version of Docker
  39. url Get the URL of a machine
  40. version Show the Docker Machine version or a machine docker version
  41. help Shows a list of commands or help for one command
  42. Run 'docker-machine COMMAND --help' for more information on a command.
  43. [root@vm1 ~]#

创建主机

  1. [root@vm1 ~]# docker-machine create hostvm
  2. Running pre-create checks...
  3. Creating machine...
  4. (hostvm) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/hostvm/boot2docker.iso...
  5. (hostvm) Creating VirtualBox VM...
  6. (hostvm) Creating SSH key...
  7. (hostvm) Starting the VM...
  8. (hostvm) Check network to re-create if needed...
  9. (hostvm) Waiting for an IP...
  10. Waiting for machine to be running, this may take a few minutes...
  11. Detecting operating system of created instance...
  12. Waiting for SSH to be available...
  13. Detecting the provisioner...
  14. Provisioning with boot2docker...
  15. Copying certs to the local machine directory...
  16. Copying certs to the remote machine...
  17. Setting Docker configuration on the remote daemon...
  18. Checking connection to Docker...
  19. Docker is up and running!
  20. To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env hostvm
  21. [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

查看主机

  1. [root@vm1 ~]# docker-machine ls
  2. NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
  3. deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
  4. manage - virtualbox Stopped Unknown
  5. [root@vm1 ~]#

登录主机

  1. [root@vm1 ~]# docker-machine ls
  2. NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
  3. deephash-master - virtualbox Running tcp://192.168.99.101:2376 v19.03.12
  4. manage * virtualbox Running tcp://192.168.99.100:2376 v19.03.12
  5. [root@vm1 ~]# docker-machine ssh manage
  6. ( '>')
  7. /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
  8. (/-_--_-\) www.tinycorelinux.net
  9. docker@manage:~$
  10. docker@manage:~$ docker version
  11. Client: Docker Engine - Community
  12. Version: 19.03.12
  13. API version: 1.40
  14. Go version: go1.13.10
  15. Git commit: 48a66213fe
  16. Built: Mon Jun 22 15:42:53 2020
  17. OS/Arch: linux/amd64
  18. Experimental: false
  19. Server: Docker Engine - Community
  20. Engine:
  21. Version: 19.03.12
  22. API version: 1.40 (minimum version 1.12)

主机信息

  1. [root@vm1 ~]# docker-machine inspect deephash-master
  2. {
  3. "ConfigVersion": 3,
  4. "Driver": {
  5. "IPAddress": "192.168.99.101",
  6. "MachineName": "deephash-master",
  7. "SSHUser": "docker",
  8. "SSHPort": 41846,
  9. "SSHKeyPath": "/root/.docker/machine/machines/deephash-master/id_rsa",
  10. "StorePath": "/root/.docker/machine",
  11. "SwarmMaster": false,
  12. "SwarmHost": "tcp://0.0.0.0:3376",
  13. "SwarmDiscovery": "",
  14. "VBoxManager": {},
  15. "HostInterfaces": {},
  16. "CPU": 1,
  17. "Memory": 1024,
  18. "DiskSize": 20000,
  19. "NatNicType": "82540EM",
  20. "Boot2DockerURL": "",
  21. "Boot2DockerImportVM": "",
  22. "HostDNSResolver": false,
  23. "HostOnlyCIDR": "192.168.99.1/24",
  24. "HostOnlyNicType": "82540EM",
  25. "HostOnlyPromiscMode": "deny",
  26. "UIType": "headless",
  27. "HostOnlyNoDHCP": false,
  28. "NoShare": false,
  29. "DNSProxy": true,
  30. "NoVTXCheck": false,
  31. "ShareFolder": ""
  32. },
  33. "DriverName": "virtualbox",
  34. "HostOptions": {
  35. "Driver": "",
  36. "Memory": 0,
  37. "Disk": 0,
  38. "EngineOptions": {
  39. "ArbitraryFlags": [],
  40. "Dns": null,
  41. "GraphDir": "",
  42. "Env": [],
  43. "Ipv6": false,
  44. "InsecureRegistry": [],
  45. "Labels": [],
  46. "LogLevel": "",
  47. "StorageDriver": "",
  48. "SelinuxEnabled": false,
  49. "TlsVerify": true,
  50. "RegistryMirror": [
  51. "https://wf45d3ay.mirror.aliyuncs.com"
  52. ],
  53. "InstallURL": "https://get.docker.com"
  54. },
  55. "SwarmOptions": {
  56. "IsSwarm": false,
  57. "Address": "",
  58. "Discovery": "",
  59. "Agent": false,
  60. "Master": false,
  61. "Host": "tcp://0.0.0.0:3376",
  62. "Image": "swarm:latest",
  63. "Strategy": "spread",
  64. "Heartbeat": 0,
  65. "Overcommit": 0,
  66. "ArbitraryFlags": [],
  67. "ArbitraryJoinFlags": [],
  68. "Env": null,
  69. "IsExperimental": false
  70. },
  71. "AuthOptions": {
  72. "CertDir": "/root/.docker/machine/certs",
  73. "CaCertPath": "/root/.docker/machine/certs/ca.pem",
  74. "CaPrivateKeyPath": "/root/.docker/machine/certs/ca-key.pem",
  75. "CaCertRemotePath": "",
  76. "ServerCertPath": "/root/.docker/machine/machines/deephash-master/server.pem",
  77. "ServerKeyPath": "/root/.docker/machine/machines/deephash-master/server-key.pem",
  78. "ClientKeyPath": "/root/.docker/machine/certs/key.pem",
  79. "ServerCertRemotePath": "",
  80. "ServerKeyRemotePath": "",
  81. "ClientCertPath": "/root/.docker/machine/certs/cert.pem",
  82. "ServerCertSANs": [],
  83. "StorePath": "/root/.docker/machine/machines/deephash-master"
  84. }
  85. },
  86. "Name": "deephash-master"
  87. }
  88. [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:~$