{"id":232178,"date":"2021-04-21T16:00:18","date_gmt":"2021-04-21T13:00:18","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/"},"modified":"2021-04-21T16:00:18","modified_gmt":"2021-04-21T13:00:18","slug":"how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/","title":{"rendered":"#How to Simplify Docker Compose Files With YAML Anchors and Extensions \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ba36a88f39\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ba36a88f39\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#What_Is_An_Anchor\" >What Is An Anchor?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Multi-Line_Anchors\" >Multi-Line Anchors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Extending_Anchor_Values\" >Extending Anchor Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Using_Extension_Fields\" >Using Extension Fields<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Composability\" >Composability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Other_Approaches_to_Modularity\" >Other Approaches to Modularity<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Simplify Docker Compose Files With YAML Anchors and Extensions \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9034\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1602\" height=\"902\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker Compose lets you manage multiple Docker containers and their associated resources such as volumes and networks. You write declarative YAML files which Compose uses to create your container stack.<\/p>\n<p>Your <code>docker-compose.yml<\/code> files can become repetitive when you\u2019re working with a complex stack. Services might share configuration options, causing you to duplicate sections of your file. Later updates can lead to mistakes if you forget to update every instance of a section.<\/p>\n<p>Because Compose files are plain YAML files, you can take advantage of built-in YAML features to modularise your stack definitions. Anchors, aliases and extensions let you abstract YAML sections into reusable blocks. You can add a reference to the section in each place it\u2019s needed.<\/p>\n<h2 id=\"what-is-an-anchor\"><span class=\"ez-toc-section\" id=\"What_Is_An_Anchor\"><\/span>What Is An Anchor?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>YAML anchors are a feature which let you identify an item and then reference it elsewhere in your file. Anchors are created using the <code>&amp;<\/code> sign. The sign is followed by an alias name. You can use this alias later to reference the value following the anchor.<\/p>\n<p>Here\u2019s how you could use an anchor to avoid repeating container restart policies:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  httpd:&#13;\n    image: httpd:latest&#13;\n    restart: &amp;restartpolicy unless-stopped&#13;\n  mysql:&#13;\n    image: mysql:latest&#13;\n    restart: *restartpolicy<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The anchor is referenced using the <code>*<\/code> character and its alias. You must ensure there\u2019s no space between the <code>&amp;<\/code>\/<code>*<\/code> characters and the following alias name.<\/p>\n<p>This example shows how a single-line value can be reused with anchors. Changing the stack restart policy can now be done in one place, without editing the services individually.<\/p>\n<h2 id=\"multi-line-anchors\"><span class=\"ez-toc-section\" id=\"Multi-Line_Anchors\"><\/span>Multi-Line Anchors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Anchors can have multi-line values. You create them using the same syntax as a single-line anchor. This is useful when you need to provide a set of configuration details to multiple services.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  first:&#13;\n    image: my-image:latest&#13;\n    environment: &amp;env&#13;\n      - CONFIG_KEY&#13;\n      - EXAMPLE_KEY&#13;\n      - DEMO_VAR&#13;\n  second:&#13;\n    image: another-image:latest&#13;\n    environment: *env<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>second<\/code> service will now pull in the same environment variables as <code>first<\/code>. We haven\u2019t had to repeat the list of environment variables, making it much more maintainable in the future.<\/p>\n<h2 id=\"extending-anchor-values\"><span class=\"ez-toc-section\" id=\"Extending_Anchor_Values\"><\/span>Extending Anchor Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The environment example above takes the anchor\u2019s value and uses it as-is. You\u2019ll often want to extend the anchor to add additional values. You can do this with an alternative syntax.<\/p>\n<p>Modify the <code>second<\/code> service as follows:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  second:&#13;\n    image: another-image:latest&#13;\n    environment:&#13;\n      &lt;&lt;: *env&#13;\n      - AN_EXTRA_KEY&#13;\n      - SECOND_SPECIFIC_KEY<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The service now pulls in the base environment configuration from the <code>env<\/code> anchor. Additional keys are then added to the environment list. You may also override existing keys defined by the anchor.<\/p>\n<h2 id=\"using-extension-fields\"><span class=\"ez-toc-section\" id=\"Using_Extension_Fields\"><\/span>Using Extension Fields<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Another <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roach to modularisation is extension fields. These are special top-level YAML fragments which will be ignored by Docker.<\/p>\n<p>Docker usually tries to interpret any node at the root of a Compose file. The parser will ignore extension fields prefixed with <code>x-<\/code>. You can use these fields to encapsulate shared configuration for later reference. Combine extension fields with anchors to abstract sections out of your service definitions.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">x-env: &amp;env&#13;\n  environment:&#13;\n    - CONFIG_KEY&#13;\n    - EXAMPLE_KEY&#13;\n\u00a0\nservices:&#13;\n  first:&#13;\n    &lt;&lt;: *env&#13;\n    image: my-image:latest&#13;\n  second:&#13;\n    &lt;&lt;: *env&#13;\n    image: another-image:latest<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This Compose file is a further refinement over the example shown above. The environment variables no longer belong to either of the services. They\u2019ve been lifted out completely, into the <code>x-env<\/code> extension field.<\/p>\n<p>This defines a new node which contains the <code>environment<\/code> field. A YAML anchor is used (<code>&amp;env<\/code>) so both services can reference the extension field\u2019s value.<\/p>\n<h2 id=\"composability\"><span class=\"ez-toc-section\" id=\"Composability\"><\/span>Composability<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Making use of these features lets you break your Compose files into self-contained chunks. This helps you avoid overly repetitive service definitions. Anything common to more than one service should be lifted into an extension field.<\/p>\n<p>Besides aiding maintainability, this practice communicates your intentions to other collaborators. It\u2019s clear that any top-level extension fields contain generic fields. They\u2019re not tied to any particular service and can be freely reused.<\/p>\n<p>Anchors and extension fields let you <em>compose<\/em> your service definitions out of reusable blocks of YAML. By keeping each field small, your services can mix and match configuration sections from the available anchors. Maintaining your Compose files should become less of a chore.<\/p>\n<h2 id=\"other-approaches-to-modularity\"><span class=\"ez-toc-section\" id=\"Other_Approaches_to_Modularity\"><\/span>Other Approaches to Modularity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Besides anchors and extensions, don\u2019t forget you can always split your Compose definitions into multiple Compose files. This may become necessary when you have more than a handful of individual services.<\/p>\n<p>Using multiple Compose files lets you allocate each service its own file. You can also create override files, where a node\u2019s values get replaced or extended. Compose will merge all the files together to create the final runtime configuration.<\/p>\n<p><strong><code>service.yml<\/code><\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  service:&#13;\n    image: my-image:latest<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong><code>service-dev.yml<\/code><\/strong><\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  service:&#13;\n    environment:&#13;\n      - DEV_MODE=true<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>In this example, applying both Compose files would result in one service, <code>my-image:latest<\/code>, with the <code>DEV_MODE<\/code> environment variable set. To use multiple files with the Compose CLI, pass the <code>-f<\/code> flag:<\/p>\n<pre>docker-compose -f service.yml -f service-dev.yml up -d<\/pre>\n<p>Files are merged in the order specified.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker Compose files can become unwieldy and repetitive. If you\u2019re spending time copying values about, consider abstracting sections of your services into dedicated YAML blocks.<\/p>\n<p>Features such as anchors and extensions aid maintainability and make for an easier authoring experience. Not every Compose file will benefit \u2013 some services may have little in common with each other \u2013 so assess your specific stack before you start.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10765\/how-to-simplify-docker-compose-files-with-yaml-anchors-and-extensions\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Simplify Docker Compose Files With YAML Anchors and Extensions \u2013 CloudSavvy IT&#8221; Docker Compose lets you manage multiple Docker containers and their associated resources such as volumes and networks. You write declarative YAML files which Compose uses to create your container stack. Your docker-compose.yml files can become repetitive when you\u2019re working with a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":232179,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dc7b5a0.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-232178","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/232178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=232178"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/232178\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/232179"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=232178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=232178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=232178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}