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

后端 未结 2 1282
鱼传尺愫
鱼传尺愫 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: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
    

提交回复
热议问题