首页 > 代码库 > docker-machine(1) 安装管理docker主机

docker-machine(1) 安装管理docker主机

说明

ubuntu     系统类型

test                       docker、docker-machine管理操作用户

192.168.1.73   docker-machine服务器端

192.168.1.80   docker 客户端

 

1sudo配置:

执行范围:docker-machine服务器端、docker 客户端

test@bogon:~$ sudo visudo

#追加1行,确保远程ssh执行命令不报错

#sudo: no tty present and no askpass program specified 

Defaults   visiblepw

#文件末尾追加 sudo无需密码

test  ALL=(ALL) NOPASSWD: ALL

二、docker-machine服务器端配置 

2.1 分发公钥

生成私钥、公钥方法自行google

scp .ssh/authorized_keys 192.168.1.80:./ssh/

2.2 下载安装

curl -L https://github.com/docker/machine/releases/download/v0.9.0-rc2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine
sudo mv /tmp/docker-machine /usr/local/bin/
sudo chmod a+x /usr/localbin/docker-machine

2.3 创建docker主机

test@bogon:~$ docker-machine create   --driver generic   --generic-ip-address=192.168.1.80   --generic-ssh-key ~/.ssh/id_rsa  --generic-ssh-user=test  80

Running pre-create checks...

Creating machine...

(80) Importing SSH key...

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 80

docker-machine详细命令参见:https://docs.docker.com/machine/

命令分析:

create  #创建docker主机
--driver generic #驱动类型 generic 支持linux通用服务器,还支持很多种云主机
--generic-ip-address=192.168.1.80 #指定主机
--generic-ssh-key ~/.ssh/id_rsa #指定私钥
--generic-ssh-user=test #指定用户
80 #主机名称

查看已创建docker主机
test@bogon:~$ docker-machine ls
NAME   ACTIVE   DRIVER    STATE     URL                       SWARM   DOCKER        ERRORS
73     *        generic   Running   tcp://192.168.1.73:2376           v17.06.0-ce   
80     -        generic   Running   tcp://192.168.1.80:2376           v17.06.0-ce  

远程执行docker命令,创建docker虚拟机

test@bogon:~$ docker-machine ssh 80  docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                  PORTS               NAMES

691bd1e50cd2        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                       tender_banach

test@bogon:~$ docker-machine ssh 80  docker run hello-world

test@bogon:~$ docker-machine ssh 80  docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES

68399de0b83b        hello-world         "/hello"            5 seconds ago       Exited (0) 4 seconds ago                       thirsty_bohr
691bd1e50cd2        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                          tender_banach

也可通过切换环境变量,来实现:

test@bogon:~$ eval $(docker-machine env 80)
test@bogon:~$ docker-machine ls
NAME   ACTIVE   DRIVER    STATE     URL                       SWARM   DOCKER        ERRORS
73     -        generic   Running   tcp://192.168.1.73:2376           v17.06.0-ce   
80     *        generic   Running   tcp://192.168.1.80:2376           v17.06.0-ce   
test@bogon:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
68399de0b83b        hello-world         "/hello"            5 seconds ago       Exited (0) 4 seconds ago                       thirsty_bohr
691bd1e50cd2        hello-world         "/hello"            2 days ago          Exited (0) 2 days ago                          tender_banach

 

 

 

 

 

 

docker-machine(1) 安装管理docker主机