利用Docker-Machine创建虚拟Docker主机

## 下载docker-machine  https://github.com/docker/machine/releases
sucd@homestead:~$ curl -L https://github.com/docker/machine/releases/download/v0.16.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
## 查看版本号
sucd@homestead:~$ docker-machine -v
docker-machine version 0.16.0, build 702c267f
## 下载并安装virtualbox 参见 https://blog.51cto.com/autophp/2320962
[sucd@localhost ~]$ VBoxManage -v 报错
[sucd@localhost ~]$ sudo /sbin/vboxconfig
提示先安装gcc
[sucd@localhost ~]$ sudo yum -y install gcc
[sucd@localhost ~]$ sudo /sbin/vboxconfig
[sucd@localhost ~]$ VBoxManage -v
5.2.22r126460
成功
## 下载boot2docker.iso(一个基于Tiny Core Linux的系统,里面Docker啥的都帮你搞定了 v18.06.1-ce版本) 
https://github.com/boot2docker/boot2docker/releases/
## 首先检查 /home/sucd/.docker/machine/cache 是否存在。不存在则创建
## 将 boot2docker.iso 文件 拷贝到 **/cache 上述目录下
## 创建虚拟机v1
[sucd@localhost ~]$ docker-machine create --driver=virtualbox v1
[sucd@localhost ~]$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
v1     -        virtualbox   Running   tcp://192.168.99.100:2376           v18.09.0   
说明虚拟机创建成功
## docker-machine相关命令
doceker-machine ls   ## 列出所有虚拟机
doceker-machine ip 虚拟机名称  ## 查看IP地址
doceker-machine stop/start 虚拟机名称 ## 停止和开启
doceker-machine env 虚拟机名称 ## 环境变量
doceker-machine ssh 虚拟机名称 ## SSH方式连接
## 移植images,具体操作步骤如下
[sucd@localhost ~]$ docker pull registry
[sucd@localhost ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              nginx               14d81ad3d755        44 hours ago        303MB
centos              httpd               bbe52af5c400        44 hours ago        333MB
centos              latest              75835a67d134        6 weeks ago         200MB
registry            latest              2e2f252f3c88        2 months ago        33.3MB
[sucd@localhost ~]$ docker run -d -p 5000:5000 --name registry registry
85dfccab3f3a70da31068871be7ef56fd2e9eb651f900ee0c72d914090f3a46d
[sucd@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
85dfccab3f3a        registry            "/entrypoint.sh /etc…"   12 seconds ago      Up 7 seconds        0.0.0.0:5000->5000/tcp   registry
[sucd@localhost ~]$ docker tag centos:httpd localhost:5000/web
[sucd@localhost ~]$ docker push localhost:5000/web
[sucd@localhost ~]$ curl http://localhost:5000/v2/_catalog
{"repositories":["web"]}
## 在虚拟机中pull镜像
[sucd@localhost ~]$ docker-machine ssh v1
docker@v1:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker@v1:~$ docker pull 192.168.99.1:5000/web
Using default tag: latest
Error response from daemon: Get https://192.168.99.1:5000/v2/: http: server gave HTTP response to HTTPS client
提示报错,需要用hppts,我们可以让其不用https, 在主机 /etc/docker/daemon.json文件中加入 "insecure-registies":["192.168.99.1:5000"] 
-- 主机中
[sucd@localhost ~]$ sudo vim /etc/docker/daemon.json
[sucd@localhost ~]$ cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://z6z6dcky.mirror.aliyuncs.com"],
  "insecure-registries":["192.168.99.1:5000"]
}
[sucd@localhost ~]$ sudo systemctl daemon-reload 
[sucd@localhost ~]$ sudo systemctl restart docker
[sucd@localhost ~]$ docker start registry 
-- 虚拟机中(先配置文件后退出)
docker@v1:~$ cat /etc/docker/daemon.json 
{
  "insecure-registries":["192.168.99.1:5000"]   
}
[sucd@localhost ~]$ docker-machine restart v1
[sucd@localhost ~]$ docker-machine ssh v1
docker@v1:~$ docker pull 192.168.99.1:5000/web

利用Docker-Machine在虚拟机中使用compose启动nginx负载均衡实例,设置IP转发在windows中访问

## 配置虚拟机docker用户环境变量(1、虚拟机中创建 .local/bin/目录  2、复制宿主机的.bash_profile到虚拟机 3、重新登录虚拟机验证)
[sucd@localhost ~]$ docker-machine ssh v1
docker@v1:~$ mkdir -p .local/bin/
[sucd@localhost ~]$ docker-machine scp /home/sucd/.bash_profile v1:/home/docker/
[sucd@localhost ~]$ docker-machine ssh v1
docker@v1:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/apps/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/docker/.local/bin:/home/docker/bin
## 拷贝 docker-compose
[sucd@localhost ~]$ docker-machine scp /usr/local/bin/docker-compose v1:/home/docker/.local/bin/
/home/docker/.local/bin/ 是虚拟机用户的一个环境变量(也可以自己设置)
docker@v1:~$ docker-compose -v
docker-compose version 1.23.1, build b02f1306
## 拷贝之前做的 compose配置文件
[sucd@localhost ~]$ docker-machine scp -r mycompose/ v1:/home/docker
## 虚拟机创建一个空文件夹 nginx(不用进入虚拟机)
[sucd@localhost ~]$ docker-machine ssh v1 mkdir nginx
## 挂载虚拟机目录到宿主机目录
[sucd@localhost ~]$ mkdir mnginx
[sucd@localhost ~]$ docker-machine mount v1:/home/docker/nginx /home/sucd/mnginx/
You must have a copy of the sshfs binary locally to use the mount feature
## 提示需要安装 sshfs
[sucd@localhost ~]$ sudo yum -y install epel-release
[sucd@localhost ~]$ sudo yum -y install fuse-sshfs
## 继续挂载
[sucd@localhost ~]$ docker-machine mount v1:/home/docker/nginx /home/sucd/mnginx/
## 将nginx目录文件全部复制到 mnginx/
[sucd@localhost mnginx]$ cd /home/sucd/nginx/
[sucd@localhost nginx]$ cp . /home/sucd/mnginx/ -r
## 进入虚拟机中并修改compose配置文件
[sucd@localhost ~]$ docker-machine ssh v1
docker@v1:~$ vi mycompose/docker-compose.yaml
docker@v1:~$ cat mycompose/docker-compose.yaml  
services: 
  nginx: 
    container_name: mynginx
    image: "nginx"
    ports: 
      - "9090:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.2
    healthcheck: 
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

  web1: 
    container_name: web1
    image: "web"
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/web1/:/var/www/html/"
    networks:
      mynginx-net:
        ipv4_address: 192.156.0.6
    healthcheck: 
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

  web2: 
    container_name: web2
    image: "web"
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/docker/nginx/web2/:/var/www/html/"
    networks: 
      mynginx-net: 
        ipv4_address: 192.156.0.3
    healthcheck:
      test: ["CMD", "curl","-s","-f", "http://localhost:80"]
      interval: 5s
      timeout: 5s
      retries: 3

networks: 
  mynginx-net: 
    driver: bridge
    ipam: 
      config: 
        - subnet: 192.156.0.0/16 
version: "3"
## 进入mycompose并且启动多个服务
docker@v1:~$ cd mycompose/
docker@v1:~/mycompose$ docker-compose up -d
## 测试,宿主机中访问虚拟机服务web1和web2交替出现说明成
[sucd@localhost ~]$ curl $(docker-machine ip v1):9090
web1
[sucd@localhost ~]$ curl $(docker-machine ip v1):9090
web2

Docker Swarm + Docker-machine创建集群

## 先创建两个虚拟机
[sucd@localhost ~]$ docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.90.1/24" m1
[sucd@localhost ~]$ docker-machine create -d virtualbox --virtualbox-hostonly-cidr "192.168.90.1/24" v1
[sucd@localhost ~]$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
m1     -        virtualbox   Running   tcp://192.168.90.100:2376           v18.09.0   
v1     -        virtualbox   Running   tcp://192.168.90.101:2376           v18.09.0 
## 初始化一个集群 
[sucd@localhost ~]$ docker-machine ssh m1
docker@m1:~$ docker swarm init --advertise-addr 192.168.99.100
docker@m1:~$ docker swarm join-token worker 
## 加入节点
[sucd@localhost ~]$ docker-machine ssh v1 docker swarm join --token SWMTKN-1-0zlzmet1ggrmlbeijj9oalsotaj2edlqnkoezuyi7h397srims-5kxodjp43mklqwfp1uh46diak 192.168.90.100:2377
This node joined a swarm as a worker.
docker@m1:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
4a9jw24oq7hx0qenh5ayqw3v7 *   m1                  Ready               Active              Leader              18.09.0
hy5hw7xpvwmlhuzsikp40x39m     v1                  Ready               Active                                  18.09.0
## 创建一个纯净的apache镜像
[sucd@localhost ~]$ docker pull httpd
[sucd@localhost ~]$ docker tag httpd:latest localhost:5000/httpd
[sucd@localhost ~]$ docker push localhost:5000/httpd
[sucd@localhost ~]$ docker rmi localhost:5000/httpd:latest 
镜像的网站路径: /usr/local/apache2/htdocs/
配置文件: /usr/local/apach2/conf/httpd.conf
## 创建服务
在m1中  加入/etc/docker/daemon.json文件
sudo /etc/init.d/docker restart   # 重启docker
docker pull 192.168.90.1:5000/httpd
docker tag 192.168.90.1:5000/httpd httpd && docker rmi 192.168.90.1:5000/httpd
在m1中创建空文件夹web,并添加一个index.html文件
doceker-machine ssh m1 mkdir web
在v1中执行相同的步骤
## 最后在m1中执行创建服务
docker@m1:~$ docker service create \
  --replicas 2 \
  --name myweb \
  --publish 9091:80 \
  --mount type=bind,source=/home/docker/web/,target=/usr/local/apache2/htdocs/ \
  httpd
## 查看服务列表
docker@m1:~$ docker service ls 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
v03oigi0wp0z        myweb               replicated          2/2                 httpd:latest        *:9090->80/tcp