Anas Anjaria
Anas Anjaria's blog

Follow

Anas Anjaria's blog

Follow
Code Reuse in docker-compose Using YAML Anchor Feature

Photo by Pakata Goh on Unsplash

Code Reuse in docker-compose Using YAML Anchor Feature

Anas Anjaria's photo
Anas Anjaria
·Aug 14, 2022·

2 min read

As a programmer, we mainly focus on reusing code as much as possible. But have you ever heard of reusing code while writing docker-compose files?

No? Me neither until I found this docker-compose file [1].


services:  
    spilo1: &spilo  
        image: spilo          
        ...  
        hostname: spilo1  
        container_name: demo-spilo1

    spilo2:  
        <<: *spilo  
        hostname: spilo2  
        container_name: demo-spilo2

I was unable to understand this syntax, and it made me curious. After googling, I found that it’s a YAML feature called anchor.

According to these references [2][3]:

YAML also has a handy feature called ‘anchors’, which let you easily duplicate content across your document.

Let us look at an example below from the same references [2][3].

base: &base_anchor  
    name: Everyone has same name  

foo:  
    # inheriting base  
    <<: *base_anchor  
    age: 10

To inherit the content of the base, we need &base_anchor. Using this anchor, we can duplicate its content in foo using <<: *base_anchor.

Impressive right? But the question is:

where could we use this feature?

And the answer is:

To form a cluster locally using a docker-compose file.

Let’s use this feature by creating a MongoDB cluster using docker-compose.

Example — Local MongoDB cluster using docker-compose

⚠️ Please note I have followed this tutorial [4] for creating a MongoDB cluster locally. I am not going into its details as it is out of the scope of this article.

Before using the YAML anchor feature, our docker-compose file looks as follows.

version: '3.3'  
services:  
  mongo-test-1:  
    container_name: mongo-test-1  
    image: mongo:4.4.15  
    tmpfs: /data/db  
    ports:  
    - "27017:27017"  
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]  
  mongo-test-2:  
    container_name: mongo-test-2  
    image: mongo:4.4.15  
    tmpfs: /data/db  
    ports:  
    - "27018:27017"  
    volumes:  
    - ./initdb-script:/initdb-script:Z  
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]  
  mongo-test-3:  
    container_name: mongo-test-3  
    image: mongo:4.4.15  
    tmpfs: /data/db  
    ports:  
    - "27019:27017"  
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs" ]

It makes code more precise and readable after using this anchor feature.

version: '3.3'  
services:  
  mongo-test-1: &mongo  
    container_name: mongo-test-1  
    image: mongo:4.4.15  
    tmpfs: /data/db  
    ports:  
    - "27017:27017"  
    entrypoint: [ "/usr/bin/mongod", "--bind\_ip\_all", "--replSet", "dbrs" ]  
  mongo-test-2:  
    <<: *mongo  
    container_name: mongo-test-2  
    ports:  
    - "27018:27017"  
    volumes:  
    - ./initdb-script:/initdb-script:Z  
  mongo-test-3:  
    <<: *mongo  
    container_name: mongo-test-3  
    ports:  
    - "27019:27017"

Source Code

docker-compose-code-reuse

Resources

[1] https://github.com/zalando/spilo/blob/master/postgres-appliance/tests/docker-compose.yml

[2] https://stackoverflow.com/a/45805673

[3] https://learnxinyminutes.com/docs/yaml/

[4] https://blog.tericcabrel.com/mongodb-replica-set-docker-compose/


Originally posted on medium


Want to connect?

anasanjaria.bio.link

 
Share this