Skip to content

OCIO y TECnología

  • Privacy Policy
Offcanvas

  • Register
  • Lost your password ?

OCIO y TECnología

  • Home » 
  • Tecnología » 
  • Docker » 
  • Using YAML anchors & aliases on docker-compose.yml

Using YAML anchors & aliases on docker-compose.yml

Emilio González Montaña 2020/03/04 1

One of the most forgotten feature of the YAML files is the usage of templates in order to decrease the copy&paste effect on them. And yes, it’s a feature of YAML files, not from docker.

Repeated code is always considered a very bad practice, but configuration files & other scripts are always ignored on the already accepted as good practices in development circles.

Why?

On very dimple docker-compose.yml files, usually with only one service, usually there is no need for this feature. For example:

version: '3.7'
services:
  anthill:
    image: ociotec/anthill:1.0.0
    ports:
      - '80'
    networks:
      - proxy_net
    deploy:
      placement:
        constraints:
          - node.labels.arch == x86
      labels:
        traefik.enable: 'true'
        traefik.port: '80'
        traefik.docker.network: 'proxy_net'
        traefik.frontend.rule: 'Host: anthill.eht.ociotec.com'
networks:
  proxy_net:
    external: true

But when you have to define several services with same network definitions, maybe placement constrains, labels… YAML anchors & labels start to shine as a useful tool.

Now imagine that you want to add a second service really similar to the first one:

version: '3.7'
services:
  anthill:
    image: ociotec/anthill:1.0.0
    ports:
      - '80'
    networks:
      - proxy_net
    deploy:
      placement:
        constraints:
          - node.labels.arch == x86
      labels:
        traefik.enable: 'true'
        traefik.port: '80'
        traefik.docker.network: 'proxy_net'
        traefik.frontend.rule: 'Host: anthill.eht.ociotec.com'
  conway:
    image: ociotec/conway:1.0.0
    ports:
      - '80'
    networks:
      - proxy_net
    deploy:
      placement:
        constraints:
          - node.labels.arch == x86
networks:
  proxy_net:
    external: true

On this example the only difference is the service name, image & one of the deployment labels, the rest is completely repeated.

The solution

A code snippet is the best description:

version: '3.7'
services:
  anthill: &service
    image: ociotec/anthill:1.0.0
    ports:
      - '80'
    networks:
      - proxy_net
    deploy:
      placement:
        constraints:
          - node.labels.arch == x86
      labels: &labels
        traefik.enable: 'true'
        traefik.port: '80'
        traefik.docker.network: 'proxy_net'
        traefik.frontend.rule: 'Host: anthill.eht.ociotec.com'
  conway:
    <<: *service
    image: ociotec/conway:1.0.0
    deploy:
      <<: *deploy
      labels:
        <<: *labels
        traefik.frontend.rule: 'Host: conway.eht.ociotec.com'
networks:
  proxy_net:
    external: true

The magic:

  • COPY: anchors are defined with & symbol after the element to copy; in the previous example there are 3 anchors:
    • one for the full service anthill,
    • one for the deploy section,
    • and other for the deployment labels.
  • PASTE: labels are defined with <<: * symbol, they should use the same name than the anchor; in the previous example there are 3 labels:
    • one for copying the full service definition into conway service,
    • one for the deploy section,
    • and other to copy the deployment labels.

The idea is that in the “repeated” service we only introduce what is different, take care than you could be tempted to omit the second and third anchors/labels but they are needed because we are redefining the the placement label traefik.frontend.rule.

If they were omitted the YAML syntax interprets that you don’t need placement section and the first 3 labels, something like this:

...
  conway:
    image: ociotec/conway:1.0.0
    ports:
      - '80'
    networks:
      - proxy_net
    deploy:
      labels:
        traefik.frontend.rule: 'Host: conway.eht.ociotec.com'
...

Conclusions

Once you start to define a lot of docker-compose.yml files you will find yourself repeating again and again the content of the file.

One day you realize you found & fixed one bug in one of the services but you missed other service in the same file.

By the way, the services used in this example are online, they are cellular automatons that I have developed myself, they are available here:

  • Anthill (GitHub, docker hub)
  • Conway’s life (GitHub, docker hub)

Useful resources

  • Don’t Repeat Yourself with Anchors, Aliases and Extensions in Docker Compose Files
  • YAML Ain’t Markup Language (YAML™) Version 1.2
  • Docker compose reference – Fragments
  • Docker compose reference – Extension
  • Docker compose reference – Variable interpolation

Comparte esto:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
Tags : Tags anthill   cellular-automatons   conways-life   docker   docker-compose.yml   docker-swarm   DRY   YAML
Share
facebookShare on FacebooktwitterShare on TwitterpinterestShare on Pinterest
linkedinShare on LinkedinvkShare on VkredditShare on ReddittumblrShare on TumblrviadeoShare on ViadeobufferShare on BufferpocketShare on PocketwhatsappShare on WhatsappviberShare on ViberemailShare on EmailskypeShare on SkypediggShare on DiggmyspaceShare on MyspacebloggerShare on Blogger YahooMailShare on Yahoo mailtelegramShare on TelegramMessengerShare on Facebook Messenger gmailShare on GmailamazonShare on AmazonSMSShare on SMS
Post navigation
Previous post

Portainer

Next post

Docker volume with NFS driver

Emilio González Montaña

Related Posts

Categories Data bases  Docker  Tecnología Using YAML anchors & aliases on docker-compose.yml

Recover corrupted Postgres DB WAL

Categories Docker  Linux  Tecnología  Utils Using YAML anchors & aliases on docker-compose.yml

Install WSL2 + Docker on Windows

Categories Docker  Tecnología  YAML Using YAML anchors & aliases on docker-compose.yml

Docker swarm Traefik reverse proxy

1 Comment

  1. Author
    1
    Emilio González Montaña
    1 year ago

    Updated with some extra interesting info to Docker compose references

    Reply

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Detachable screens to avoid SSH disconnections
  • Expose WSL2 port to the network
  • Rosaleda (parque El Retiro, Madrid)
  • Back to the painting table
  • Recover corrupted Postgres DB WAL

Categories

  • Aficiones
  • AI
  • Bricolage
  • Data bases
  • Docker
  • Escenografía
  • Eventos
  • Fotografía
  • Hardware
  • Linux
  • Networking
  • Partidas
  • Proxmox
  • Sin categoría
  • Tecnología
  • Utils
  • Viajes
  • Virtualization
  • Warhammer
  • YAML

Tags

anthill (1) apt (5) cellular-automatons (1) ceph (2) context (1) conways-life (1) cortador (1) debian (3) docker (9) docker-compose.yml (4) docker-swarm (5) DRY (1) El Imperio (2) Enanos (2) escenografía (3) fotos (3) GlusterFS (1) Guerreros del Caos (2) informática (1) Linux (8) M.2 (1) Mac (1) Madrid (2) maqueta (2) MariaDB (2) miniaturas (4) MySQL (2) NFS (2) NVMe (1) partida (2) poliestireno (1) portainer (1) proxmox (2) rocas (1) Sony A65 (2) SSH (5) ssh-key (1) Ubuntu (8) update (2) volume (1) Warhammer (11) Windows (3) WSL (2) WSL2 (2) YAML (1)

Archives

  • June 2024
  • May 2024
  • April 2024
  • February 2024
  • January 2024
  • September 2023
  • August 2023
  • June 2023
  • December 2021
  • April 2021
  • May 2020
  • April 2020
  • March 2020
  • November 2019
  • July 2018
  • February 2017
  • June 2015
  • April 2014
  • April 2011
  • January 2011
  • July 2010
  • June 2010
  • March 2010
  • November 2009
  • June 2009
  • December 2008
  • November 2008
  • October 2008
  • June 2008
  • May 2008
  • October 2007

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Copyright © 2025 OCIO y TECnología - Powered by Nevothemes.
Offcanvas