docker之docker-machine用法
转自:https://www.cnblogs.com/jsonhc/p/7784466.html
docker-machine 是docker官方提供的docker管理工具。
通过docker-machine可以轻松的做到:
在Windows平台和MAC平台安装和运行docker
搭建和管理多个docker 主机
搭建swarm集群
环境win下面安装的virtualbox,virtualbox安装的centos7,网络模式NAT+hostonly
ip:192.168.56.102(hostonly)
1、安装docker-machine:
curl -L https://github.com/docker/machine/releases/download/v0.13.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine
2、查看docker-machine版本:
# docker-machine version
[root@docker ~]# docker-machine version
docker-machine version 0.13.0, build 9ba6da9
3、在centos7环境下创建machine:
[root@localhost ~]# docker-machine create -d virtualbox default
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"
但是却报错了,以为virtualbox安装的centos7环境支持的是virtualbox驱动,才发现环境安装支持virtualbox驱动
于是采用generic驱动,具体介绍查看官网:https://docs.docker.com/machine/drivers/generic/
[root@localhost ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=root vm
[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id root@192.168.56.102
将密码发给自己,然后重新继续创建machine:
[root@localhost ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=root vm
Running pre-create checks...
Creating machine...
(vm) 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 centos...
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 vm
于是终于创建machine成功了
[root@localhost ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
vm - generic Running tcp://192.168.56.102:2376 v17.09.0-ce
[root@localhost ~]# docker-machine env vm
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.56.102:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/vm"
export DOCKER_MACHINE_NAME="vm"
# Run this command to configure your shell:
# eval $(docker-machine env vm)
[root@localhost ~]# eval $(docker-machine env vm)
利用ssh登录到machine中:
[root@localhost ~]# docker-machine ssh --help
Usage: docker-machine ssh [arg...]
Log into or run a command on a machine with SSH.
Description:
Arguments are [machine-name] [command]
[root@localhost ~]# docker-machine ssh vm
Last login: Sat Nov 4 17:55:53 2017 from 192.168.56.102
[root@vm ~]#
[root@localhost ~]# docker run -d --name=nginx nginx
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e62975fab90 nginx "nginx -g 'daemon ..." About a minute ago Up 59 seconds 80/tcp nginx
[root@localhost ~]# docker-machine ssh vm
Last login: Sat Nov 4 18:13:27 2017 from 192.168.56.102
[root@vm ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e62975fab90 nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp nginx
[root@docker ~]# ssh-keygen
[root@docker ~]# ssh-copy-id root@192.168.56.102
[root@docker ~]# docker-machine create -d generic --generic-ip-address=192.168.56.102 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=root default
Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) 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 centos...
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 default
执行环境变量,进入到machine环境:
[root@docker ~]# docker-machine env default
[root@docker ~]# eval $(docker-machine env default)
[root@docker ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - generic Running tcp://192.168.56.102:2376 v17.09.0-ce
可以看见在192.168.101.14环境上为远程主机192.168.56.102创建的machine
[root@docker ~]# docker run -d --name=nginx nginx(本地没有nginx镜像)
b1f08986f6d5dbb1ede699e915bde734bab278fbe70f93af06ec2267fae2fef3
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1f08986f6d5 nginx "nginx -g 'daemon ..." 4 seconds ago Up 3 seconds 80/tcp nginx
[root@docker ~]# docker-machine ssh default
Last login: Sat Nov 4 18:51:49 2017 from 192.168.56.1
[root@default ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1f08986f6d5 nginx "nginx -g 'daemon ..." 23 seconds ago Up 22 seconds 80/tcp nginx
现在查看远程主机是否创建了容器:
[root@localhost ~]# docker ps -a
could not read CA certificate "/root/.docker/machine/machines/default/ca.pem": open /root/.docker/machine/machines/default/ca.pem: no such file or directory
报错原因:
[root@localhost ~]# unset DOCKER_TLS_VERIFY
[root@localhost ~]# unset DOCKER_CERT_PATH
[root@localhost ~]# unset DOCKER_MACHINE_NAME
[root@localhost ~]# unset DOCKER_HOST
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1f08986f6d5 nginx "nginx -g 'daemon ..." 8 minutes ago Up 8 minutes 80/tcp nginx
可以发现,为远程主机创建容器成功
[root@docker ~]# docker pull registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc:v4
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest b72d63324dbb 13 hours ago 108MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4 6c5128aaff05 2 days ago 464MB
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest b72d63324dbb 13 hours ago 108MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4 6c5128aaff05 2 days ago 464MB
可以看见两主机的镜像同步,也是容器也是同步的
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST
执行上面将machine的环境变量取消就可以返回原来的环境了:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_init v1 383ff3502443 26 hours ago 448MB
centos_nginx v8 6f792dc07c35 2 days ago 464MB
centos_nginx v7 9e875385d6be 2 days ago 464MB
centos_nginx v6 959fdf4d4288 2 days ago 464MB
centos_nginx v5 5c1131306686 2 days ago 464MB
registry.cn-hangzhou.aliyuncs.com/wadeson/jsonhc v4 6c5128aaff05 2 days ago 464MB
192.168.101.14:5000/centos_nginx v4 6c5128aaff05 2 days ago 464MB
centos_nginx v4 6c5128aaff05 2 days ago 464MB
centos_nginx v3 0e49a2c0562f 2 days ago 464MB
centos_nginx v2 2031faf8894a 2 days ago 464MB
centos_nginx v1 78d18f16e757 3 days ago 464MB
registry latest 2ba7189700c8 9 days ago 33.3MB
ubuntu latest 747cb2d60bbe 3 weeks ago 122MB
centos latest 196e0ce0c9fb 7 weeks ago 197MB
而如果需要返回machine环境就继续执行machine环境变量就行,这种方式很好的隔离了本地和远程镜像和容器
docker之docker-machine用法的更多相关文章
- 理解Docker(1):Docker 安装和基础用法
本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...
- Docker 安装和基础用法
理解Docker(1):Docker 安装和基础用法 本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 ...
- Docker 概念及基本用法
Docker 概念及基本用法 一. 本节内容简介 本实验中我们初步接触Docker的概念和基本用法.需要依次完成下面几项任务: 理解Docker是什么 学习如何在Linux上安装Docker 学习如何 ...
- ubuntu-docker入门到放弃(二)docker初探(基本用法及命令)
一.使用公共镜像 docker有一个开源的镜像管理系统,上面有很多常见的images,如mysql,nginx,tomcat等,可以直接根据自己的需求下载下来用,还有系统images,如redhat, ...
- 【Docker】Docker概述
[Docker] Docker可以说是近几年非常热门的技术之一了.不管是别人敦促我还是从自己的想法来说,都觉得Docker这玩意儿肯定是要好好学习一下的,无奈没啥时间专门播出来给Docker,一直以来 ...
- docker 2 docker介绍
docker是基于go语言实现的云开源项目 docker的主要目标是‘build ,ship and run any app,anywhere’,也就是说通过对应用程序组件的封装,分发,部署,运行等生 ...
- 【docker】docker安装和使用
一.docker简介: docker是容器技术的一个代表,而容器技术是将程序打包和隔离的一种技术,其实它并不是一个新技术,之前在linux内核中早已存在,真正被大众所用所了解是因为docker的出现. ...
- docker 系列 - Docker 安装和Hub Mirror地址设置
======================为什么要使用Docker?======================= 容器真是个好东西, (1)容器能提供隔离性; (2)容器能打包环境, 简化部署难度 ...
- 【Docker】docker常用指令
开启docker,并设置开机自启动 老式写法: service docker start chkconfig docker on 新式写法: systemctl start docker.servic ...
- Docker 0x13: Docker 构建集群/服务/Compose/分布式服务栈
目录 Docker 构建集群/服务/Compose/分布式服务栈 集群 初始化集群服务 安装docker-machine 管理节点和工作节点 docker集群构建完成 集群中部署应用 集群服务访问特性 ...
随机推荐
- STP RSTP
一.透明网桥 1.对于一般的透明网桥来说,通常都具有以下的特点: +拓展LAN的能力 +自主动态学习站点的地址信息 当网桥的某个端口上收到含有某个源MAC地址的数据帧时,它就把该MAC地址和接收该数据 ...
- [UE4]Visiblity、Render Opacity
Visiblity 1.Visible 默认是visible,可见,并且可响应事件(如点击事件) 2.Collapsed 不可见.不响应事件.不占用容器空间 3.Hidden 不可见,不响应事件,但占 ...
- [UE4]在蓝图中判断一个对象是否有效
- 超实用!9个目前流行的MATERIAL DESIGN前端框架
http://www.uisdc.com/material-design-frameworks-top-9 谷歌推出的Material Design风格已见有一些APP UI采用,视觉和交互体验都很棒 ...
- AnimDynamics简介
转自:http://www.cnblogs.com/corgi/p/5405452.html AnimDynamics简介 AnimDynamics是UE4.11 Preview 5测试版本发布的An ...
- vue2.0-组件传值
父组件给子组件传值,子组件用props接收 例子:两个组件,一个是父组件标签名是parent,一个是子组件标签名是child,并且child组件嵌套在父组件parent里,大概的需求是:我们子组件里需 ...
- 学习js第三天小结
1.冒泡排序分析: 例:将数组[9,8,7,6,5,4,3,2,1,0]按照从小打大的顺序进行冒泡排序. 演变过程: 第一趟: 8,7,6,5,4,3,2,1,0,9 比较了9次: 第二趟: 7 ...
- day6需要记忆(元组字典集合)
一:基本使用:(元组 tuple)优先掌握的操作1.按索引取值(正向取+反向取):只能取2.切片(顾头不顾尾,步长)3.长度 len()4.成员运算in和not in5.循环需要掌握的操作1.cou ...
- 2012年第三届蓝桥杯Java本科组省赛试题解析
题目地址:https://wenku.baidu.com/view/326f7b4be518964bcf847c96.html?rec_flag=default => 百度文档 题目及解析 ...
- linux中的IP地址的修改
一,图形界面,setup 二,修改文件 1,修改主机名字 vi /etc/sysconfig/network NETWORKING=yes #HOSTNAME=localhost.localdomai ...