(五)Docker三剑客之Machine

Docker Machine 是 Docker 官方编排( Orchestration) 项目之一,负责在多种平台上快速安装 Docker 环境。
Docker Machine 项目基于 Go 语言实现,目前在 Github 上进行维护。

参考:

1. docker-machine 下载安装

  1. 下载
[root@Thor ]# curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` > /usr/local/bin/docker-machine
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    434      0 --:--:--  0:00:01 --:--:--   433
100 25.3M  100 25.3M    0     0  1338k      0  0:00:19  0:00:19 --:--:-- 4264k
[root@Thor compose]# chmod +x /usr/local/bin/docker-machine
  1. 查看版本
[root@Thor compose]# docker-machine version
docker-machine version 0.13.0, build 9ba6da9
  1. 查看help
[root@Thor compose]# docker-machine help
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.13.0, build 9ba6da9

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@Thor compose]# 

2. virtualbox 的安装使用docker-machine 需要安装virtualbox

  1. 添加yum源
[root@Thor yum.repos.d] cd /etc/yum.repos.d/
[root@Thor yum.repos.d] touch virtualbox.repo

[root@Thor yum.repos.d]# cat virtualbox.repo 
[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
  1. 安装 VirtualBox-5.2
yum install -y VirtualBox-5.2

3. 创建docker主机

[root@Thor yum.repos.d]# docker-machine create -d virtualbox manager
Running pre-create checks...
(manager) Image cache directory does not exist, creating it at /root/.docker/machine/cache...
(manager) No default Boot2Docker ISO found locally, downloading the latest release...
(manager) Latest release for github.com/boot2docker/boot2docker is v18.06.0-ce
(manager) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.06.0-ce/boot2docker.iso...
Error with pre-create check: "Get https://github-production-release-asset-2e65be.s3.amazonaws.com/14930729/6d1e7256-8b4a-11e8-9a24-a19446163c40?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180726%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180726T092243Z&X-Amz-Expires=300&X-Amz-Signature=7647b3962d30f593b7d6d678c49d93412ca0ede14e17d6d12e452bfd19db9290&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dboot2docker.iso&response-content-type=application%2Foctet-stream: dial tcp 52.216.224.112:443: getsockopt: connection timed out"

[root@Thor yum.repos.d]# cd /root/.docker/machine/cache/
[root@Thor cache]# ls
[root@Thor cache]# 

下载https://github.com/boot2docker/boot2docker/releases/download/v18.06.0-ce/boot2docker.iso,并放入到/root/.docker/machine/cache/目录下

[root@Thor machine]# cd /root/.docker/machine/cache/
[root@Thor cache]# ls
boot2docker.iso

创建一个machine

[root@Thor machine]# docker-machine create -d virtualbox manager
Running pre-create checks...
Creating machine...
(manager) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/manager/boot2docker.iso...
(manager) Creating VirtualBox VM...
(manager) Creating SSH key...
(manager) Starting the VM...
(manager) Check network to re-create if needed...
(manager) 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...

This machine has been allocated an IP address, but Docker Machine could not
reach it successfully.

SSH for the machine should still work, but connecting to exposed ports, such as
the Docker daemon port (usually <ip>:2376), may not work properly.

You may need to add the route manually, or use another related workaround.

This could be due to a VPN, proxy, or host file configuration issue.

You also might want to clear any VirtualBox host only interfaces you are not using.
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 manager
[root@Thor machine]# 

4. docker-machine ls 命令

[root@Thor cache]# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
manager   -        virtualbox   Running   tcp://192.168.99.100:2376           v18.06.0-ce 

5. 通过ssh连接到docker主机

[root@Thor cache]# docker-machine ssh manager
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
                          __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___  __| | ___   ___| | _____ _ __
| '_  / _  / _ | __| __) / _` |/ _  / __| |/ / _  '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.06.0-ce, build HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018
Docker version 18.06.0-ce, build 0ffa825
docker@manager:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker@manager:~$ 

连接到主机之后你就可以在其上使用 Docker 了.

原文地址:https://www.cnblogs.com/walkinginthesun/p/9430109.html