Sub topic 12
Splitting and reusing configuration with include and extends
This chapter assumes you know how Compose merges files, which the earlier chapters on -f, COMPOSE_FILE and merge rules covered. You should also be comfortable with docker compose config. It also assumes Docker Compose v2.20 or later, because include does not exist in older releases. extends has been available much longer.
Override files and profiles change a project's behavior per environment. include and extends solve a different problem: a single compose.yaml that has grown too large, or definitions that several projects should share.
Two tools, two levels
include works on whole files. It pulls another Compose file into the current one, resources and all. extends works on a single service. It copies one service definition into another service and lets you adjust it.
| Need | Tool | Operates on |
|---|---|---|
| Change the same project per environment | `-f` / override files | The whole merged model |
| Pull in a self-contained set of services owned by another team or folder | `include` | A whole file |
| Reuse one service definition as the starting point for another | `extends` | One service |
| Make services optional | `profiles` | One service |
Including another Compose file
Suppose the database definition is owned by a platform team and lives in its own folder.
project/
compose.yaml
infra/
compose.yaml
common/
services.yamlThe included file is a normal, complete Compose file.
# infra/compose.yaml
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:The main file lists it under the top-level include key.
# compose.yaml
include:
- infra/compose.yaml
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
depends_on:
- dbweb can depend on db even though db is defined elsewhere. After loading, the included services, volumes and networks are part of the same application. docker compose up starts both, and docker compose config prints them in one document.
How include differs from -f
-f merges files into one model. All files share the project directory of the first file, and a later file can change anything in an earlier one.
include loads each file as a separate model first, then combines the results. This has three consequences.
- Relative paths inside the included file, such as
build: .or./data:/data, are resolved against the included file's own directory. With-f, they resolve against the first file's directory. - The included file can be checked on its own. You can run
docker compose -f infra/compose.yaml configand get a valid result. - The including file cannot override included resources by redefining them. If both files define a service named
db, Compose stops with an error that names the conflict. It does not merge.
Long syntax
A plain path is enough for simple cases. The long form controls the project directory and the variables used when the included file is parsed.
include:
- path: ../shared/compose.yaml
project_directory: ../shared
env_file: ../shared/dev.envenv_file supplies the values used to interpolate variables inside the included file. By default, Compose reads a .env file from the included file's project directory, so a ${DB_PASSWORD} in an included file may read a different .env than the one next to your main file. When a variable is empty in one place and set in another, run docker compose config and read the result.
Changing an included service
You cannot redefine an included service in the main file. To adjust one, give path a list. Compose merges those files first, using the normal merge rules, and then includes the result.
# compose.yaml
include:
- path:
- infra/compose.yaml
- infra/dev-tweaks.yaml# infra/dev-tweaks.yaml
services:
db:
ports:
- "5432:5432"This keeps the platform team's file untouched. Your tweaks live in a separate file that you own.
Profiles and includes
Included services keep their profiles. If infra/compose.yaml puts adminer in the debug profile, it stays off until you activate debug. You can also stack the mechanisms. A main file can use include, and you can still pass -f compose.yaml -f compose.prod.yaml. The includes of every listed file are processed.
Extending a service
extends copies a service definition into another service. The base can live in a different file or in the same file. Here the shared settings are in common/services.yaml.
# common/services.yaml
services:
app-base:
image: example/web:1.0
restart: unless-stopped
environment:
LOG_LEVEL: info
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30sThe main file builds two services from it.
# compose.yaml
include:
- infra/compose.yaml
services:
web:
extends:
file: common/services.yaml
service: app-base
ports:
- "8000:8000"
depends_on:
- db
worker:
extends:
file: common/services.yaml
service: app-base
command: ["python", "worker.py"]
environment:
LOG_LEVEL: debug
depends_on:
- dbdocker compose config shows web and worker each with the image, restart policy and health check. worker also has the LOG_LEVEL value changed to debug. To extend a service from the same file, leave out file and give only service.
Merge rules for extends
The extended service is the base, and the local definition is merged over it. These are the same rules as for multiple files.
- Scalars such as
commandorimageare replaced by the local value. - Mappings such as
environmentare merged key by key, and the local value wins on conflicts. - Most lists, such as
ports, are combined.
The !override and !reset tags from the previous chapter work here too, where your Compose version supports them.
What extends does not copy
Compose does not copy depends_on, links or volumes_from from the base service. These attributes tie a service to other services, and the base may belong to a file that does not define those services. That is why web and worker above declare depends_on themselves.
Relative paths in an extended service, such as build contexts and bind mounts, are rewritten so that they still point at the files next to the base. Confirm this with docker compose config before relying on it.
Extends chains are allowed, so a base can itself extend another service. A circular chain is an error.
Common errors
- Conflicting resources after include. Two included files, or an included file and the main file, define the same service, volume or network name. Rename one, or merge them deliberately with the
pathlist form. - Unknown top-level key
include. The Compose version is older than 2.20. Rundocker compose versionand upgrade. - Missing dependency after extends. The service starts before its database because
depends_onwas not copied. Declare it on the extending service. - Wrong relative paths. A bind mount in an included file resolves against that file's folder. Use
project_directoryif you want a different base, and check the result withconfig. - Variable empty only in an included file. The included file's
.envorenv_filesupplied the value, not your shell's.env. Setenv_fileexplicitly.
Choosing between them
Use include when a group of services is a unit with its own folder and owner, such as a database stack shared by many projects. Use extends when several services in your project start from the same settings. Keep using override files and -f for the environment differences. Use profiles for services that are optional.
A good layout has a small main file that uses include for infrastructure, extends for repeated service settings, and compose.override.yaml and compose.prod.yaml on top for environment differences.
Exercise
A teammate includes infra/compose.yaml, which defines a db service, and then adds a db: section to their own compose.yaml to change the published port. Compose fails. Why, and what are two ways to get the port change?
Show answer
Included resources cannot be redefined in the including file. Compose reports a conflict instead of merging the two db definitions. One fix is to list a second file under the include's path, for example path: [infra/compose.yaml, infra/dev-tweaks.yaml], where the second file sets ports for db. Another is to leave include as it is and add the port in an override file that is passed with -f or loaded as compose.override.yaml.
Exercise
A service api uses extends on a base service that has depends_on: [db]. After docker compose config, api has no depends_on. What happened, and how do you fix it?
Show answer
Compose does not copy depends_on, links or volumes_from through extends. Add depends_on: [db] to the api service itself, then run docker compose config again to confirm it appears.