Merge (original) (raw)

Docker Compose lets you merge and override a set of Compose files together to create a composite Compose file.

By default, Compose reads two files, a compose.yaml and an optionalcompose.override.yaml file. By convention, the compose.yamlcontains your base configuration. The override file can contain configuration overrides for existing services or entirely new services.

If a service is defined in both files, Compose merges the configurations using the rules described below and in theCompose Specification.

To use multiple override files, or an override file with a different name, you can either use the pre-definedCOMPOSE_FILE environment variable, or use the -f option to specify the list of files.

Compose merges files in the order they're specified on the command line. Subsequent files may merge, override, or add to their predecessors.

For example:

The compose.yaml file might specify a webapp service.

The compose.admin.yaml may also specify this same service:

Any matching fields override the previous file. New values, add to the webapp service configuration:

For more merging rules, seeMerge and override in the Compose Specification.

Additional information

A common use case for multiple files is changing a development Compose app for a production-like environment (which may be production, staging or CI). To support these differences, you can split your Compose configuration into a few different files:

Start with a base file that defines the canonical configuration for the services.

compose.yaml

In this example the development configuration exposes some ports to the host, mounts our code as a volume, and builds the web image.

compose.override.yaml

When you run docker compose up it reads the overrides automatically.

To use this Compose app in a production environment, another override file is created, which might be stored in a different git repository or managed by a different team.

compose.prod.yaml

To deploy with this production Compose file you can run

This deploys all three services using the configuration incompose.yaml and compose.prod.yaml but not the dev configuration in compose.override.yaml.

For more information, seeUsing Compose in production.

Docker Compose supports relative paths for the many resources to be included in the application model: build context for service images, location of file defining environment variables, path to a local directory used in a bind-mounted volume. With such a constraint, code organization in a monorepo can become hard as a natural choice would be to have dedicated folders per team or component, but then the Compose files relative paths become irrelevant.