Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the double arrow (<<: *django) means in dockerfile?

Tags:

docker

yaml

I see something like

   celerybeat:
     <<: *django

in https://github.com/pydanny/cookiecutter-django example docker files.

What does it mean? I can't google <<: *

like image 873
eugene Avatar asked Apr 28 '18 04:04

eugene


People also ask

What is Docker Django?

Docker-Compose Django. Docker Compose. Conclusion. Docker is an open-source platform, and it makes it easy and possible to develop, run, and ship allocated applications with the help of a well-defined set of rules in controlled and distributed environments as a containerization platform.

What is the line continuation character in Dockerfiles?

In the Dockerfile spec the backslash is used as an escape character, and also for line continuation.


1 Answers

<< and * are both YAML keys (you can also think of them as operators). And one more key related to your question is &.

In YAML, you can define anchors and later use them. For example,

foo: &myanchor
  key1: "val1"
  key2: "val2"

bar: *myanchor

In this code snippet, & defines an anchor names it myanchor, and *myanchor references that anchor. Now both foo and bar have the same keys and values.

<< is called the YAML merge key. You can compare it to class inheritance in OOP (not so accurate, but can help you understand). See below example.

foo: &myanchor
  key1: "val1"
  key2: "val2"

bar:
  << : *myanchor
  key2: "val2-new"
  key3: "val3"

In this code snippet, we merge keys and values from foo to bar but override the key2 to a new value. And we add a new key-value pair to bar.

Now bar has the following value: {"bar": {"key1": "val1", "key2": val2-new", "key3": "val3"}}.

Hope that helps.

like image 59
Yuankun Avatar answered Oct 03 '22 09:10

Yuankun