环境准备:
安装docker-machine:(参考文档:https://docs.docker.com/machine/install-machine/)
curl -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
mv /tmp/docker-machine /usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine
检查是否安装成功:
-
- [root@yj152 docker]# docker-machine version
- docker-machine version 0.16.0, build 702c267f
- [root@yj152 docker]#
安装docker-machine脚本:
在目录 https://github.com/docker/machine/tree/master/contrib/completion/bash 下载如此三个文件
scripts=( docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash ); for i in "${scripts[@]}"; do wget https://raw.githubusercontent.com/docker/machine/v0.15.0/contrib/completion/bash/${i} -P /etc/bash_completion.d; done 添加以下内容到$HOME/.bashrc中:
-
- source /etc/bash_completion.d/docker-machine-wrapper.bash
- source /etc/bash_completion.d/docker-machine-prompt.bash
- source /etc/bash_completion.d/docker-machine.bash
- PS1='[\u@\h \W$(__docker_machine_ps1)]\$ '
创建Machine:
创建machine要求能够无密码远程登录主机:
ssh-keygen
ssh-copy-id username@remote_IP
使用docker-machine create 创建host1
docker-machine create --driver generic --generic-ip-address=remote_IP host1
使用docker-machine ls 查看已创建的machine
具体步骤如下图:
管理machine:
执行远程的docker命令,如下:
docker -H tcp://remote_IP:Port command
显示host1中的所有环境变量:
docker-machine env host1
根据提示执行:eval $(docker-machine env host1)
docker-machine 子命令:
docker-machine upgrade : 更新machine的docker到最新版本
docker-machine config : 查看machine的docker的配置
stop/start/restart : 是对machine 的操作系统操作,而不是 stop/start/restart docker daemon
docker-machine scp : 可以在不同machine之间复制文件
例:docker-machine scp host1:/tmp/a host2:/tmp/b