Search code examples
dockeryaml

what's the double arrow (<<: *django) mean in a docker-compose file?


I see something like

   celerybeat:
     <<: *django

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

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


Solution

  • << 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 and names it myanchor.

    *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"
       }
    }