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:
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? ??
Senior Analytics Engineer | Data Scientist | Data Engineer | GCP | Databricks
1 个月Thank you for sharing.
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?
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 ??????
Software Engineer MERN | React.JS | Nodejs | Javascript | Typescript | MongoDB | GCP | Python
1 个月Really nice to know tks for sharing this tips
Fullstack Software Engineer | Node | Typescript | React | Next.js | AWS | Tailwind | NestJS | TDD | Docker
1 个月Useful tips. Thanks for sharing Elison G.