【问题标题】:Meaning of ampersand (&) in docker-compose.yml filedocker-compose.yml 文件中与符号 (&) 的含义
【发布时间】:2017-08-21 20:56:41
【问题描述】:

我最近遇到了这个问题,想知道&django 是什么意思

version: '2'

services:
  django: &django

我在docs 中看不到任何与此相关的内容。

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    这些是称为锚点的 YAML 功能,并非 Docker Compose 特有的。我建议您查看以下 URL 以了解更多详细信息

    https://learnxinyminutes.com/docs/yaml/

    关注EXTRA YAML FEATURES部分

    YAML 还有一个名为“锚点”的便捷功能,可让您轻松复制 文档中的内容。这两个键将具有相同的值:

    anchored_content: &anchor_name 此字符串将显示为两个键的值。 other_anchor: *anchor_name

    锚点可用于复制/继承属性

    base: &base
        name: Everyone has same name
    
    foo: &foo
        <<: *base
        age: 10
    
    bar: &bar
        <<: *base
        age: 20
    

    【讨论】:

      【解决方案2】:

      为了补充 Tarun 的答案,&amp; 标识了一个锚点,* 是指回锚点的别名。在YAML specification中是这样描述的:

      在表示图中,一个节点可能出现在多个 收藏。序列化此类数据时,第一次出现 节点由锚点标识。随后的每一次出现都是 序列化为一个别名节点,它引用这个锚点。

      旁注:

      对于那些想开始在 docker-compose 文件中使用锚点的人,有更强大的方法可以通过使用 docker-compose YAML extension fields 来制作可重复使用的锚点。

      version: "3.4"
      
      # x-docker-data is an extension and when docker-compose
      # parses the YAML, it will not do anything with it
      
      x-docker-data: &docker-file-info
        build:
          context: .
          dockerfile: Dockerfile
      
      services:
        some_service_a:
          <<: *docker-file-info
          restart: on-failure
          ports:
            - 8080:9090
        some_service_b:
          <<: *docker-file-info
          restart: on-failure
          ports:
            - 8080:9595
      

      【讨论】:

      • 值得强调的是,它不必被称为x-docker-data。 “任何以 x- 开头的顶级键都将被 compose 忽略。”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2021-10-30
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多