docker-machine 安装管理docker主机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-machine 安装管理docker主机相关的知识,希望对你有一定的参考价值。

说明

ubuntu     系统类型

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

192.168.1.73   docker-machine服务器端

192.168.1.80   docker 客户端

 

1sudo配置:

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

[email protected]:~$ 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主机

[email protected]:~$ 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主机
[email protected]:~$ 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虚拟机

[email protected]:~$ 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

[email protected]:~$ docker-machine ssh 80  docker run hello-world

[email protected]:~$ 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

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

[email protected]:~$ eval $(docker-machine env 80)
[email protected]:~$ 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   
[email protected]:~$ 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 安装管理docker主机的主要内容,如果未能解决你的问题,请参考以下文章

docker三剑客 docker-machine安装&管理swarm

Docker三剑客之docker-machine

Docker machine 多主机管理

安装docker-machine

容器技术之Docker-machine

docker-machine 远程安装docker