base=https://raw.githubusercontent.com/docker/machine/v0.14.0
for i in docker-machine-prompt.bash docker-machine-wrapper.bash docker-machine.bash
do
sudo wget "$base/contrib/completion/bash/${i}" -P /etc/bash_completion.d
done
使脚本生效。
root@ubuntu:~# docker-machine create --driver generic --generic-ip-address=192.168.1.201 host1
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(host1) No SSH key specified. Assuming an existing key at the default location.
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 ubuntu(systemd)...
Installing Docker...
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 host1
① 通过 ssh 登录到远程主机。
② 安装 docker。
③ 拷贝证书。
④ 配置 docker daemon。
⑤ 启动 docker。
再次执行 docker-machine ls :
root@ubuntu:~# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
host1 - generic Running tcp://192.168.1.201:2376 v18.04.0-ce
使用同样的方法创建 host2:
[root@ubuntu ~ [host1]]# docker run -itd busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
f70adabe43c0: Pull complete
Digest: sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
Status: Downloaded newer image for busybox:latest
13dda8965035ab88ff29541e0d1c92d9cb963063d2d9c1e118e21dea78075d09 3、docker-machine ssh
除了上面的使用方法外,我们还有更加简便的方式,就是使用docker-machine ssh,需要我们提前配置好 /etc/hosts,比如上面的命令还可以直接通过如下指令创建。
[root@ubuntu ~]# docker-machine ssh host1 "docker run -itd busybox"
6d46a6a6f36b5ee464ff227f50a2d48e1f0db1ad9d47ce2996181436447c1a8b
[root@ubuntu ~]# docker-machine ssh host1 "docker ps -a"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13dda8965035 busybox "sh" About a minute ago Up About a minute peaceful_payne 4、host2 启动容器
执行eval $(docker-machine env host2)切换到 host2:
[root@ubuntu ~ [host2]]# docker ps -a
WARN[0000] Unable to use system certificate pool: requires building with go 1.7 or later
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
可见,在多主机环境下 Docker Machine 可以大大提高效率,而且操作也很简单,通过使用docker-machine ssh 我们可以不用再使用eval $(docker-machine env host2)进行切换。