Docker Best Practices: Reduce Errors by 30% with Smarter Compose Files

Docker Best Practices: Reduce Errors by 30% with Smarter Compose Files

It may seem quite strange due to the way it's done, but there's a way to avoid rework when writing a docker-compose.yml.

There are two structures that help reduce rework:

  • Extensions (x-): Allow defining reusable custom configurations, such as variables, networks, or volumes, without cluttering the main structure of docker-compose.yml.
  • Anchors (& and *): Facilitate the reuse of configuration snippets, avoiding duplication and ensuring consistency across services.

As you can see in the image below, an extension allows you to reuse directives in other services.

To create an extension in a Docker Compose file, simply start the directive at the top of the file with "x-".

It will look something like: "x-common-values" or "x-values".

Then, define it using an ampersand (&) followed by a name:

Example: "x-common-values: &common-values"


The x-common-config extension is used to define shared settings like restart policy and network configuration. By referencing <<: *common-config, the app, database, and cache containers all inherit these common settings, keeping the docker-compose.yml clean and consistent. This reduces repetition and makes updates easier.

After creating the extension, to use it within a service, just insert the instruction followed by

"<<: *" the extension name.

It will look like this: "<<: *common-values".

And voilà!

Your docker-compose.yml file will now be more concise, readable, and require less rework.

If you ever need to make a change, you’ll only have to update it in one place!

And you? Have you ever used extensions in your Docker Compose files? ??



José Daniel Abrantes de Lacerda

Senior Analytics Engineer | Data Scientist | Data Engineer | GCP | Databricks

1 个月

Thank you for sharing.

Nikhil Bhagare

Senior Software Engineer | Mobile App Development & Automation | Expertise in Native & Hybrid Solutions | Mentor & Tech Lead | Team Collaboration

1 个月

So, & is for saving a reusable configuration block, * is for copying and pasting those blocks, and x- is used for defining user-defined reusable configurations. Is this correct? If you want to share custom or Docker-provided configurations across multiple docker-compose files, what should be used?

Giancarlo Mendon?a

Diretor de TI | Red Team | Threat Intelligence | OSINT | Senior Developer | IA Developer | DevOps | Docker | Python | Node.js | PHP | PostgreSQL | ITIL | ISO 27001 | LGPD | OWASP | CAP | DCPT?

1 个月

Aí sim!! Parabéns meu amigo ??????

Alexandre Pereira

Software Engineer MERN | React.JS | Nodejs | Javascript | Typescript | MongoDB | GCP | Python

1 个月

Really nice to know tks for sharing this tips

Kaique Perez

Fullstack Software Engineer | Node | Typescript | React | Next.js | AWS | Tailwind | NestJS | TDD | Docker

1 个月

Useful tips. Thanks for sharing Elison G.

要查看或添加评论,请登录

Elison G.的更多文章

社区洞察

其他会员也浏览了