DEV Community

Cover image for Tips and Tricks for Docker Compose: Leveraging the override Feature
Alessio Izzo
Alessio Izzo

Posted on

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!

Top comments (1)

Collapse
 
ngtduc693 profile image
Duc Nguyen Thanh

Oh, great! Thanks for your knowleadge