码迷,mamicode.com
首页 > 其他好文 > 详细

DOCKER学习_017:Docker-Compose介绍

时间:2020-01-07 00:57:33      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:声明   查看   停止   htm   chm   pre   var   minutes   secret   

dockers三驾马车

  •  Docker Machine
  •  Docker Swarm
  •  Docker Compose

一 Docker Compose介绍

Docker Compose是一个定义和运行多容器应用的单机编排工具。通过Docker Compose你可以使用一个单一的YAML文件来配置多个应用服务,通过一条命令,就可以将所有配置的服务全部启动起来。

1.1 使用Docker Compose的三个步骤

  • 使用Dockerfile定义环境,这样可以确保其在任意地方运行

  • 使用docker-compose.yml文件定义服务,这样它们就可以在独立环境中一起运行

  • 运行docker-compose up使用docker-compose启动所有应用

1.2 Docker Compose可以管理应用的整个生命周期

  • 启动、停止、重建服务

  • 查看服务的运行状态

  • 流式输出服务日志

  • 对服务执行一次性命令

二 Docker Compose安装使用

2.1 二进制安装

下载地址:https://github.com/docker/compose/releases

技术图片

 

 

同时还有安装方式,也可以wget下载上面的文件,本身也是一个二进制文件

 [root@docker-server3 ~]# curl -L https://github.com/docker/compose/releases/download/1.25.0/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    470      0 --:--:--  0:00:01 --:--:--   470
100 16.2M  100 16.2M    0     0   767k      0  0:00:21  0:00:21 --:--:-- 1622k

[root@docker-server3 ~]# chmod +x /usr/local/bin/docker-compose

[root@docker-server3 ~]# docker-compose --version

docker-compose version 1.25.0, build 0a186604

[root@docker-server3 ~]# mkdir /docker-compose

[root@docker-server3 ~]# cd /docker-compose

2.2 简单使用

写一个简单的dockerpose文件

[root@docker-server3 docker-compose]# vim docker-compose.yml

技术图片

version: 3
services:
  httpd-test:
    image: httpd:2.4

[root@docker-server3 docker-compose]# docker-compose up -d

Creating network "docker-compose_default" with the default driver
Pulling httpd-test (httpd:2.4)...
2.4: Pulling from library/httpd
8ec398bc0356: Already exists
354e6904d655: Pull complete
27298e4c749a: Pull complete
10e27104ba69: Pull complete
36412f6b2f6e: Pull complete
Digest: sha256:769018135ba22d3a7a2b91cb89b8de711562cdf51ad6621b2b9b13e95f3798de
Status: Downloaded newer image for httpd:2.4
Creating docker-compose_httpd-test_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
06a7e97739b2        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        80/tcp              docker-compose_httpd-test_1

删除然后在执行,就会拉去一个一样的镜像

[root@docker-server3 docker-compose]# docker rm -f 06a7e97739b2

[root@docker-server3 docker-compose]# docker-compose up -d

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS               NAMES
d184230f2de6        httpd:2.4           "httpd-foreground"   3 seconds ago       Up 2 seconds        80/tcp              docker-compose_httpd-test_1

同时创建两个容器,修改compose文件

[root@docker-server3 docker-compose]# vi docker-compose.yml

version: 3
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]#  cat /data/index.html

just a test

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                NAMES
8b60d28de874        httpd:2.4           "httpd-foreground"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   6 minutes ago       Up 6 minutes        80/tcp               docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# docker exec -it 8b60d28de874 /bin/bash

root@8b60d28de874:/usr/local/apache2# ls /var/www/html/
ca_download  database  index.html  job_logs  psc  redis  registry  secret
root@8b60d28de874:/usr/local/apache2# cat /var/www/html/index.html 
just a test

[root@docker-server3 docker-compose]# vi docker-compose.yml 

version: 3
services:
  httpd-test:
    image: httpd:2.4
  httpd-test-2:
    image: httpd:2.4
    volumes:
      - "/data:/var/www/html"
    ports:
      - "80:80"
  httpd-test-3:
    image: httpd:2.4
    volumes:
      - "/data:/usr/local/apache2/htdocs"
    ports:
      - "8080:80"

[root@docker-server3 docker-compose]# docker-compose up -d

docker-compose_httpd-test_1 is up-to-date
docker-compose_httpd-test-2_1 is up-to-date
Creating docker-compose_httpd-test-3_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
a3d24a4c5d25        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1
8b60d28de874        httpd:2.4           "httpd-foreground"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
d184230f2de6        httpd:2.4           "httpd-foreground"   14 minutes ago      Up 14 minutes       80/tcp                 docker-compose_httpd-test_1

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080

just a test

[root@docker-server3 docker-compose]# echo 111 > /data/111.html

[root@docker-server3 docker-compose]# curl http://192.168.132.133:8080/111.html

111

以上就是docker的一个简答的操作

2.3 docker-compose的常用指令

在上面的一个简单示例中,我们已经使用了docker-compose up来启动一个docker-compose.yml文件定义的服务。刚刚通过docker-compose up虽然启动了服务,当是docker-compose指令却在前台执行,如果需要将其放入后台运行,可以使用-d参数:

docker-compose up -d      #后台运行
docker-compose -f xx.yaml up -d    #如果yml文件不是docker-compose.yml需要使用-f强制指定
docker-compose down     #删除容器
docker-compose ps        #作用也是查看容器,但是它只能查看docker-compose启动的容器,使用docker run启动的容器无法看到
docker-compose start
docker-compose stop
docker-compose restart 
docker-compose logs

[root@docker-server3 docker-compose]# docker-compose down

Stopping docker-compose_httpd-test-3_1 ... done
Stopping docker-compose_httpd-test-2_1 ... done
Stopping docker-compose_httpd-test_1   ... done
Removing docker-compose_httpd-test-3_1 ... done
Removing docker-compose_httpd-test-2_1 ... done
Removing docker-compose_httpd-test_1   ... done
Removing network docker-compose_default

[root@docker-server3 docker-compose]# docker-compose up -d 

Creating network "docker-compose_default" with the default driver
Creating docker-compose_httpd-test-3_1 ... done
Creating docker-compose_httpd-test_1   ... done
Creating docker-compose_httpd-test-2_1 ... done

[root@docker-server3 docker-compose]# docker ps -a

CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
ddf6b2a8f4bb        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp     docker-compose_httpd-test-2_1
a1dabe398019        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        80/tcp                 docker-compose_httpd-test_1
81030fea5437        httpd:2.4           "httpd-foreground"   6 seconds ago       Up 5 seconds        0.0.0.0:8080->80/tcp   docker-compose_httpd-test-3_1

[root@docker-server3 docker-compose]# docker-compose ps -a

            Name                    Command        State          Ports        
-------------------------------------------------------------------------------
docker-compose_httpd-test-2_1   httpd-foreground   Up      0.0.0.0:80->80/tcp  
docker-compose_httpd-test-3_1   httpd-foreground   Up      0.0.0.0:8080->80/tcp
docker-compose_httpd-test_1     httpd-foreground   Up      80/tcp

启动停止的操作

技术图片

前面就是对docker-compose的一个简单介绍的操作,后续在仔细研究docker-compose的语法以及案例


博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!

DOCKER学习_017:Docker-Compose介绍

标签:声明   查看   停止   htm   chm   pre   var   minutes   secret   

原文地址:https://www.cnblogs.com/zyxnhr/p/12158816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!