docker-compose编排规则

Docker-compose简介

Docker常用工具:

	docker-compose # 多容器编排工具,基于 Yaml 格式资源清单
	docker-swarm # 集群化管理器
	docker-machine # 是安装和管理 Docker 的工具,创建具有docker服务的虚拟机的技术

docker-compose介绍

docker-compose是docker官方的多容器编排工具,用于定义和运行多容器 Docker 应用程序的工具。

docker-compose使用步骤

使用 docker-compose.yml 定义构成应用程序的服务和环境,这样它们可以在隔离环境中一起运行

用docker-compose -f /path/docker-compose.yml up -d --build 来启动整个docker应用程序的运行

docker-compose安装

$ http://get.daocloud.io/#install-compose # 获取下载地址
$ curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
备注: 如果宿主机环境相同, 可以直接将下载好的docker-compose文件拷贝过来,chmod权限,即可使用.

用Docker-compose方式部署nextcloud(私有网盘)示例yml文件:

version: "3.9"
services:
  nextcloud:
    image: "nextcloud:latest"
    networks:
      - def_compose_net
    restart: always
    ports:
      - "13001:80"
    depends_on:
      - pgservice
    volumes:
      - type: volume
        source: nextcloud_data
        target: /var/www/html
      - type: bind
        source: /etc/localtime
        target: /etc/localtime
    privileged: true #默认true
    container_name: nextcloud
  pgservice:
    image: "postgres:latest"
    container_name: pgservice
    restart: always
    ports:
      - "13002:5432"
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 12345678
    networks:
      - def_compose_net
    volumes:
      - type: bind
        source: /etc/localtime
        target: /etc/localtime
      - type: bind
        source: /etc/timezone
        target: /etc/timezone
      - type: volume
        source: pgdata
        target: /var/lib/postgresql/data
      - type: bind
        source: /opt/FaceService
        target: /opt/FaceService
volumes:
  nextcloud_data:
  pgdata:
    external: false
networks:
  def_compose_net:
    external: false
    name: def_compose_net

docker-compose常用命令

$ docker-compose -f 文件路径/文件名 [选项]
	-f  # 指定使用的 yaml 文件位置	
	version # 查看版本
	ps # 显示所有容器信息s
	restart # 重新启动容器
	logs # 查看日志信息
	config -q # 验证 yaml 配置文件是否正确
	start/stop # 启动/停止容器
	up -d # 启动容器项目
	pause # 暂停容器
	unpause # 恢复暂停
	rm # 删除容器

Docker-compose 基本语法

version: '2'
services:
  web:
    image: dockercloud/hello-world
    ports:
      - 8080
    networks:
      - front-tier
      - back-tier
 
  redis:
    image: redis
    links:
      - web
    networks:
      - back-tier
 
  lb:
    image: dockercloud/haproxy
    ports:
      - 80:80
    links:
      - web
    networks:
      - front-tier
      - back-tier
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock 
 
networks:
  front-tier:
    driver: bridge
  back-tier:
driver: bridge

yml文件配置文件详解

1、version

指定本yml一从的compose 哪个版本制定的。

2、image

services: #定义服务
  web: # web服务
    image: hello-world # 启动服务使用的镜像

指定容器运行的镜像,格式:

image: redis # 镜像名称
image: ubuntu:14.04 #镜像:版本号
image: tutum/influxdb # 个人用户级别的镜像
image: example-registry.com:4000/postgresql # 非官方仓库的镜像
image: a4bc65fd # 镜像ID

3、build

服务除了可以基于指定的镜像,还可以基于一份 "Dockerfile" ,在使用 up 启动之时执行构建任务,这个构建标签就是 build,它可以指定 Dockerfile 所在文件夹的路径。Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器

build: /path/to/build/dir/

也可以是相对路径,只要上下文确定就可以读取到 Dockerfile

build: ./dir
build:
  context: /home/transport或./transport#context:指定Dockerfile文件所在的路径
  dockerfile: Dockerfile#dockerfile:指定context指定的目录下面的Dockerfile的名称(默认为Dockerfile)
args:#args:Dockerf
   args: # 指定输入环境变量
    buildno: 1 # 环境变量
    password: secret # 环境变量
image: webapp:tag # 命名的镜像名称,根据此名称启动

4、command 覆盖容器启动的默认命令。

command: bundle exec thin -p 3000

command: [bundle, exec, thin, -p, 3000]

5、container_name:<项目名称><服务名称><序号>

container_name: app # 指定自定义容器名称,而不是生成的默认名称。

6、depends_on设置依赖关系,建立关联,优先级启动

version: '2'
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres

docker-compose up :以依赖性顺序启动服务。在以下示例中,先启动 db 和 redis ,才会启动 web。
docker-compose up SERVICE :自动包含 SERVICE 的依赖项。在以下示例中,docker-compose up web 还将创建并启动 db 和 redis。
docker-compose stop :按依赖关系顺序停止服务。在以下示例中,web 在 db 和 redis 之前停止。

7、dns

自定义 DNS 服务器,可以是单个值或列表的多个值。

dns: 8.8.8.8

dns:
  - 8.8.8.8
  - 9.9.9.9

8、dns_search

自定义 DNS 搜索域。可以是单个值或列表。

dns_search: example.com
dns_search:
dc1.example.com
dc2.example.com

9、tmpfs

在容器内安装一个临时文件系统。可以是单个值或列表的多个值。

tmpfs: /run
tmpfs:
  - /run
  - /tmp

10、 entrypoint

entrypoint: /code/entrypoint.sh #覆盖容器默认的 entrypoint

11、env_file

env_file: .env
从文件添加环境变量。可以是单个值或列表的多个值。

env_file: # 列表格式
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env

12、environment:镜像变量

添加环境变量。您可以使用数组或字典、任何布尔值,布尔值需要用引号引起来,以确保 YML 解析器不会将其转换为 True 或 False。

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:
 
environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

13、expose

暴露端口的定义,但不映射到宿主机,只被连接的服务访问。
仅可以指定内部端口为参数:

expose:
 - "3000"
 - "8000"

14、 external_links:链接外部容器

external_links: # 将容器的地址注入到host文件里
 - redis_1
 - project_db_1:mysql # 添加别名
 - project_db_1:postgresql

15、extra_hosts

添加主机名映射

extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"

会在此服务的内部容器中 /etc/hosts 创建一个具有 ip 地址和主机名的映射关系:

162.242.195.82 somehost
50.31.209.229 otherhost

16、labels

labels:
  com.example.description: "Accounting webapp"
  com.example.department: "Finance"
  com.example.label-with-empty-value: ""
labels:
 - "com.example.description=Accounting webapp"
 - "com.example.department=Finance"
 - "com.example.label-with-empty-value"

17、links:与 Docker client 的 --link 一样效果,会连接到其它服务中的容器

links:
 - db
 - db:database
 - redis

18、 logging

服务的日志记录配置。

logging:
  driver: syslog # 指定服务容器的日志记录驱动程序,默认值为json-file,有以下三个选项 driver: “json-file”    driver: “syslog”     driver: “none”
  options:
    syslog-address: "tcp://192.168.0.42:123"

19、pid

指定pid名称

pid: "host"

20、port

指定端口的暴露

ports:
 - "3000"
 - "8000:8000"
 - "49100:22"
 - "127.0.0.1:8001:8001"

21、security_opt

为每个容器覆盖默认的标签。简单说来就是管理全部服务的标签。比如设置全部服务的user标签值为USER。

security_opt:
  - label:user:USER
  - label:role:ROLE

label:user:USER # 设置容器的用户标签
label:role:ROLE # 设置容器的角色标签
label:type:TYPE # 设置容器的安全策略标签
label:level:LEVEL # 设置容器的安全等级标签

22、 stop_signal

设置停止容器的替代信号。
stop_signal: SIGUSR1

23、volumes

volumes:
  // 只是指定一个路径,Docker 会自动在创建一个数据卷(这个路径是容器内部的)。
  - /var/lib/mysql
 
  // 使用绝对路径挂载数据卷
  - /opt/data:/var/lib/mysql
 
  // 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。
  - ./cache:/tmp/cache
 
  // 使用用户的相对路径(~/ 表示的目录是 /home/<用户目录>/ 或者 /root/)。
  - ~/configs:/etc/configs/:ro
 
  // 已经存在的命名的数据卷。
  - datavolume:/var/lib/mysql

24、volumes_from

从其它容器或者服务挂载数据卷,可选的参数是 :ro或者 :rw,前者表示容器只读,后者表示容器对数据卷是可读可写的。默认情况下是可读可写的

volumes_from:
  - service_name
  - service_name:ro
  - container:container_name
  - container:container_name:rw

25、cap_add, cap_drop

添加或删除容器拥有的宿主机的功能。

cap_add:
  - ALL
 
cap_drop:
  - NET_ADMIN # 网络管理员权限
  - SYS_ADMIN

26、extends

连接其他文件

extends:
  file: common.yml
  service: webapp

27、network_mode

设置网络模式。

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

28、 networks

配置容器连接的网络,引用顶级 networks 下的条目 。

services:
  some-service:
    networks:
     - some-network
     - other-network

29、ulimits

覆盖容器默认的ulimits

ulimits:
	nproc: 65535
	nofile:
	soft: 20000
	hard: 40000

30、stop_grace_period

指定在容器无法处理 SIGTERM (或者任何 stop_signal 的信号),等待多久后发送 SIGKILL 信号关闭容器
stop_grace_period: 1s # 等待 1 秒
stop_grace_period: 1m30s # 等待 1 分 30 秒
默认的等待时间是 10 秒。

31、secrets

存储敏感数据

version: “3.1”
services:

	mysql:
	    image: mysql
	    environment:
 	        MYSQL_ROOT_PASSWORD_FILE: /run/secrets/my_secret
	    secrets:
		    - my_secret
	    secrets:
		   my_secret:
		   file: ./my_secret.txt

32、restart

no:是默认的重启策略,在任何情况下都不会重启容器。
always:容器总是重新启动。
on-failure:在容器非正常退出时(退出状态非0),才会重启容器。
unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
restart: “no”
restart: always
restart: on-failure
restart: unless-stopped
注:swarm 集群模式,请改用 restart_policy。

33、aliases

同一网络上的其他容器可以使用服务名称或此别名来连接到对应容器的服务

34、healthcheck

用于检测 docker 服务是否健康运行。

healthcheck:
test: [“CMD”, “curl”, “-f”, “http://localhost”] # 设置检测程序
interval: 1m30s # 设置检测间隔
timeout: 10s # 设置检测超时时间
retries: 3 # 设置重试次数
start_period: 40s # 启动后,多少秒开始启动检测程序

34、devices

指定设备映射列表。

devices:
“/dev/ttyUSB0:/dev/ttyUSB0”

35、cgroup_parent

为容器指定父 cgroup 组,意味着将继承该组的资源限制。
cgroup_parent: m-executor-abcd

36、deploy

指定与docker swarm集群服务的部署和运行有关的配置。只在 swarm 模式下才会有用。

理解:
docker run 单机运行单个容器
docker-compose 单机运行多个容器
docker swarm 集群服务
docker service create  创建集群中的单个服务;
docker stack 管理集群中的多个服务;

37、endpoint_mode:访问集群服务的方式。

endpoint_mode: vip
Docker 集群服务一个对外的虚拟 ip。所有的请求都会通过这个虚拟 ip 到达集群服务内部的机器。
endpoint_mode: dnsrr
#DNS 轮询(DNSRR)。所有的请求会自动轮询获取到集群 ip 列表中的一个 ip 地址。

38、labels

#在服务上设置标签。可以用容器上的 labels(跟 deploy 同级的配置) 覆盖 deploy 下的 labels。

39、mode

指定服务提供的模式。

40、replicated

#复制服务,复制指定服务到集群的机器上。

41、global

全局服务,服务将部署至集群的每个节点。

42、replicas

mode 为 replicated 时,需要使用此参数配置具体运行的节点数量。
指定集群中运行容器的数量;

43、resources

配置服务器资源使用的限制,例如上例子,配置 redis 集群运行需要的 cpu 的百分比 和 内存的占用。避免占用资源过高出现异常。


44、restart_policy:

配置如何在退出容器时重新启动容器。

condition:可选 none,on-failure 或者 any(默认值:any)。
delay:设置多久之后重启(默认值:0)。
max_attempts:尝试重新启动容器的次数,超出次数,则不再尝试(默认值:一直重试)。
window:设置容器重启超时时间(默认值:0)。

45、rollback_config

配置在更新失败的情况下应如何回滚服务。

parallelism:一次要回滚的容器数。如果设置为0,则所有容器将同时回滚。
delay:每个容器组回滚之间等待的时间(默认为0s)。
failure_action:如果回滚失败,该怎么办。其中一个 continue 或者 pause(默认pause)。
monitor:每个容器更新后,持续观察是否失败了的时间 (ns|us|ms|s|m|h)(默认为0s)。
max_failure_ratio:在回滚期间可以容忍的故障率(默认为0)。
order:回滚期间的操作顺序。其中一个 stop-first(串行回滚),或者 start-first(并行回滚)(默认 stop-first )。

46、update_config

配置应如何更新服务,对于配置滚动更新很有用。

parallelism:一次更新的容器数。
delay:在更新一组容器之间等待的时间。
failure_action:如果更新失败,该怎么办。其中一个 continue,rollback 或者pause (默认:pause)。
monitor:每个容器更新后,持续观察是否失败了的时间 (ns|us|ms|s|m|h)(默认为0s)。
max_failure_ratio:在更新过程中可以容忍的故障率。
order:回滚期间的操作顺序。其中一个 stop-first(串行回滚),或者 start-first(并行回滚)(默认stop-first)。
注:仅支持 V3.4 及更高版本。

47.memory

持续更新
###: docker-compose配置详解官方文档

参考链接:https://www.cnblogs.com/wang-yy/p/14502718.html

Guess you like

Origin blog.csdn.net/yfanjy/article/details/120952235