Logo

dev-resources.site

for different kinds of informations.

Tips and Tricks for Docker Compose: Leveraging the override Feature

Published at
10/7/2024
Categories
docker
dockercompose
dev
devtips
Author
aless10
Categories
4 categories in total
docker
open
dockercompose
open
dev
open
devtips
open
Author
7 person written this
aless10
open
Tips and Tricks for Docker Compose: Leveraging the override Feature

Hello there! I would like to share some useful tips and tricks I've gathered while working with docker compose.
Today I am focusing on on the override feature. This powerful functionality allows you to customize your Docker configurations without duplicating your code. Letโ€™s dive in!

What is Docker Compose?

For those new to Docker Compose, itโ€™s a tool for defining and running multi-container Docker applications. With a single docker-compose.yml file, you can manage services, networks, and volumes, making it easier to work with complex applications.

Understanding the override feature

Docker Compose provides a mechanism to override configurations by using multiple Compose files. The default docker-compose.yml can be complemented with a second file named docker-compose.override.yml. When you run docker-compose up, Docker Compose automatically reads both files and merges their configurations.

Why Use Overrides?

Using overrides is beneficial for:

  • Environment-Specific Configurations: You can have different settings for development, testing, and production environments without changing the core configuration.
  • Easier Testing: You can tweak settings for testing purposes, like changing environment variables or ports, without affecting the main setup.
  • Simplified Configuration Management: Maintain a clean and organized setup while applying specific overrides as needed.

How to Use the override Feature

Hereโ€™s a step-by-step guide on how to implement the override feature:

Step 0: Start with the Dockefile

Create a simple Dockerfile so that you can test which compose file is running by passing an argument to the Dockerfile

FROM alpine:3.14
ARG HELLO_TO
ENV HELLO_TO=${HELLO_TO}
CMD echo "hello ${HELLO_TO}"
Enter fullscreen mode Exit fullscreen mode

This image reads the HELLO_TO argument and it just echo it to the console.

Step 1: Create Your Base compose file

Start by defining your main docker-compose.yml file. Hereโ€™s a simple example:

services:
  hello:
    build:
      context: .
      args:
        - HELLO_TO=compose
Enter fullscreen mode Exit fullscreen mode

In this example, we are passing the HELLO_TO argument to the Dockerfile with the value compose.

Step 2: Create the Override File

Next, create a docker-compose.override.yml file in the same directory. This file will include the settings you want to override. In this example, we set the argument HELLO_TO to override and we expect this to be printed to the console.

services:
  hello:
    build:
      context: .
      args:
        - HELLO_TO=override
Enter fullscreen mode Exit fullscreen mode

Step 3: Run!

Simply run your application with the following command:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Docker Compose will automatically merge both files, applying the overrides defined in docker-compose.override.yml.
You should see the following output

hello-1  | hello override
hello-1 exited with code 0
Enter fullscreen mode Exit fullscreen mode

This means that the container that ran was using the docker-compose.override.yml file, as expected.

Best Practices

  • Keep It Simple: Use overrides for small, environment-specific tweaks rather than extensive changes. This maintains clarity in your configurations.
  • Document Changes: Always comment on significant changes in your override files. This helps team members understand the purpose behind each modification.
  • Version Control: Ensure both your main and override files are version-controlled to track changes effectively.

Conclusion

The override feature in Docker Compose is a powerful tool that can help streamline your development process. By using it wisely, you can maintain clean, manageable configurations while easily adapting your applications to different environments.

You can find all the code I used in my repo dockerComposeGym where I am going to add other useful (at least for me) examples of how to use docker compose and its not so well known features.

If you found this tip helpful or have your own Docker Compose tricks to share, let me know in the comments! Happy bugging!

dev Article's
30 articles in total
Favicon
Latest Trends in AI in 2025
Favicon
Making Python CLIs More Maintainable: A Journey with Dynamic Command Loading
Favicon
Latest Trends in AI in 2026
Favicon
Meta-Arguments and Provider in Terraform Day 10
Favicon
Lazyvim version 14.x in WSL
Favicon
How to Print in GoLang
Favicon
Hello Dev.to Mates!
Favicon
What's new in Flutter 3.27
Favicon
Amazon S3 Security Essentials: Protect Your Data with These Key Practices
Favicon
Hacktoberfest 2024: A Celebration of Open Source and Community
Favicon
Tech and Tools I use
Favicon
โœ‹๐Ÿป Stop using VS Code Use This โ€” Cursor: The AI Code Editor
Favicon
Djanblog
Favicon
Judging the first DEV Web Game Challenge
Favicon
5 Key Tips for First-Time Software Outsourcing
Favicon
Tips and Tricks for Docker Compose: Leveraging the override Feature
Favicon
Flutter SwitchListTile and ListTile
Favicon
Which flutter features makes you to become a flutter developer?
Favicon
Flutter provides mainly 2 widgets Stateless and StatefulWidget
Favicon
Hello I'am from Brazil and my country recently blocked access to X (Twitter). Lets talk about it
Favicon
Back-End Development: Definition, Stats, & Trends To Follow In 2024
Favicon
Topographical surveyor in Coimbatore
Favicon
How do I close my DEV account
Favicon
Why firebase instead of other databases?
Favicon
Can anybody tell me which popular tech company has best UI LoginScreen?
Favicon
Another great neovim smooth scroll plugin
Favicon
Meme Monday
Favicon
Avoiding Common useState() Mistakes in React
Favicon
Frontend Challenge CSS Beach
Favicon
Ferramentas que nรฃo podem faltar no setup de um(a) dev

Featured ones: