9.2.1 services

build

指定Dockerfile所在文件夹的路径(可以是绝对路径,或者相对compose.yml文件的路径)。Compose将会利用它自动构建应用镜像,然后使用这个镜像,例如:

services:
    app:
        build: /path/to/build/dir

build指令还可以指定创建镜像的上下文、Dockerfile路径、标签、Shm大小、参数和缓存来源等:

services:
    app:
        build:
            context: /path/to/build/dir
            dockerfile: Dockerfile-app
            labels:
                version: "2.0"
                released: "true"
            shm_size: '2gb'
            args:
                key: value
                name: myApp
            cache_from:
                - myApp:1.0

cap_add, cap_drop

指定容器的内核能力(capacity)分配。例如,让容器拥有所有能力可以指定为:

cap_add:
    - ALL

去掉NET_ADMIN能力可以指定为:

cap_drop:
    - NET_ADMIN

command

覆盖容器启动后默认执行的命令,可以为字符串格式或JSON数组格式。例如:

command: echo "hello world"

或者:

command: ["bash", "-c", "echo", "hello world"]

configs

Configs允许服务调整它们的行为,不需要重新构建新的docker镜像。

Configs allow services to adapt their behaviour without the need to rebuild a Docker image.

Compose reports an error if config doesn't exist on the platform or isn't defined in the configs top-level element in the Compose file.

Services can only access configs when explicitly granted by the configs attribute. Two different syntax variants are supported. You can grant a service access to multiple configs, and you can mix long and short syntax.

Short syntax

The short syntax variant only specifies the config name. This grants the container access to the config and mounts it as files into a service’s container’s filesystem. The location of the mount point within the container defaults to /<config_name> in Linux containers, and C:\<config-name> in Windows containers.

The following example uses the short syntax to grant the redis service access to the my_config and my_other_config configs. The value of my_config is set to the contents of the file ./my_config.txt, and my_other_config is defined as an external resource, which means that it has already been defined in the platform. If the external config does not exist, the deployment fails.

services:
  redis:
    image: redis:latest
    configs:
      - my_config
      - my_other_config
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true

Long syntax

The long syntax provides more granularity in how the config is created within the service's task containers.

  • source: The name of the config as it exists in the platform.

  • target: The path and name of the file to be mounted in the service's task containers. Defaults to /<source> if not specified.

  • uid and gid: The numeric UID or GID that owns the mounted config file within the service's task containers. Default value when not specified is USER running container.

  • mode: The permissions for the file that is mounted within the service's task containers, in octal notation. Default value is world-readable (0444). Writable bit must be ignored. The executable bit can be set.

The following example sets the name of my_config to redis_config within the container, sets the mode to 0440 (group-readable) and sets the user and group to 103. The redis service does not have access to the my_other_config config.

services:
  redis:
    image: redis:latest
    configs:
      - source: my_config
        target: /redis_config
        uid: "103"
        gid: "103"
        mode: 0440
configs:
  my_config:
    external: true
  my_other_config:
    external: true

container_name

指定容器名称。如果不指定,将会按照“项目名称_服务名称_序号”这样的格式自动生成容器名称。

需要注意,指定容器名称后,该服务将无法进行扩展,因为Docker不允许多个容器实例重名。

depends_on

指定多个服务之间的依赖关系。启动时,会先启动被依赖服务。

dns

定义DNS服务器。可以是一个值,也可以是一个列表。

dns: 8.8.8.8
dns:
  - 8.8.8.8
  - 9.9.9.9

entrypoint

覆盖容器中默认的入口命令

entrypoint: python app.py

env_file

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

env_file can also be a list. The files in the list are processed from the top down. For the same variable specified in two env files, the value from the last file in the list stands.

env_file:
  - ./a.env
  - ./b.env

Relative path are resolved from the Compose file's parent folder. As absolute paths prevent the Compose file from being portable, Compose warns you when such a path is used to set env_file.

Environment variables declared in the environment section override these values. This holds true even if those values are empty or undefined.

environment

设置环境变量,可以使用数组字典两种格式。只给定名称的变量会自动获取运行Compose主机上对应变量的值,可以用来防止泄露不必要的数据。

environment:
  RACK_ENV: development
  SHOW: "true"
  USER_INPUT:

或者:

environment:
  - RACK_ENV=development
  - SHOW=true
  - USER_INPUT

注意,如果变量名称或者值中用到true|false, yes|no等表达布尔含义的词汇,最好放到引号里,避免YAML自动解析某些内容为对应的布尔语义

expose

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

expose:
  - "3000"
  - "8000"

extends

基于其他模板文件进行扩展。例如,我们已经有了一个webapp服务,定义一个基础模板文件为common.yml:

common.yml
webapp:
    build: ./webapp
    environment:
        - DEBUG=false
        - SEND_EMAILS=false

再编写一个新的development.yml文件,使用common.yml中的webapp服务进行扩展:

development.yml
web:
    extends:
        file: common.yml
        service: webapp
    ports:
        - "8000:8000"
    links:
        - db
    environment:
        - DEBUG=true
db:
    image: postgres

后者会自动继承common.yml中的webapp服务及环境变量定义。

使用extends需要注意以下两点:

  • 要避免出现循环依赖。

  • extends不会继承links和volumes_from中定义的容器和数据卷资源。

一般情况下,推荐在基础模板中只定义一些可以共享的镜像和环境变量,在扩展模板中具体指定应用变量、链接、数据卷等信息。

链接到一个已存在的外部容器,该容器并非由当前Compose应用所管理。

external_links:
  - redis
  - database:mysql
  - database:postgresql

extra_hosts

类似Docker中的--add-host参数,指定额外的host名称映射信息

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

上述配置会在启动后的服务容器的/etc/hosts文件中增添以下两行数据:

162.242.195.82  somehost
50.31.209.229   otherhost

healthcheck

指定检测应用健康状态的机制,包括检测方法(test)、间隔(interval)、超时(timeout)、重试次数(retries)、启动等待时间(start_period)等。

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s
  start_interval: 5s

image

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

image: redis
image: redis:5
image: redis@sha256:0ed5d5928d4737458944eb604cc8509e245c3e19d02ad83935398bc4b991aac7
image: library/redis
image: docker.io/library/redis
image: my_private.registry:5000/redis

labels

为容器添加Docker元数据(metadata)信息。建议使用反向DNS作为键,以防止与其他软件冲突。

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

logging

跟日志相关的配置,包括一系列子配置。

  • logging.driver:类似于Docker中的--log-driver参数,指定日志驱动类型。目前支持三种日志驱动类型:

    • driver: "json-file"

    • driver: "syslog"

    • driver: "none"

  • logging.options:日志驱动的相关参数。

    logging:
        driver: "syslog"
        options:
            syslog-address: "tcp://192.168.0.42:123"

    logging:
        driver: "json-file"
        options:
            max-size: "1000k"
            max-file: "20"

network_mode

设置网络模式。使用和docker client的--net参数一样的值。

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

其中:service:{name} 表示容器只能访问指定的服务。

networks

所加入的网络。需要在顶级的networks字段中定义具体的网络信息。

services:
    web:
        networks:
            web_net:
                aliases: web_app
                ipv4_address: 172.16.0.10
networks:
    web_net:
        driver: bridge
        enable_ipv6: true
        ipam:
            driver: default
            config:
                subnet: 172.16.0.0/24

对于一个service,不能同时配置networks和network_mode,两者只能存在一个!

pid

跟主机系统共享进程命名空间。打开该选项的容器之间,以及容器和宿主机系统之间可以通过进程ID来相互访问和操作。

pid: "host"

ports

暴露端口信息。使用“宿主:容器(HOST:CONTAINER)”格式,或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。

ports:
  - "3000"
  - "3000-3005"
  - "8000:8000"
  - "9090-9091:8080-8081"
  - "49100:22"
  - "8000-9000:80"
  - "127.0.0.1:8001:8001"
  - "127.0.0.1:5000-5010:5000-5010"
  - "6060:6060/udp"

ulimits

指定容器的ulimits限制值。

例如,指定最大进程数为65535,指定文件句柄数为20000(软限制,应用可以随时修改,不能超过硬限制)和40000(系统硬限制,只能root用户提高)。

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

volumes

数据卷所挂载路径设置。可以设置宿主机路径(HOST:CONTAINER)或加上访问模式(HOST:CONTAINER:ro)。支持driver、driver_opts、external、labels、name等子配置。

services:
  backend:
    image: example/backend
    volumes:
      - type: volume
        source: db-data
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: /var/run/postgres/postgres.sock
        target: /var/run/postgres/postgres.sock

volumes:
  db-data:

Last updated