Meaning of ampersand (&) in docker-compose.yml file

后端 未结 2 1237
鱼传尺愫
鱼传尺愫 2021-01-31 15:41

I recently came across this and was wondering what &django means

version: \'2\'

services:
  django: &django

I can\'t see

相关标签:
2条回答
  • 2021-01-31 16:17

    These are a YAML feature called anchors, and are not particular to Docker Compose. I would suggest you have a look at below URL for more details

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

    Follow the section EXTRA YAML FEATURES

    YAML also has a handy feature called 'anchors', which let you easily duplicate content across your document. Both of these keys will have the same value:

    anchored_content: &anchor_name This string will appear as the value of two keys. other_anchor: *anchor_name

    Anchors can be used to duplicate/inherit properties

    base: &base
        name: Everyone has same name
    
    foo: &foo
        <<: *base
        age: 10
    
    bar: &bar
        <<: *base
        age: 20
    
    0 讨论(0)
  • 2021-01-31 16:26

    To complement Tarun's answer, & identifies an anchor and * is an alias referring back to the anchor. It is described as the following in the YAML specification:

    In the representation graph, a node may appear in more than one collection. When serializing such data, the first occurrence of the node is identified by an anchor. Each subsequent occurrence is serialized as an alias node which refers back to this anchor.

    Sidenote:

    For those who want to start using anchors in your docker-compose files, there is more powerful way to make re-usable anchors by using 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
    
    0 讨论(0)
提交回复
热议问题