第09篇 Compose-02 模板文件

一、简单介绍

模板文件是使用 Compose 的核心 ,涉及的指令关键字也比较多。但是大部分指令与 docker [container] createlrun相关参数的含义都是类似的。
默认的模板文件名称为 docker compose.yml,格式为 Y灿1L格式,目前最新的版 本为 v3

参考文档:https://docs.docker.com/compose/compose-file/

二、指令详解

  1. build
    指定创建镜像的上下文、 缓存来源等,如:
    version: "3.7"
    services:
      webapp:
        build:
          context: ./dir 
          dockerfile: dockerfile 
          args:
            key1: value1
            key2: value2
    
  • context:存放Dockerfile的路径,可以是一个路径(可以是绝对路径,或者相对 docker-compose.yml文 件的路径)或者是git仓库地址

  • dockerfile:自己指定dockerfile名字,一般用于非默认文件名,需要指定context

  • args:为服务指定构建参数,如:
    Dockerfile定义如下:

    ARG buildno
    ARG gitcommithash
    RUN echo "Build number: $buildno"
    RUN echo "Based on commit: $gitcommithash"
    

    那么我们可以如下传参数:

    build:
      context: .
      args:
        buildno: 1
        gitcommithash: cdc3b19
    

    或者:

    build:
      context: .
      args:
        - buildno=1
        - gitcommithash=cdc3b19
    

    注意事项:

    1. 如果Dockerfile中的ARG在FROM之前,那么在FROM之后的指令中不能使用ARG参数;如果需要整个文件中都能使用,需要把ARG指令放在FROM之后;
    2. 对于true, false, yes, no, on, off等参数值,需要使用双引号引起来,这样,yml文件才能把它解析成字符串;
  • lables:给结果镜像定义的标签,如:

    build:
      context: .
      labels:
        com.example.description: "Accounting webapp"
        com.example.department: "Finance"
        com.example.label-with-empty-value: ""
    
  • shm_size:设置构建容器的内存大小,值为字节数,或者是'2gb'这种

    如果只需要指定Dockerfile所在文件夹的路径,可以如下指定:

    version: "3.7"
    services:
      webapp:
        build: ./dir
    
  • target:指定生成的阶段名,用于多阶段构建

  1. ports
    暴露主机端口,使用宿主:容器(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"
    
  2. volumes
    数据卷所挂载路径设置。 可以设置宿主机路径 (HOST:CONTAINER) 或加上访间模式 (HOST:CONTAINER:ro) 。

    version: "3.7"
    services:
      web:
        image: nginx:alpine
        volumes:
          - type: volume
            source: mydata
            target: /data
            volume:
              nocopy: true
          - type: bind
            source: ./static
            target: /opt/app/static
    
      db:
        image: postgres:latest
        volumes:
          - "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
          - "dbdata:/var/lib/postgresql/data"
    
    volumes:
      mydata:
      dbdata:
    
  3. command
    覆盖容器启动后默认执行的命令,可以为字符串格式或 JSON 数组格式 。
    command: echo helloworld 或者:command: ["bash","-c","echo","helloworld"]

  4. configs
    给服务设置配置信息。支持从文件读取或外部读取,语法格式为:

    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
    

    这里指定redis服务使用两个配置my_config和 my_other_config,其中my_config通过配置文件./my_config.txt进行设置的,而my_other_config是通过外部资源来设置的,外部配置一般是通过 docker config create命令来创建的

  5. container_name
    指定容器名称。
    如:container_name: my-web-container
    默认将会使用“项目 名称 服务名称一序号”这样的格式

  6. depends_on
    指定多个服务之间的依赖关系 。 启动时,会先启动被依赖服务 。 例如,可以指定依赖于db和redis服务

    depends_on:
      - db
      - redis
    
  7. devices
    指定设备映射关系,如:

    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
    
  8. dns
    自定义 DNS 服务器。 可以是一个值,也可以是一个列表
    dns: 8.8.8.8

    dns:
      - 8.8.8.8
      - 9.9.9.9
    
  9. dns_search
    自定义DNS搜索域,可以是单个或者是一个list

  10. entrypoint
    覆盖容器中默认的人口命令,注意,也会取消掉 镜像中指定的人口命令和默认启动命。
    entrypoint: /code/entrypoint.sh

  11. env_file
    从文件中获取环境变量,可以为单独的文件路径或列表。 如果通过docker-compose -f FILE方式来指定 Compose模板文件, 则 env_file 中变量的路径会基于模板文件路径 。 如果有变量名称与 environment 指令冲突,则按照惯例,以后者为准。
    env_file: .env 或者

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

    环境变量文件内的格式为:key=value

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

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

    或者

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

    注意:对于一些布尔类型的值 (“true”/“false”, “yes”/“no” or “on”/“off”)等,最好用引号包起来;

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

    expose:
     - "3000"
     - "8000"
    
  14. external_links
    链接到 docker-compose.yml外部的容器,甚至并非 Compose管理的外部容器。 参数格式 跟 links类似

    external_links:
     - redis_1
     - project_db_1:mysql
     - project_db_1:postgresql
    
  15. healthcheck
    指定检测应用健康状态的机制, 包括检测方法(test)、 间隔( interval)、 超时(timeout)、 重试次数(retries)、 启动等待时间(start_ period)等。
    例如, 指定检测方法为访问 8080 端口, 间隔为1分 30 秒, 超时为 10 秒, 重试 3 次, 启动后等待 40 秒再做检查

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

    也可以简单的使用:test: curl -f https://localhost || exit 1
    关闭healthcheck方式如下:

    healthcheck:
      disable: true
    

    或者:

    healthcheck:
    	test: ["NONE"]
    
  16. image
    指定为用来启动容器的镜像名称或镜像ID。 如果镜像在本地不存在,Compose将会尝试拉去这个镜像.

    image: redis
    image: ubuntu:14.04
    image: tutum/influxdb
    image: example-registry.com:4000/postgresql
    image: a4bc65fd
    
  17. isolation
    容器的隔离机制,可以取值为:default, process and hyperv

  18. labels
    给容器添加元数据信息

  19. links
    链接到其他服务中的容器。 使用服务名称(同时作为别名)或 服务名称: 服务别名 (SERVICE:ALIAS) 格式都可以

    web:
      links:
       - db
       - db:database
       - redis
    

    不过这个是旧语法;

  20. logging
    给服务配置日志选项

    version: "3.7"
    services:
      some-service:
        image: some-service
        logging:
          driver: "json-file"
          options:
            max-size: "200k"
            max-file: "10"
    

    driver指定使用的存储方式,可以是"json-file"、“syslog”、“none”,默认是json-file;
    max-size指定文件大小
    max-file指定文件个数

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

  22. restart
    设置重启策略,可以是no(不重启)、always(总是重启)、on-failure(失败重启)、unless-stopped(除非关闭了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值