0%

使用docker machine 管理docker主机

在使用docker的时候,难免会遇到当前主机不支持运行docker的情况,这时可以使用运程ssh的形式连接一个已经创建了docker的主机,本地正常使用docker命令

Docker Machine Generic驱动

首先说明一下 Docker Machine Generic驱动
它可以 创建一个machines通过SSH使用已经存在的虚拟机或是主机。

Example
创建一个machine的实例,需要指定–driver generic,主机的IP地址,DNS名和SSH私钥路径去连接它.

1
2
3
4
5
docker-machine create \
--driver generic \
--generic-ip-address=203.0.113.81 \
--generic-ssh-key ~/.ssh/id_rsa \
vm

除了上述的命令,你还可以使用的选项如下

1
2
3
4
5
6
Options
--generic-engine-port:Docker Daemon使用的端口(注意:这个标识在boot2docker中无效)
--generic-ip-address:必需字段,主机IP地址.
--generic-ssh-key:SSH user的私钥路径.
--generic-ssh-user:SSH连接使用的username.
--generic-ssh-port:SSH使用的端口.

之后 eval $(docker-machine env vm)
注意 这里 vm 是你设置的 docker machine 名字

执行一个 hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/