• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    浅谈docker compose书写规则

    本文对集群部署相关的一概不做介绍

    版本约束

    结构介绍

    docker-compose.yaml 文件结构主要由

    version # docker compose版本
    networks # 网络,用于docker容器内部通讯
    x-{name} # 模版命名规则 以x-开头 用于复用
    volumes # 挂载卷
    services # 服务模块,内部定义容器信息 其内部参数相当于docker run时的参数

    模块介绍

    Docker Compose官方文档

    version

    设定docker-compose.yaml的版本
    需要升级的话,参看文档版本升级参考文档

    Compose file 版本 Docker Engine 版本
    3.8 19.03.0+
    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.+

    network_mode

    使用与--network参数相同的值,以及特殊形式service:[service name]

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

    networks

    为当前docker-compose.yaml文件创建的容器设定网络

    不一定存在于和version同级,也可以在各个其他模块中,例如services中

    内部网络

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

    公用网络

    version: "3"
    networks:
     default-network:

    aliases(待补充)

    网络的别名

    version: "3.8"
    
    services:
     web:
      image: "nginx:alpine"
      networks:
       - new
    
     worker:
      image: "my-worker-image:latest"
      networks:
       - legacy
    
     db:
      image: mysql
      networks:
       new:
        aliases:
         - database
       legacy:
        aliases:
         - mysql
    
    networks:
     new:
     legacy:

    ipv4_address , ipv6_address(待补充)

    version: "3.8"
    
    services:
     app:
      image: nginx:alpine
      networks:
       app_net:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10
    
    networks:
     app_net:
      ipam:
       driver: default
       config:
        - subnet: "172.16.238.0/24"
        - subnet: "2001:3984:3989::/64"

    services

    最主要的部分,用来配置各个服务

    build

    用于构建镜像,当build和image字段都存在时,使用image指定的镜像名和tag作为build镜像的name和tag

    version: "3.8" # docker compose版本
    services:
     webapp: # docker-compose定义的服务(容器)名,主要是针对docker-compose命令的参数,与docker ps看到的容器名不一定一致
      build: # 使用Dockerfile构建镜像
       context: ./dir 上下文路径,相对路径则是相对于compose文件路径
       dockerfile: Dockerfile-alternate # 指定Dockerfile文件名
       args: # 指定Dockerfile的参数 环境变量
        buildno: 1 # directory写法和list写法均可

    context

    可以使用相对路径或者git仓库的url

    build:
     context: ./dir

    Dockerfile

    指定Dockerfile文件名,必须指定context

    build:
     context: .
     dockerfile: Dockerfile-alternate

    args

    Dockerfile中的ARG字段,用于指定docker build时的环境变量

    ARG buildno
    ARG gitcommithash
    
    RUN echo "Build number: $buildno" # bash-like风格的写法
    RUN echo "Based on commit: $gitcommithash"

    可以使用list或者map来设定args

    build:
     context: .
     args: # map
      buildno: 1
      gitcommithash: cdc3b19
    build:
     context: .
     args: # list
      - buildno=1
      - gitcommithash=cdc3b19

    tips
    如果需要使用boolean值,需要使用双引号("true", "false", "yes", "no", "on", "off"),以便解析器将他们解析为字符串。

    cache_from

    为build过程指定cache

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

    labels

    同Dockerfile中的LABEL指令,为镜像设定metadata

    build:
     context: .
     labels: # map
      com.example.description: "Accounting webapp"
      com.example.department: "Finance"
      com.example.label-with-empty-value: ""
    build:
     context: .
     labels: # list
      - "com.example.description=Accounting webapp"
      - "com.example.department=Finance"
      - "com.example.label-with-empty-value"

    network

    docker --network指令,为容器指定网络,个人理解为设定局域网
    桥接可以将两个物理局域网连接起来
    三种模式

    build:
     context: .
     network: host # host模式,网络延迟最低,性能与宿主机一致
    build:
     context: .
     network: custom_network_1 # 自创network 
    build:
     context: .
     network: none # 无网络

    shm_size

    设置容器内/dev/shm目录的大小

    /dev/shm目录非常重要,此目录并不在硬盘上,而是在内存中,默认大小为内存的一半大小,存入其中的文件不会被清空,容器内划分此目录可以一定程度上指定容器的性能。

    build:
     context: .
     shm_size: '2gb' # 使用字符串设置大小
    build:
     context: .
     shm_size: 10000000 # 设置字节大小

    command

    相当于Dockerfile中的CMD命令

    command: bundle exec thin -p 3000 # shell-like
    command: ["bundle", "exec", "thin", "-p", "3000"] # json-like

    container_name

    相当于docker run --name

    container_name: my-web-container

    depends_on

    用于表述服务之间的依赖关系

    docker-compose up时,会根据depends_on来决定启动顺序

    version: "3.8"
    services:
     web:
      build: .
      depends_on: # 先启动 db 和 redis
       - db
       - redis
     redis:
      image: redis
     db:
      image: postgres

    tips:
    docker-compose 不会等待depends_on中的容器的状态时‘ready'时才启动,所以需要在启动完成后检查容器状态。官方给出了解决办法,使用shell脚本来曲线救国,不做赘述。

    depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
           ----form https://docs.docker.com/compo...

    devices

    挂载的外接设备,与--devices功能相同

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

    dns

    自定义dns地址

    dns: 8.8.8.8 # 单个string值
    dns: # list 
     - 8.8.8.8
     - 9.9.9.9

    dns_search

    自定义dns搜索域名

    dns_search: example.com # 单个string值
    dns_search:
     - dc1.example.com
     - dc2.example.com

    entrypoint

    覆盖重写默认的 entrypoint

    entrypoint: /code/entrypoint.sh

    同Dockerfile中的写法

    entrypoint: ["php", "-d", "memory_limit=-1", "vendor/bin/phpunit"]

    tips:
    docker-compose.yaml 中的 entrypoint 清空Dockerfile中的CMD命令,并覆盖所有的Dockerfile中的ENTRYPOINT指令。

    env_file

    docker-compose.yaml添加环境变量文件。如果在docker-compose -f FILE中设置了 compose 文件,则env_file中的文件路径是相对于FILE的相对路径

    env_file: .env # single value
    env_file: # list
     - ./common.env
     - ./apps/web.env
     - /opt/runtime_opts.env

    tips:
    .env 文件格式:

    # Set Rails/Rack environment # '#'为注释,
    # 空行会被忽略
    RACK_ENV=development # 格式为 VAR=VAL

    .env 文件中的环境变量无法在build过程中被显示读取,只会被docker-compose.yaml文件读取,如果需要在build时使用该环境变量,请在build后加args子参数。

    对于指定多个.env文件的情况,官网的这句话贼复杂

    Keep in mind that the order of files in the list is significant in determining the value assigned to a variable that shows up more than once.
    ---from https://docs.docker.com/compo...

    直译过来是

    请记住,列表中文件的顺序对于确定分配给多次显示的变量的值很重要。

    因为对环境参数文件的处理是自上而下的,所以翻译成人话就是,多个参数文件中包含同一个环境变量,以最后一个为准。

    environment

    添加环境变量

    environment: # map
     RACK_ENV: development
     SHOW: 'true'
     SESSION_SECRET:
    environment: # list
     - RACK_ENV=development
     - SHOW=true
     - SESSION_SECRET

    tips:
    .env 文件中的环境变量无法在build过程中被显示读取,只会被docker-compose.yaml文件读取,如果需要在build时使用该环境变量,请在build后加args子参数。
    次环境变量在构建镜像时,可以被我们自身的代码读取到,例如:

    func getEnvInfo() string {
      rackEnv := os.Getenv("RACK_ENV")
      fmt.Println(rackEnv)
    }
    
    output:
    development

    expose

    暴露端口,但是仅限于服务之间的通信,暴露的是内部端口,类似Dockerfile的EXPOSE指令

    expose:
     - "3000"
     - "8000"

    external_links

    连接服务

    external_links:
     - redis_1
     - project_db_1:mysql
     - project_db_1:postgresql

    tips:
    官方推荐使用network

    extra_hosts

    添加自定义域名,同--add-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

    同Dockerfile中的HEALTHCHECK指令

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

    使用disabel: true,相当于test: ["NONE"]

    healthcheck:
     disable: true

    image

    指定需要拉取或使用的镜像,也可以使用仓库/标签或部分镜像ID

    image: redis # 默认标签 latest
    image: ubuntu:18.04
    image: tutum/influxdb
    image: example-registry.com:4000/postgresql
    image: a4bc65fd

    init

    在容器内运行一个初始化程序,以转发信号开获取进程。

    version: "3.8"
    services:
     web:
      image: alpine:latest
      init: true

    tips:
    使用的默认初始化二进制文件是Tini,并安装在/usr/libexec/docker-init守护程序主机上。您可以通过init-path配置选项将守护程序配置为使用自定义init二进制文件 。

    isolation

    指定容器隔离技术,Linux只支持default,windows支持default`process hyperv`三个值,具体参考 Docker Engine Docs

    labels

    同Dockerfile中的LABEL指令,为容器设定metadata

    build:
     context: .
     labels: # map
      com.example.description: "Accounting webapp"
      com.example.department: "Finance"
      com.example.label-with-empty-value: ""
    build:
     context: .
     labels: # list
      - "com.example.description=Accounting webapp"
      - "com.example.department=Finance"
      - "com.example.label-with-empty-value"

    links

    旧版的功能,不推荐使用

    logging

    为当前服务设置日至参数

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

    driver参数同--log-driver指令

    driver: "json-file"
    driver: "syslog"
    driver: "none"

    tips:
    只有使用json-filejournald时,docker-compose updocker-compose logs才能看到日志,其他任何的驱动都无法看到日志打印。

    指定日志设置,同docker run --log-opt.格式为k-v结构

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

    默认的日志驱动是json-file,可以设置存储限制

    options:
     max-size: "200k" # 单文件最大存储
     max-file: "10" # 文件最大数量

    tips:
    上述的option参数仅为json-file日志驱动支持的参数,不同驱动支持的参数各不相同,具体参照下表。

    支持的驱动列表

    Driver Description
    none 不输出日志。
    local 日志以自定义格式存储,旨在最大程度地减少开销。
    json-file 日志以自定义格式存储,旨在最大程度地减少开销。
    syslog 将日志消息写入syslog设施。该syslog守护程序必须在主机上运行。
    journald 将日志消息写入journald。该journald守护程序必须在主机上运行。
    gelf 将日志消息写入Graylog扩展日志格式(GELF)端点,例如Graylog或Logstash。
    fluentd 将日志消息写入fluentd(向前输入)。该fluentd守护程序必须在主机上运行。
    awslogs 将日志消息写入Amazon CloudWatch Logs。
    splunk 将日志消息写入到splunk使用HTTP Event Collector。
    etwlogs 将日志消息写为Windows事件跟踪(ETW)事件。仅在Windows平台上可用。
    gcplogs 将日志消息写入Google Cloud Platform(GCP)日志记录。
    logentries 将日志消息写入Rapid7 Logentries。

    tips:
    具体请参考 Configure logging drivers

    ports

    对外暴露端口

    short syntax:
    要么指定两个端口HOST:CONTAINER,要么仅指定容器端口(选择了临时主机端口)。

    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"
     - "12400-12500:1240"

    tips:
    当以HOST:CONTAINER格式映射端口时,使用低于60的容器端口可能会报错,因为YAML会将格式xx:yy中的数字解析为以60进制的数(可以理解为时间)。因此建议始终将端口映射显式指定为字符串。

    long syntax

    长语法允许出现短语法不允许出现的字段

    ports:
     - target: 80
      published: 8080
      protocol: tcp
      mode: host

    restart

    容器重启策略

    restart: "no" # 失败不重启
    restart: always # 失败后总是重启
    restart: on-failure # 错误码为 on-failure 时才重启
    restart: unless-stopped # 手动停止后不重启

    secrets(待补充)

    volumes

    用来挂载数据卷

    short syntax
    短语法使用最简单的格式[SOURCE:]TARGET[:MODE]

    如果使用宿主机的相对路径,则以docker-compose.yaml为基础进行拓展。

    volumes:
     # 指定容器内路径,docker 自动创建路径
     - /var/lib/mysql
    
     # 挂载绝对路径
     - /opt/data:/var/lib/mysql
    
     # 挂载相对路径
     - ./cache:/tmp/cache
    
     # 用户目录相对路径
     - ~/configs:/etc/configs/:ro
    
     # 命名挂载
     - datavolume:/var/lib/mysql

    long syntax

    长语法允许使用短语法无法表达的字段

    version: "3.8"
    services:
     web:
      image: nginx:alpine
      ports:
       - "80:80"
      volumes:
       - type: volume
        source: mydata
        target: /data
        volume:
         nocopy: true
       - type: bind
        source: ./static
        target: /opt/app/static
    
    networks:
     webnet:
    
    volumes:
     mydata:

    到此这篇关于浅谈docker compose书写规则的文章就介绍到这了,更多相关docker compose书写规则内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    上一篇:Linux面试中最常问的10个问题总结
    下一篇:Nginx反向代理springboot的jar包过程解析
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    浅谈docker compose书写规则 浅谈,docker,compose,书写,规则,