---
description: How merging Compose files works
keywords: compose, docker, merge, compose file
title: Merge Compose files
linkTitle: Merge
weight: 10
aliases:
- /compose/multiple-compose-files/merge/
---
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 optional
`compose.override.yaml` file. By convention, the `compose.yaml`
contains 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 the
[Compose Specification](/reference/compose-file/merge.md).
## How to merge multiple Compose files
To use multiple override files, or an override file with a different name, you
can either use the pre-defined [COMPOSE_FILE](../environment-variables/envvars.md#compose_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:
```console
$ docker compose -f compose.yaml -f compose.admin.yaml run backup_db
```
The `compose.yaml` file might specify a `webapp` service.
```yaml
webapp:
image: examples/web
ports:
- "8000:8000"
volumes:
- "/data"
```
The `compose.admin.yaml` may also specify this same service:
```yaml
webapp:
environment:
- DEBUG=1
```
Any matching
fields override the previous file. New values, add to the `webapp` service
configuration:
```yaml
webapp:
image: examples/web
ports:
- "8000:8000"
volumes:
- "/data"
environment:
- DEBUG=1
```
## Merging rules
- Paths are evaluated relative to the base file. When you use multiple Compose files, you must make sure all paths in the files are relative to the base Compose file (the first Compose file specified
with `-f`). This is required because override files need not be valid
Compose files. Override files can contain small fragments of configuration.
Tracking which fragment of a service is relative to which path is difficult and
confusing, so to keep paths easier to understand, all paths must be defined
relative to the base file.
>[!TIP]
>
> You can use `docker compose config` to review your merged configuration and avoid path-related issues.
- Compose copies configurations from the original service over to the local one.
If a configuration option is defined in both the original service and the local
service, the local value replaces or extends the original value.
- For single-value options like `image`, `command` or `mem_limit`, the new value replaces the old value.
original service:
```yaml
services:
myservice:
# ...
command: python app.py
```
local service:
```yaml
services:
myservice:
# ...
command: python otherapp.py
```
result:
```yaml
services:
myservice:
# ...
command: python otherapp.py
```
- For the multi-value options `ports`, `expose`, `external_links`, `dns`, `dns_search`, and `tmpfs`, Compose concatenates both sets of values:
original service:
```yaml
services:
myservice:
# ...
expose:
- "3000"
```
local service:
```yaml
services:
myservice:
# ...
expose:
- "4000"
- "5000"
```
result:
```yaml
services:
myservice:
# ...
expose:
- "3000"
- "4000"
- "5000"
```
- In the case of `environment`, `labels`, `volumes`, and `devices`, Compose "merges" entries together with locally defined values taking precedence. For `environment` and `labels`, the environment variable or label name determines which value is used: