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!

devtips Article's
30 articles in total
Favicon
π“π‘πž 𝐌𝐒𝐬𝐭𝐚𝐀𝐞𝐬 𝐈 𝐌𝐚𝐝𝐞 𝐀𝐬 𝐚 𝐁𝐞𝐠𝐒𝐧𝐧𝐞𝐫 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐞𝐫
Favicon
Debugging Techniques Every Developer Should Know
Favicon
13 Hidden Windows Productivity Tricks You Should Know
Favicon
Tkinter: Python's Secret Weapon for Stunning GUIs
Favicon
Tips and Tricks for Docker Compose: Leveraging the override Feature
Favicon
Essential Security Practices for Web Developers: Keep Your Code Safe and Sound
Favicon
13 Hidden Windows Productivity Tricks You Should Know
Favicon
Automating Laravel Tasks with JSON-Based Task Runner
Favicon
8 Plugins You Should Add To Your IDE And Why
Favicon
How to Safely Edit a Git Commit Message After Pushing *Demystifying one of the tricky aspects of Git*
Favicon
A Guide to Efficient Problem Solving: Techniques for Tackling Coding Challenges
Favicon
How to improve the PageSpeed score of your Nuxt.js website in 6 steps
Favicon
Top 10 Vue.js Resources For Your Project πŸš€
Favicon
How is testing different in monolith and microservices architectures?
Favicon
Top 15 Flutter Tools that you should know
Favicon
Top 10 tools for (not only) multilingual Android development
Favicon
Why allow users to switch languages
Favicon
Top 10 Android Libraries to boost your development in 2022
Favicon
Seven reasons why you should use Microservices architecture
Favicon
TypeScript library for Localazy API
Favicon
Cheatsheet: Getting started with Software Localization
Favicon
My paper to-do strategy
Favicon
Measuring productivity with GitHub issues
Favicon
How to set up `git bro` command with git alias
Favicon
Digital resilience: redundancy for websites and communications
Favicon
CodeSandbox init shortcuts
Favicon
How to become a software developer
Favicon
How to write good documentation
Favicon
So you're the family tech support
Favicon
Transforming an object to array in JavaScript

Featured ones: