简体   繁体   English

docker-compose.yml文件中&符号的含义

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

I recently came across this and was wondering what &django means 我最近遇到过这个,并想知道&django意味着什么

version: '2'

services:
  django: &django

I can't see anything in the docs related to this. 我无法在与此相关的文档中看到任何内容。

These are a YAML feature called anchors, and are not particular to Docker Compose. 这些是称为锚点的YAML功能,并不特定于Docker Compose。 I would suggest you have a look at below URL for more details 我建议您查看以下URL以获取更多详细信息

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

Follow the section EXTRA YAML FEATURES 请遵循EXTRA YAML FEATURES部分

YAML also has a handy feature called 'anchors', which let you easily duplicate content across your document. YAML还有一个名为“锚点”的便捷功能,可让您轻松复制文档中的内容。 Both of these keys will have the same value: 这两个键都具有相同的值:

anchored_content: &anchor_name This string will appear as the value of two keys. anchored_content&anchor_name此字符串将显示为两个键的值。 other_anchor: *anchor_name 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

To complement Tarun's answer, & identifies an anchor and * is an alias referring back to the anchor. 为了补充Tarun的答案, &识别一个锚点, *是一个别名,用于回顾锚点。 It is described as the following in the YAML specification : 它在YAML规范中描述如下:

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 . 对于那些想要在docker-compose文件中开始使用锚点的人来说,有更强大的方法可以通过使用docker-compose YAML扩展字段来制作可重用的锚点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM