Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

version: '2'

services:
  django: &django

I can't see anything in the docs related to this.

like image 264
lukeaus Avatar asked Aug 21 '17 20:08

lukeaus


People also ask

What does the ampersand symbolize?

An ampersand is a sign for the word and. It's written or typed as the symbol &. It's a modification of the term “and per se and,” which has Latin origins. The ampersand can indicate that the listed items are grouped together as part of a name.

What did ampersand originally mean?

Etymology. The term ampersand is a corruption of and (&) per se and, which literally means "(the character) & by itself (is the word) and." The symbol & is derived from the ligature of ET or et, which is the Latin word for "and."

When should ampersand be used?

An ampersand (&) is a typographical symbol that is rarely used in formal writing. It is read aloud as the word and and is used as a substitute for that word in informal writing and in the names of products or businesses.

What is this symbol called &?

What is an &? & is called an ampersand symbol (pronounced “AM- per-sand”). Essentially, it means “and”. It is used both (a) in the body of the paper as part of a citation and (b) at the end of the paper as part of a reference.


2 Answers

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
like image 86
Tarun Lalwani Avatar answered Oct 09 '22 03:10

Tarun Lalwani


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
like image 31
Mike Avatar answered Oct 09 '22 03:10

Mike