Docker compose yml文档结构

1,233 阅读2分钟

介绍

  • Compose是用于定义和运行多容器Docker应用程序的工具。
  • 通过Compose,您可以使用YAML文件来配置应用程序的服务。
  • 使用一个命令,就可以从配置中创建并启动所有服务
  • Compose可在所有环境中工作:生产,登台,开发,测试以及CI工作流

compose 使用步骤:

  • 使用定义您的应用环境,Dockerfile以便可以在任何地方复制它。

  • 定义组成应用程序的服务,docker-compose.yml 以便它们可以在隔离的环境中一起运行。

  • Run docker-compose upand Compose启动并运行您的整个应用程序。

安装

mac os 默认安装

测试

> docker-compose --version
docker-compose version 1.24.1, build 4667896b

compose yaml结构解说

build

version: "3.7"
services:
 webapp:
   build:
     context: ./dir
     dockerfile: Dockerfile-alternate
     args:
       buildno: 1
     cache_from:
       - alpine:latest
       - corp/web_app:3.14
     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"
     shm_size: '2gb'
     shm_size: '2gb'

cap_add

 cap_add:
  - ALL

cap_drop

cap_drop:
  - NET_ADMIN
  - SYS_ADMIN

cgroup_parent

cgroup_parent: m-executor-abcd

command

command: bundle exec thin -p 3000  # or
command: ["bundle", "exec", "thin", "-p", "3000"]

configs

# short 
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 
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: my-web-container

credential_spec

credential_spec:
  file: my-credential-spec.json
  # or

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

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
      endpoint_mode: vip
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M
      placement:
        constraints:
          - node.role == manager
          - engine.labels.operatingsystem == ubuntu 14.04
        preferences:
          - spread: node.labels.zone
      labels:
        com.example.description: "This label will appear on the web service"
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

devices

devices:
  - "/dev/ttyUSB0:/dev/ttyUSB0"

dns

dns:
  - 8.8.8.8
  - 9.9.9.9

dns_search

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

entrypoint

entrypoint:
    - php
    - -d
    - zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
    - -d
    - memory_limit=-1
    - vendor/bin/phpunit

env_file

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

environment

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

expose

expose:
 - "3000"
 - "8000"

external_links

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"

healthcheck

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

image

image: redis
image: ubuntu:14.04
image: tutum/influxdb
image: example-registry.com:4000/postgresql
image: a4bc65fd

init

version: "3.7"
services:
  web:
    image: alpine:latest
    init: true
    logging:
      driver: syslog
      options:
        syslog-address: "tcp://192.168.0.42:123"
    links:
       - db
       - db:database
       - redis
    labels:
      - "com.example.description=Accounting webapp"
      - "com.example.department=Finance"
      - "com.example.label-with-empty-value"

network_mode

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

docker compose yaml最终结构

version: "3.7"
services:
 webapp:
   image: webapp:tag
   cap_add:
     - ALL
   cap_drop:
     - NET_ADMIN
     - SYS_ADMIN
   cgroup_parent: m-executor-abcd
   command: bundle exec thin -p 3000
   container_name: my-web-container
   credential_spec:
     config: my_credential_spec
   build:
     context: ./dir  # 相对于yml文件 
     dockerfile: Dockerfile-alternate
     depends_on:
       - db
       - redis
     args:
       buildno: 1  # - buildno=1 也可以 ,需要dockerfile 定义ARG buildno 
     cache_from:
       - alpine:latest
       - corp/web_app:3.14
     labels:
       com.example.description: "Accounting webapp"
       com.example.department: "Finance"
       com.example.label-with-empty-value: ""
     shm_size: '2gb'   # shm_size: 10000000 也可以
   deploy:
     replicas: 1
   devices:
     - "/dev/ttyUSB0:/dev/ttyUSB0"
   dns: 8.8.8.8
   dns_search: example.com
   entrypoint: /code/entrypoint.sh
   env_file:
     - ./common.env
     - ./apps/web.env
     - /opt/secrets.env 
   environment:
     RACK_ENV: development
     SHOW: 'true'
     SESSION_SECRET: 
   expose:
    - "3000"
    - "8000"
   external_links:
    - redis_1
    - project_db_1:mysql
    - project_db_1:postgresql
   extra_hosts:
    - "somehost:162.242.195.82"
    - "otherhost:50.31.209.229"
   links:
    - db
    - db:database
    - redis
   logging:
     driver: syslog
     options:
       syslog-address: "tcp://192.168.0.42:123"
   ports:
      - "3000"
      - "3000-3005"
      - "8000:8000"
      - "9090-9091:8080-8081"
      - "49100:22"
      - "127.0.0.1:8001:8001"
      - "127.0.0.1:5000-5010:5000-5010"
      - "6060:6060/udp"
   restart: on-failure
   stop_signal: SIGUSR1
   ulimits:
     nproc: 65535
     nofile:
       soft: 20000
       hard: 40000
   configs:
     - source: my_config
       target: /redis_config
       uid: '103'
       gid: '103'
       mode: 0440
 redis:
   image: redis
 db:
   image: postgres
configs:
 my_config:
   file: ./my_config.txt
 my_other_config:
   external: true
 my_credentials_spec:
   file: ./my-credential-spec.json|

参考文献

本文作者:前端首席体验师(CheongHu)

联系邮箱:simple2012hcz@126.com