What is 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,我们需要如下几个步骤(Ubuntu)
- 安装 https CA 证书
- 添加 GPG key
- 添加 docker apt 源
- 安装 docker
但对于多主机环境,每台都如此则显得重复工作且效率低下,因此有了docker-machine—能在多台主机上批量安装配置docker,主机可以是本地虚拟机、物理机、云主机等,支持环境如下:
- 常规 Linux 操作系统
- 虚拟化平台 - VirtualBox、VMWare、Hyper-V
- OpenStack
- 公有云 - Amazon Web Services、Microsoft Azure、Google Compute Engine、Digital Ocean 等
在docker machine中,都称之为Provider,不同Provider对应不同driver来安装配置docker host。
Install Docker Machine
Install Docker
1
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
If you would like to use Docker as a non-root user, you should now consider
adding your user to the “docker” group with something like:1
sudo usermod -aG docker <your username>
1
2
3
4
5
6
7
8sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["填入你的阿里云专属加速器地址"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Download the Docker Machine binary and extract it to your PATH.
1
2
3$ curl -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
Check the installation by displaying the Machine version:
1
2$ docker-machine version
docker-machine version 0.12.2, build 9371605
Installing bash completion scripts
1
scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do sudo wget https://raw.githubusercontent.com/docker/machine/v0.12.2/contrib/completion/bash/${i} -P /etc/bash_completion.d; done
To enable the
docker-machine
shell prompt, add$(__docker_machine_ps1)
to yourPS1
setting in~/.bashrc
.1
PS1='[\u@\h \W$(__docker_machine_ps1)]\$ '
Use Machine to run Docker containers
To run a Docker container,you:
- create a new(or start an exiting) Docker virtual machine
- switch your environment to your new VM
- use the docker client to create,load and manage containers
Machine drivers—Generic
Create machines using an existing VM/Host with SSH.
This is useful if you are using a provider that Machine does not support directly or if you would like to import an existing host to allow Docker Machine to manage.
The driver will perform a list of tasks on create:
- If docker is not running on the host, it will be installed automatically.
- It will update the host packages (
apt-get update
,yum update
…). - It will generate certificates to secure the docker daemon.
- The docker daemon will be restarted, thus all running containers will be stopped.
- The hostname will be changed to fit the machine name.
1 | docker-machine create --driver generic --generic-ip-address=192.168.56.104 host1 |
Sudo privileges
The user that is used to SSH into the host can be specified with --generic-ssh-user
flag. This user has to have password-less sudo privileges. If it’s not the case, you need to edit the sudoers
file and configure the user as a sudoer with NOPASSWD
. See https://help.ubuntu.com/community/Sudoers .
ENVIRONMENT VARIABLES AND DEFAULT VALUES
CLI option | Environment variable | Default |
---|---|---|
--generic-engine-port |
GENERIC_ENGINE_PORT |
2376 |
–generic-ip-address | GENERIC_IP_ADDRESS |
- |
--generic-ssh-key |
GENERIC_SSH_KEY |
- |
--generic-ssh-user |
GENERIC_SSH_USER |
root |
--generic-ssh-port |
GENERIC_SSH_PORT |
22 |
create 过程:
① 通过 ssh 登录到远程主机。
② 安装 docker。
③ 拷贝证书。
④ 配置 docker daemon。
⑤ 启动 docker。
Machine CLI
1 | Options: |
1 | Commands: |