Programming
Code Reuse in Docker-Compose Using YAML Anchor Feature
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? Answer — 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"
GitHub’s commit view to make changes more visible.
Source Code
I would recommend going through this repository via commits. It will give more clarity about the concept due to syntax highlighting. It gives us the same feeling as if we are reviewing some pull requests.
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/
Thanks for reading. If you enjoyed this post, you might like my other stories in the programming series.
Want to connect?
http://anasanjaria.bio.link/Want to subscribe to my newsletter?
https://medium.com/subscribe/@anasanjaria