Meaning of ampersand (&) in docker-compose.yml file
DockerDocker ComposeDocker Problem Overview
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.
Docker Solutions
Solution 1 - Docker
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
Solution 2 - Docker
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