compose文件格式

定义配置文件中的部分(如构建,部署,依赖,网络等)的顶级键与支持它们作为子主题的选项一起列出。这映射到Compose文件的<key>:<option>:<value>缩进结构

服务配置参考

Compose文件是定义services,networks和volumes的YAML文件。 Compose文件的默认路径是./docker-compose.yml。

服务定义包含应用于为该服务启动的每个容器的配置. 类似:



docker container create
docker network create 
docker volume create



build

  • 在构建时应用的配置选项. 可以指定为包含构建上下文路径的字符串.
version: "3.7"    #版本
services:         #服务
  nginx:          #服务名称
    build: ./dir  #指定构建目录
    image: nginx:tag  #指定基础镜像



context

  • 包含Dockerfile的目录的路径,或者是git存储库的url。
  • 当提供的值是相对路径时,它被解释为相对于Compose文件的位置。此目录也是发送到Docker守护程序的构建上下文。
build:
  context: ./dir



dockerfile

  • Compose使用备用文件来构建。还必须指定构建路径。
build:
  context: .
  dockerfile: Dockerfile-alternate



ARGS

  • 添加构建参数.在dockerfile中定义:
ARG buildno
ARG gitcommithash



  • 指定参数在build指令下构建
build:
  context: .
  args:
    buildno: 1



注意: 在Dockerfile中,如果在FROM指令之前指定ARG,则在FROM下的构建指令中不能使用ARG。
YAML布尔值(true,false,yes,no,on,off)必须用引号括起来,以便解析器将它们解释为字符串。

CACHE_FROM



build:
  context: .
  cache_from:
    - alpine:latest
    - corp/web_app:3.14



LABELS

  • 使用Docker标签将元数据添加到生成的镜像中. 可以使用数组或字典。
build:
  context: .
  labels:
    com.example.description: "Accounting webapp"



SHM_SIZE

  • 为此构建的容器设置/dev/shm分区的大小
build:
  context: .
  shm_size: '2gb'



TARGET

  • 在内部定义构建指定的阶段dockerfile
  • 多阶段构建文档链接: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
build:
  context: .
  target: prod



cap_add和cap_drop

  • 添加或删除容器功能
cap_add:
  - ALL

cap_drop:
  - NET_ADMIN
  - SYS_ADMIN



cgroup_parent

  • 为容器指定可选的父cgroup
cgroup_parent: m-executor-abcd



command

  • 覆盖默认命令,和dockerfile指令相似.
command: ["bundle", "exec", "thin", "-p", "3000"]



configs

  • 短语法变体仅指定配置名称。这将授予容器对配置的访问权限,并将其安装在容器中的/ <config_name>中。源名称和目标安装点都设置为配置名称. 支持3.3版本以上.
  • 以下示例使用短语法授予对my_config和my_other_config配置的redis服务访问权限。 my_config的值设置为./my_config.txt文件的内容,my_other_config被定义为外部资源,这意味着它已经在Docker中定义,通过运行docker config create命令或另一个堆栈部署。如果外部配置不存在,则堆栈部署将失败并显示config not found错误
version: "3.7"
services:
  redis:
    image: redis:latest
    deploy:
      replicas: 1
    configs:
      - my_config
      - my_other_config
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true



LONG SYNTAX

  • source: Docker中存在的配置名称
  • target: 要在服务的任务容器中装入的文件的路径和名称。默认为:/<source>
  • uid和gid: 在服务的任务容器中拥有已装入的配置文件的数字UID或GID.默认为0.
  • mode: 以八进制表示法在服务的任务容器中装入的文件的权限。默认值为0444.配置无法写入,因为它们安装在临时文件系统中,因此如果设置了可写位,则会将其忽略。可以设置可执行位。
  • 以下示例将my_config的名称设置为容器中的redis_config,将模式设置为0440(组可读)并将用户和组设置为103. redis服务无权访问my_other_config配置。
version: "3.7"
services:
  redis:
    image: redis:latest
    deploy:
      replicas: 1
    configs:
      - source: my_config
        target: /redis_config
        uid: '103'
        gid: '103'
        mode: 0440
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true



  • 可以授予对多个配置的服务访问权限.

container_name

  • 指定自定义容器的名称.
container_name: nginx-test



  • Docker容器名称必须是唯一的,如果指定了自定义名称,则无法将服务扩展到多个容器.

EXAMPLE GMSA CONFIGURATION

  • 为服务配置gMSA凭据规范时,只需指定凭据规范.
version: "3.8"
services:
  myservice:
    image: myimage:latest
    credential_spec:
      config: my_credential_spec

configs:
  my_credentials_spec:
    file: ./my-credential-spec.json|



depends_on

  1. 服务依赖关系之间的Express依赖关系会导致以下行为:
  2. docker-compose up: 以依赖顺序启动服务.
  3. docker-compose up SERVICE: 自动包含SERVICE的依赖项.
  4. docker-compose stop: 按依赖顺序停止服务.
version: "3.7"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres



deploy

  • 指定与部署和运行服务相关的配置.
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure



ENDPOINT_MODE

  • 为连接到群集的外部客户端指定服务发现方法。
  • endpoint_mode: vip: Docker为服务分配虚拟IP(VIP),作为客户端到达网络服务的前端。 Docker在客户端和服务的可用工作节点之间路由请求,而无需客户端知道有多少节点参与服务或其IP地址或端口。
  • endpoint_mode: dnsrr: DNS循环(DNSRR)服务发现不使用单个虚拟IP。 Docker为服务设置DNS条目,以便服务名称的DNS查询返回IP地址列表,客户端直接连接到其中一个。
version: "3.7"

services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    networks:
      - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: vip

  mysql:
    image: mysql
    volumes:
       - db-data:/var/lib/mysql/data
    networks:
       - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: dnsrr

volumes:
  db-data:

networks:
  overlay:



LABELS

  • 指定服务的标签。这些标签仅在服务上设置,而不在服务的任何容器上设置。
version: "3.7"
services:
  web:
    image: web
    deploy:
      labels:
        com.example.description: "This label will appear on the web service"



  • 要在容器上设置标签,可以把labels键放在和image键同级.

MODE

  • 全局或复制(副本容器)
version: "3.7"
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    deploy:
      mode: global



PLACEMENT

  • 指定约束和首选项的位置
version: "3.7"
services:
  db:
    image: postgres
    deploy:
      placement:
        constraints:
          - node.role == manager
          - engine.labels.operatingsystem == ubuntu 14.04
        preferences:
          - spread: node.labels.zone



REPLICAS

  • 如果复制了服务,请指定在任何给定时间应运行的容器数。
version: "3.7"
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 6



RESOURCES

  • 配置资源限制.
  • 在这个示例中,redis服务被限制为使用不超过50M的内存和0.50(单核的50%)的可用处理时间(CPU),并且具有20M的内存和0.25个CPU时间保留(始终可用).
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M



Out Of Memory Exceptions (OOME)

  • 如果您的服务或容器尝试使用的内存超过系统可用的内存,则可能会遇到内存不足异常(OOME),并且内核OOM杀手可能会杀死容器或Docker守护程序。要防止这种情况发生,请确保您的应用程序在具有足够内存的主机上运行.

RESTART_POLICY

  • 配置是否以及如何在容器退出时重新启动容器.
  • condition: none,on-failure,any
  • delay: 重启尝试之间等待多长时间,默认0.
  • max_attempts: 在放弃之前尝试重启容器的次数.例如,如果max_attempts设置为“2”,并且第一次尝试时重新启动失败,则可能会尝试重新启动两次以上。
  • window: 在决定重启是否成功之前等待多长时间.
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s



ROLLBACK_CONFIG

  • 配置在更新失败的情况下应如何回滚服务.
  • `parallelism: 一次回滚的容器数. 0为同时回滚.
  • delay: 每个容器组的回滚之间等待的时间.
  • failure_action: 如果回滚失败该怎么办。选择continue或者pause
  • monitor: 每次更新任务后的持续时间.ns|us|ms|s|m|h
  • max_failure_ratio: 回滚期间容忍的失败率.
  • order: 回滚期间的操作顺序。停止优先(旧任务在启动新任务之前停止)或启动优先(新任务首先启动,运行任务暂时重叠)(默认停止优先)

UPDATE_CONFIG

  • 配置服务应如何更新。用于配置滚动更新.
  • parallelism:一次更新的容器数
  • delay: 更新一组容器之间的等待时间.
  • failure_action: 如果更新失败该怎么办。选择continue,rollback,pause.默认pause.
  • monitor: 每次更新任务后的持续时间.(ns|us|ms|s|m|h)
  • max_failure_ratio: 更新期间容忍的失败率。
  • order : 更新期间的操作顺序。 stop-first之一(旧任务在启动新任务之前停止)或start-first(首先启动新任务,并且运行任务暂时重叠)(默认stop-first)
version: "3.7"
services:
  vote:
    image: dockersamples/examplevotingapp_vote:before
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
        order: stop-first



dns

  • 自定义dns服务
dns:
  - 8.8.8.8
  - 9.9.9.9



dns_search

  • 自定义DNS搜索域
dns_search:
  - dc1.example.com
  - dc2.example.com



entrypoint

  • 覆盖默认entrypoint
entrypoint: /code/entrypoint.sh



env_file

  • 从文件添加环境变量
env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env



  • Compose期望env文件中的每一行都是VAR = VAL格式。以#开头的行被视为注释并被忽略。空行也被忽略。

environment

  • 添加环境变量。您可以使用数组或字典。任何布尔值; true,false,yes no,需要用引号括起来
environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:



expose

  • 暴露端口而不将它们发布到主机 - 它们只能被链接服务访问。只能指定内部端口
expose:
 - "3000"
 - "8000"



external_links

  • 链接到此docker-compose.yml之外或甚至在Compose之外的容器。指定容器名称和链接别名.
external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql



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



healthcheck

  • 配置运行的检查以确定此服务的容器是否“健康”.
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s
test: ["CMD", "curl", "-f", "http://localhost"]
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
test: curl -f https://localhost || exit 1



禁用运行状况检查



healthcheck:
  disable: true



init

  • 在容器内运行init,转发信号并重新获得进程.
version: "3.7"
services:
  web:
    image: alpine:latest
    init: true



links

  • 链接到另一个服务中的容器。指定服务名称和链接别名.
web:
  links:
   - db
   - db:database
   - redis



  • 链接服务的容器可以在与别名相同的主机名上访问,如果未指定别名,则可以访问服务名称。
  • 链接还以与depends_on相同的方式表达服务之间的依赖关系.