Override Configurations in Docker Compose

Learn to override configurations in the base docker-compose file.

As developers, we work in a variety of environments. Having multiple environments helps us customize the software that we build and allows us to adapt it to the environment in which it runs.

In this lesson, we use docker-compose files to customize services in multiple environments.

Override existing settings from docker-compose file

Suppose we have a local environment where we develop, a CI environment where our code is integrated, and a production environment where our services run. We may want to use environment-specific customizations depending on the environment we’re in.

For example, here are a few things we might want to do depending on the environment:

  • We might mount our host volume inside the container volume while developing locally so that changes made to our source code are automatically reflected in the container.

  • We might map some ports on the host machine locally, which is unnecessary or needed in other environments.

  • In a CI environment, we might start up a mock server to help usmock API calls to remote/external services to run automated tests.

These are only a few examples of scenarios where we might need a specific setup to one or two environments rather than to all of them.

The docker-compose tool supports sharing configurations across multiple files and projects.

Suppose we have a base docker-compose.yml file as shown below:

Get hands-on with 1200+ tech skills courses.