Docker | 第七章:Docker-Compose服务编排介绍及使用

前言

前面章节,咱们学习了如何构建本身的镜像文件,如何保存本身的镜像文件。大多都是一个镜像启动。当一个系统须要多个子系统进行配合时,若每一个子系统也就是镜像须要一个个手动启动和中止的话,那估计实施人员也会崩溃的,并且效率也很低,维护的量也就很大了。因此,本章节就来了解下,如何利用官方提供的Compose编排工具按必定的业务规则来合理的进行容器启停工做。html

Compose介绍

Compose是一个用于定义和运行多个Docker容器的编排工具。能够一条命令启动多个容器。主要是解决了容器与容器之间如何管理编排的问题。java

按官网的说明,使用Compose基本上也就分红三步:python

  • 利用Dockerfile定义运行环境
  • 使用docker-compose.yml定义组成应用的各服务
  • 运行docker-compose up启动应用

官网介绍

题外话:我所理解的就是,其实就是个批量工具,如批处理命令同样。mysql

Compose实现原理图:linux

原理图

Compose 中有两个重要的概念:git

  • 服务 (service) :一个应用的容器,实际上能够包括若干运行相同镜像的容器实例。
  • 项目 (project) :由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。

一个项目能够由多个服务(容器)关联而成,Compose 面向项目进行管理,经过子命令对项目中的一组容器进行便捷地生命周期管理。github

Compose 项目由 Python 编写,实现上调用了 Docker 服务提供的 API 来对容器进行管理。所以,只要所操做的平台支持 Docker API,就能够在其上利用 Compose 来进行编排管理。web

题外话:关于Docker API会在下一章节进行详细说明。redis

Compose安装

自己Compose是一个独立的产品,因此须要独立安装的。spring

按官网的说明,Compose能够在MacWindowsLinux等操做系统,本章节主要讲解下关于Linux的安装,其余操做系统能够查看官网的安装指南:https://docs.docker.com/compose/install/#install-compose

安装指南

二进制文件直接安装

此安装方式简单,但可能会有网络链接问题。或者直接去gitHub上的资源列表下载到本地也能够的。笔者下载过程(11M左右)仍是很顺利的,╰( ̄▽ ̄)╮

  • 下载
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
  • 受权
sudo chmod +x /usr/local/bin/docker-compose
  • 查看版本验证是否成功
docker-compose --version

可看见控制台输出:

docker-compose version 1.22.0, build f46880fe

说明已经安装成功了。固然你们能够选择不一样的安装版本了,直接去github上下载便可。

使用pip(Python包管理工具)安装

这种安装方式就是不会受限于网络,但安装比较麻烦。

#安装pip
yum -y install epel-release
yum -y install python-pip
#确认版本
pip --version
#更新pip
pip install --upgrade pip
#安装docker-compose
pip install docker-compose 
#查看版本
docker-compose version

命令补齐

命令补齐功能能够提升输入效率,因此须要安装命令补齐功能的,能够照着官网:https://docs.docker.com/compose/completion/#install-command-completion 说明了相应配置,这里就不进行阐述了。

命令补齐

Compose经常使用命令

在控制窗口,直接输入docker-compose能够看见,其命令的组成及命令的集合了。

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

这里简单说明下一些经常使用的:

build

构建或者从新构建服务。

kill

经过发送SIGKILL信号来强制中止服务容器。支持经过参数来指定发送的信号,如:docker-compose kill -s SIGINT

logs

查看服务的日志输出。

port

打印绑定的公共端口

ps

列出全部的容器或者指定的容器

pull

拉取镜像

rm

输出容器

run

在一个服务上执行一个命令。如docker-compose run ubuntu ping docker.com将会启动一个 ubuntu 服务,执行 ping docker.com 命令。默认状况下,全部关联的服务将会自动被启动,除非这些服务已经在运行中。 该命令相似启动容器后运行指定的命令,相关卷、连接等等都将会按照指望建立。 两个不一样点:

  • 给定命令将会覆盖原有的自动运行命令;
  • 不会自动建立端口,以免冲突。

若是不但愿自动启动关联的容器,可使用 --no-deps 选项,例如

$ docker-compose run --no-deps web python manage.py shell

将不会启动 web 容器所关联的其它容器。

start

启动一个已经存在的服务容器。

stop

中止一个已经运行的容器,但不删除它。

up

构建,(从新)建立,启动,连接一个服务相关的容器。连接的服务都将会启动,除非他们已经运行。 默认状况, docker-compose up 将会整合全部容器的输出,而且退出时,全部容器将会中止。若是使用 docker-compose up -d ,将会在后台启动并运行全部的容器。 默认状况,若是该服务的容器已经存在, docker-compose up 将会中止并尝试从新建立他们(保持使用 volumes-from 挂载的卷),以保证 docker-compose.yml 的修改生效。若是你不想容器被中止并从新建立,可使用 docker-compose up --no-recreate。若是须要的话,这样将会启动已经中止的容器。

pause

暂停容器服务

其余的命令,可直接经过帮助查询下。

docker-compose.yml模版文件经常使用命令

compose的默认模版文件为:docker-compose.yml。和Dockerfile同样,它也是有本身的语法命令的。其中定义的每一个服务都必须经过image指令指定镜像或build指令(须要 Dockerfile)来自动构建。其它大部分指令都跟docker run中的相似。

若是使用build指令,在Dockerfile中设置的选项(例如:CMD, EXPOSE, VOLUME, ENV 等) 将会自动被获取,无需在docker-compose.yml中再次设置。

模版文件有多种写法,例如Version 1 file formatVersion 2 file formatVersion 2.1 file formatVersion 3 file format等。其中,Version 1 file format将逐步被被弃用;Version 2.xVersion 3.x基本兼容,是将来的趋势。

具体说明可查看:https://docs.docker.com/compose/compose-file/

文件写法

各版本对应的支持的模版文件以下:

Compose file format Docker Engine release
3.7 18.06.0+
3.6 18.02.0+
3.5 17.12.0+
3.4 17.09.0+
3.3 17.06.0+
3.2 17.04.0+
3.1 1.13.1+
3.0 1.13.0+
2.4 17.12.0+
2.3 17.06.0+
2.2 1.13.0+
2.1 1.12.0+
2.0 1.10.0+
1.0 1.9.1.+

image

指定为镜像名称或镜像 ID。若是镜像在本地不存在,Compose 将会尝试拉去这个镜像。

例如:

image: frolvlad/alpine-glibc

build

指定 Dockerfile 所在文件夹的路径。 Compose 将会利用它自动构建这个镜像,而后使用这个镜像。

build: /opt/docker

其中build还有一些小选项。

build:
  context: ./webapp
  dockerfile: Dockerfile-alternate
  args:
    - buildno=1
    - user=someuser

context上下文是docker build中很重要的一个概念。构建镜像必须指定contextcontextdocker build 命令的工做目录。默认状况下,若是不额外指定 Dockerfile 的话,会将 Context 下的名为 Dockerfile 的文件做为 Dockerfile

dockerfile选项是值得备选的dockerfileargs是一些提供的参数。

command

覆盖容器启动后默认执行的命令。

command: bundle exec thin -p 3000

links

连接到其它服务中的容器。使用服务名称(同时做为别名)或服务名称:服务别名 (SERVICE:ALIAS) 格式均可以。

links:
 - db
 - db:database
 - redis

使用的别名将会自动在服务容器中的 /etc/hosts 里建立。例如:

172.17.2.186  db
172.17.2.186  database
172.17.2.187  redis

相应的环境变量也将被建立。

dns

配置 DNS 服务器。能够是一个值,也能够是一个列表。

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9

environment

设置环境变量。你可使用数组或字典两种格式。

只给定名称的变量会自动获取它在 Compose 主机上的值,能够用来防止泄露没必要要的数据。

environment:
  RACK_ENV: development
  SESSION_SECRET:

environment:
  - RACK_ENV=development
  - SESSION_SECRET

env_file

从文件中获取环境变量,能够为单独的文件路径或列表。

若是经过 docker-compose -f FILE 指定了模板文件,则 env_file 中路径会基于模板文件路径。

若是有变量名称与 environment 指令冲突,则之后者为准。

env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env

环境变量文件中每一行必须符合格式,支持 # 开头的注释行。

# common.env: Set Rails/Rack environment
RACK_ENV=development

expose

暴露端口,但不映射到宿主机,只被链接的服务访问。

仅能够指定内部端口为参数

expose:
 - "3000"
 - "8000"

volumes

卷挂载路径设置。能够设置宿主机路径 (HOST:CONTAINER) 或加上访问模式 (HOST:CONTAINER:ro)。

volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
 - ~/configs:/etc/configs/:ro

volumes_from

从另外一个服务或容器挂载它的全部卷。

volumes_from:
 - service_name
 - container_name

其余相关命名了或者了解命令的其余配置项,可到官网查看,包括一些选项等,真的蛮多的。。

官网:https://docs.docker.com/compose/compose-file

Compose编排实践

讲了命令后,你们估计和我同样一头雾水,接下来,就以一个实际的启动例子,进行讲解下。这里启动两个先前章节编写过的SpringBoot项目,进行api调用进行举例说明。

0.编写docker-compose.yml文件:

# 表示该文件使用Version 3 文件格式
version: '3.3'
services: 
 springboot-demo-1: 
   # 依赖的镜像,直接使用先前章节自定义的镜像文件
   image: lqdev.cn/springboot:0.1
   # 暴露端口信息
   ports: 
      - "1222:8080"
 springboot-demo-2: 
      # 依赖的镜像,直接使用先前章节自定义的镜像文件
   image: lqdev.cn/springboot:0.1
   # 暴露端口信息
   ports: 
      - "1333:8080"

1.在docker-compose.yml文件所在目录执行

# -d 说明是后台运行
docker-compose up -d

此时能够看见,启动成功了:

[root@izbp16chpwsnff41nrjtfhz java]# docker-compose up -d
Creating java_springboot-demo-1_1 ... done
Creating java_springboot-demo-2_1 ... done

利用ps命令查看,说明已经启动成功了。

[root@izbp16chpwsnff41nrjtfhz java]# docker-compose ps
          Name                        Command               State           Ports         
------------------------------------------------------------------------------------------
java_springboot-demo-1_1   java -Djava.security.egd=f ...   Up      0.0.0.0:1222->8080/tcp
java_springboot-demo-2_1   java -Djava.security.egd=f ...   Up      0.0.0.0:1333->8080/tcp

或者查看下日志输出,能够看见SpringBoot已经启动成功:

日志查看

访问下服务,能够看见能正常输出了:

服务服务


示例就简单的利用imagesports进行简单的运行了一个Comoose示例,你们对于复杂的能够本身试着编写下,对于一个后端的我来讲,这块用到的机会仍是不多了,毕竟都是本身一个微服务,本身启停了。⊙﹏⊙‖∣


参考资料

官方的文档仍是很齐全的,建议你们仍是直接去官网查看相关资料。

  1. https://docs.docker.com/compose/install
  2. https://docs.docker.com/compose/reference/
  3. https://docs.docker.com/compose/compose-file
  4. https://www.hi-linux.com/posts/12554.html

总结

本章节主要介绍了关于Compose编排工具的使用。使用了编排工具后,提供了应用系统部署各个管理应用的便捷性。学习后,你们就能够根据本身的业务需求,业务系统进行合理的编排工做了。通常上仍是利用在微服务的编排上,通常上一个应用系统可能存在这几十个微服务的,因此说合理的利用编排工具能够提升部署效率,同时也能减小出错遗漏的机率。

最后

鉴于做者也是个初学者,水平有限,若文中有错误或者遗漏之处,还望指出,共同进步!

老生常谈

  • 我的QQ:499452441
  • 微信公众号:lqdevOps

公众号

我的博客:http://blog.lqdev.cn

原文地址:http://blog.lqdev.cn/2018/08/12/docker/docker-seven/

相关文章
相关标签/搜索