Total Pageviews

Wednesday, 25 July 2018

Docker三剑客之docker-compose和docker-machine

三剑客简介

docker-machine

docker技术是基于Linux内核的cgroup技术实现的,那么问题来了,在非Linux平台上是否就不能使用docker技术了呢?答案是可以的,不过显然需要借助虚拟机去模拟出Linux环境来。
docker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。

docker-compose

docker镜像在创建之后,往往需要自己手动pull来获取镜像,然后执行run命令来运行。当服务需要用到多种容器,容器之间又产生了各种依赖和连接的时候,部署一个服务的手动操作是令人感到十分厌烦的。
dcoker-compose技术,就是通过一个.yml配置文件,将所有的容器的部署方法、文件映射、容器连接等等一系列的配置写在一个配置文件里,最后只需要执行docker-compose up命令就会像执行脚本一样的去一个个安装容器并自动部署他们,极大的便利了复杂服务的部署。

docker-swarm

swarm是基于docker平台实现的集群技术,他可以通过几条简单的指令快速的创建一个docker集群,接着在集群的共享网络上部署应用,最终实现分布式的服务。

docker-compose 的工作流程

使用Compose仅需要三步:
1、用一个定义你的应用程序的环境,Dockerfile这样它就可以在任何地方再现。
2、定义组成您的应用程序的服务,docker-compose.yml 以便它们可以在隔离的环境中一起运行。
3、运行docker-compose up和撰写启动并运行您的整个应用程序。
参考文章:https://docs.docker.com/compose/overview/

docker-compose安装布署

[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    396      0 --:--:--  0:00:01 --:--:--   397
100 10.3M  100 10.3M    0     0   678k      0  0:00:15  0:00:15 --:--:-- 1876k
[root@docker ~]# chmod +x /usr/local/bin/docker-compose 
[root@docker ~]# docker-compose --version
docker-compose version 1.21.2, build a133471

docker-compose管理命令介绍

Compose具有管理应用程序整个生命周期的命令:

  • 启动,停止和重建服务
  • 查看正在运行的服务的状态
  • 流式传输运行服务的日志输出
  • 在服务上运行一次性命令
[root@docker ~]# docker-compose --help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information
docker-compose 运行时是需要指定service名称,可以同时指定多个,也可以不指定。不指定时默认就是对配置文件中所有的service执行命令。
-f #用于指定配置文件
-p #用于指定项目名称
docker-compose build
用来创建或重新创建服务使用的镜像
docker-compose build service_a
创建一个镜像名叫service_a
docker-compose kill
用于通过容器发送SIGKILL信号强行停止服务
docker-compose logs
显示service的日志信息
docker-compose pause/unpause
docker-compose pause #暂停服务
docker-compose unpause #恢复被暂停的服务
docker-compose port
用于查看服务中的端口与物理机的映射关系
docker-compose port nginx_web 80
查看服务中80端口映射到物理机上的那个端口
dokcer-compose ps
用于显示当前项目下的容器
注意,此命令与docker ps不同作用,此命令会显示停止后的容器(状态为Exited),只征对某个项目。
docker-compose pull
用于拉取服务依赖的镜像
docker-compose restart
用于重启某个服务中的所有容器
docker-compose restart service_name
只有正在运行的服务可以使用重启命令,停止的服务是不可以重启
docker-compose rm
删除停止的服务(服务里的容器)
-f #强制删除
-v #删除与容器相关的卷(volumes)
docker-compose run
用于在服务中运行一个一次性的命令。这个命令会新建一个容器,它的配置和srvice的配置相同。
但两者之间还是有两点不同之处
1、run指定的命令会直接覆盖掉service配置中指定的命令
2、run命令启动的容器不会创建在service配置中指定的端口,如果需要指定使用--service-ports指定
docker-compose start/stop
docker-compose start 启动运行某个服务的所有容器
docker-compose stop 停止运行某个服务的所有容器
docker-compose scale
指定某个服务启动的容器个数
[root@docker ~]# docker-compose scale --help
Numbers are specified in the form `service=num` as arguments.
For example:

    $ docker-compose scale web=2 worker=3

This command is deprecated. Use the up command with the `--scale` flag
instead.

Usage: scale [options] [SERVICE=NUM...]

Options:
  -t, --timeout TIMEOUT      Specify a shutdown timeout in seconds.
                             (default: 10)

docker-compose配置文件实例

一个docker-compose.yml的实例文件如下
version: "3"
services:
  nginx:
    container_name: web-nginx
    image: nginx:latest
    restart: always
    ports:
      - 80:80
    volumes:
    - ./webserver:/webserver
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf
下面将配置文件做个简单的解释说明
docker-compose的配置文件是一个.yml格式的文件
第一部分
version: "3"  #指定语法的版本
第二部分
services:     #定义服务
  nginx:      #服务的名称,-p参数后接服务名称
    container_name: web-nginx    #容器的名称
    image: nginx:latest          #镜像
    restart: always
    ports:                       #端口映射
      - 80:80
第三部分
volumes:       #物理机与容器的磁盘映射关系
    - ./webserver:/webserver
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf
整体的目录结构如下
[root@docker docker]# tree ./
./
├── docker-compose.yml
├── nginx
│?? └── nginx.conf
└── webserver
    └── index.html

2 directories, 3 files
配置文件如下
[root@docker docker]# cat webserver/index.html 
welcome to nginx server!!!!!!!!!
[root@docker docker]# cat nginx/nginx.conf
#user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    client_max_body_size 10m;  
    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /webserver;
        index  index.html index.htm;
    }

}
    include /etc/nginx/conf.d/*.conf;
}
使用命令启动容器
[root@docker docker]# docker-compose up -d
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ... 
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating nginx-server ... done
[root@docker docker]# lsof -i :80
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
docker-pr 891 root    4u  IPv6 1187080      0t0  TCP *:http (LISTEN)
[root@docker docker]# docker ps |grep nginx
07ca899cc44b   nginx:1.14   "nginx -g 'daemon ..."   29 seconds ago  Up 28 seconds  0.0.0.0:80->80/tcp  nginx-server
#如果启动时不指定里面的服务名称,就是直接启动配置文件里所有的服务
浏览器测试是否正常访问
容器技术|Docker三剑客之docker-compose
然后我们修改相应的首页文件如下
[root@docker docker]# cat webserver/index.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>welcome to nginx web stie</title>
</head>
<body>
   <h2>欢迎来nginx站点</h2>
</body>
</html>
再次打开浏览器查看效果.

from http://blog.51cto.com/mingongge/2129754 
-------------


Docker三剑客之docker-machine

docker-machine是什么?

ocker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。

Docker 与 Docker Machine 的区别

Docker 是一个 Client-Server 架构的应用,Docker是大家对Docker Engine简称。

Docker包括以下三个部分:

  • 1、Docker daemon
  • 2、一套与 Docker daemon 交互的 REST API
  • 3、一个命令行客户端
    下图很清晰的展示了它们之间的关系:
    容器技术|Docker三剑客之docker-machine
Docker Machine 则是一个安装和管理 Docker 的工具。它有自己的命令行工具:docker-machine。

docker-machine安装

[root@master ~]# curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    390      0 --:--:--  0:00:01 --:--:--   390
100 26.7M  100 26.7M    0     0  1618k      0  0:00:16  0:00:16 --:--:-- 3622k
[root@master ~]# chmod +x /tmp/docker-machine 
[root@master ~]# cp /tmp/docker-machine /usr/local/bin/docker-machine
[root@master ~]# ll /usr/local/bin/docker-machine 
-rwxr-xr-x 1 root root 28034848 612 15:24 /usr/local/bin/docker-machine
[root@master ~]# docker-machine -v
docker-machine version 0.14.0, build 89b8332

docker-machine命令

[root@master ~]# docker-machine --help
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.14.0, build 89b8332

Author:
  Docker Machine Contributors - <https://github.com/docker/machine>

Options:
  --debug, -D                   Enable debug mode
  --storage-path, -s "/root/.docker/machine"    Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                 CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                  Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                 Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key              Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                  Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --bugsnag-api-token               BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --help, -h                    show help
  --version, -v                 print the version

Commands:
  active        Print which machine is active
  config        Print the connection config for machine
  create        Create a machine
  env           Display the commands to set up the environment for the Docker client
  inspect       Inspect information about a machine
  ip            Get the IP address of a machine
  kill          Kill a machine
  ls            List machines
  provision     Re-provision existing machines
  regenerate-certs  Regenerate TLS Certificates for a machine
  restart       Restart a machine
  rm            Remove a machine
  ssh           Log into or run a command on a machine with SSH.
  scp           Copy files between machines
  mount         Mount or unmount a directory from a machine with SSHFS.
  start         Start a machine
  status        Get the status of a machine
  stop          Stop a machine
  upgrade       Upgrade a machine to the latest version of Docker
  url           Get the URL of a machine
  version       Show the Docker Machine version or a machine docker version
  help          Shows a list of commands or help for one command

docker-machine active
显示当前的活动主机

docker-machine config
显示连接主机的配置

docker-machine create
创建一个主机

docker-machine env
设置当前的环境与哪个主机通信

docker-machine inspect
查看主机的详细信息

docker-machine ip
查看主机的IP 

docker-machine kill
强制关闭一个主机

docker-machine ls 
查看所有的主机信息

docker-machine provision
重新配置现在主机

docker-machine regenerate-certs
为主机重新生成证书

docker-machine restart
重启主机

docker-machine rm
删除主机

docker-machine ssh
以SSH的方式连接到主机上

docker-machine scp
远程复制

docker-machine status
查看主机的状态

docker-machine stop
停止一个正在运行的主机

docker-machine upgrade
升级主机的docker服务到最新版本

docker-machine version
查看docker-machine版本

常用实例操作

[root@master ~]# docker-machine create -d virtualbox testhost
[root@master ~]# docker-machine create --driver virtualbox testhost
#两个命令的作用相同,创建一个名为testhost的主机,驱动方式是virtualbox

[root@master ~]# docker-machine create -d virtualbox testhost
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"
#报错提示没有发现VBoxManage。因此,需要手工安装,具体安装操作如下。

[root@master ~]# vim  /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc

[root@master ~]# yum search VirtualBox   #查找具体安装版本
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
============== N/S matched: VirtualBox ===================================
VirtualBox-4.3.x86_64 : Oracle VM VirtualBox
VirtualBox-5.0.x86_64 : Oracle VM VirtualBox
VirtualBox-5.1.x86_64 : Oracle VM VirtualBox
VirtualBox-5.2.x86_64 : Oracle VM VirtualBox
[root@master ~]# yum install -y VirtualBox-5.2  #安装

[root@master ~]# /sbin/vboxconfig   #重新加载virtualbox服务
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-693.el7.x86_64
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-693.el7.x86_64

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.
#如果内核版本不一致,会出现上面的报错,需要安装相同的内核版本

[root@master ~]# rpm -ivh kernel-devel-3.10.0-693.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:kernel-devel-3.10.0-693.el7   ################################# [100%]
[root@slave1 ~]# yum install gcc make perl -y
[root@slave1 ~]# rpm -qa kernel\*
kernel-tools-3.10.0-693.el7.x86_64
kernel-devel-3.10.0-693.el7.x86_64
kernel-tools-libs-3.10.0-693.el7.x86_64
kernel-3.10.0-693.el7.x86_64
kernel-headers-3.10.0-862.3.2.el7.x86_64
[root@slave1 ~]# /sbin/vboxconfig 
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.

[root@centos7 ~]# docker-machine create --driver virtualbox testhost
Running pre-create checks...
Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"
#这个报错不用解释了吧,没有开启虚拟化功能,直接打开

[root@centos7 ~]# docker-machine create --driver virtualbox default
Running pre-create checks...
(default) No default Boot2Docker ISO found locally, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v18.05.0-ce
(default) Downloading /root/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.05.0-ce/boot2docker.iso...
(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(default) Copying /root/.docker/machine/cache/boot2docker.iso to /root/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet0"
(default) Waiting for an IP...
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 boot2docker...
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
#可以先安装个默认的虚拟机
[root@centos7 ~]# docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v18.05.0-ce   
[root@centos7 ~]# docker-machine status
Running
#接下来配置环境变量,方便后面直接操作主机
[root@centos7 ~]# docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env default)
[root@centos7 ~]#  eval $(docker-machine env default)
[root@centos7 ~]# docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.05.0-ce, build HEAD : b5d6989 - Thu May 10 16:35:28 UTC 2018
Docker version 18.05.0-ce, build f150324

docker@default:~$ docker --version
Docker version 18.05.0-ce, build f150324
#正常进入虚拟机

实例

两台服务器
  • 本地主机:192.168.22.177
  • 远程主机:192.168.22.175
    可能大家还是不太明白,这个docker-machine到底有什么用?docker容器的好处都知道了。
    而docker-machine可以在本地布署相应环境的同时完成远程docker主机相同环境的布署,减少重复的操作。
1、配置免密登录
[root@test001 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7Ue0bc08pEs+PonJqy7/hyxUhNO4uEegVizX4EJC8J8 root@centos7.3
The key's randomart image is:
+---[RSA 2048]----+
| .oo ...o +      |
|  . o..= = o     |
|   . .=.o + .  . |
|    .oo. + o oo+ |
|    .E  S o ooo.+|
|       . + .o.. .|
|        o + =+.  |
|       . . B.+.  |
|        +++oo..  |
+----[SHA256]-----+
[root@centos7 ~]# ssh-copy-id 192.168.22.175
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.22.175 (192.168.22.175)' can't be established.
ECDSA key fingerprint is SHA256:p6+FPeTxTUx37cwJWJP8cUE9NhcUHSvAppVPyj4aj8c.
ECDSA key fingerprint is MD5:89:6d:f7:46:11:45:2e:fd:21:87:42:bd:62:06:fe:fd.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.22.175's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.22.175'"
and check to make sure that only the key(s) you wanted were added.

[root@test001 ~]# ssh 192.168.22.175
Last login: Tue Apr 24 06:51:06 2018 from 192.168.22.170
[root@test002 ~]# ip add |grep 192.168.22
    inet 192.168.22.175/24 brd 192.168.22.255 scope global ens32
2、创建远程主机
远程主机需要安装有docker环境
[root@test001 ~]# docker-machine create -d generic --generic-ip-address=192.168.22.175 --generic-ssh-user=root --engine-registry-mirror http://ef017c13.m.daocloud.io   dockerhost
Running pre-create checks...
Creating machine...
(dockerhost) 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 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 dockerhost
[root@test001 ~]# docker-machine ls
NAME         ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
dockerhost   -        generic      Running   tcp://192.168.22.175:2376           v1.13.1   
3、配置环境变量
[root@centos7 ~]# docker-machine env dockerhost
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.22.175:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/dockerhost"
export DOCKER_MACHINE_NAME="dockerhost"
# Run this command to configure your shell: 
# eval $(docker-machine env dockerhost)
[root@test001 ~]# eval $(docker-machine env dockerhost)
[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:32:42 2018 from 192.168.22.177
[root@dockerhost ~]# docker --version
Docker version 1.13.1, build 94f4240/1.13.1
4、运行一个容器
[root@test001 ~]# docker run -d nginx:1.13
Unable to find image 'nginx:1.13' locally
Trying to pull repository docker.io/library/nginx ... 
sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35: Pulling from docker.io/library/nginx
f2aa67a397c4: Pull complete 
3c091c23e29d: Pull complete 
4a99993b8636: Pull complete 
Digest: sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
Status: Downloaded newer image for docker.io/nginx:1.13
72efb659ec38d263519c894bf0b5eb3d5ca35af1e3d0e9522abbcc19d8739403
[root@test001 ~]# docker image ls
REPOSITORY          TAG     IMAGE ID       CREATED       SIZE
docker.io/nginx     1.13    ae513a47849c   6 weeks ago   109 MB
[root@test001 ~]# docker-machine ssh dockerhost
Last login: Thu Jun 14 02:58:51 2018 from 192.168.22.177
[root@dockerhost ~]# docker image ls
REPOSITORY          TAG      IMAGE ID      CREATED       SIZE
docker.io/nginx     1.13     ae513a47849c  6 weeks ago   109 MB
 
from http://blog.51cto.com/mingongge/2140402 

 

No comments:

Post a Comment