Sub topic 12
Splitting and Reusing Configuration with include and extends
This chapter assumes you can read Compose YAML, know how compose.yaml and override files merge, and have run docker compose config. Override files layer one environment on top of another. include and extends solve a different problem: sharing definitions between files, services, or teams.
Two tools for two kinds of reuse
includepulls a whole Compose file into your project. Its services, networks, and volumes become part of the application.extendscopies one service definition from another file or service into a new service, which can then change it.
| Need | Tool |
|---|---|
| Reuse a complete sub-application, such as a shared database stack | `include` |
| Reuse one service as a template for several similar services | `extends` |
| Change settings per environment | Override files |
Including another Compose file
Suppose the database setup lives in its own directory and is maintained separately.
# infra/db.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 includes it and defines only the web service.
# compose.yaml
include:
- infra/db.yaml
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
depends_on:
- dbCompose resolves relative paths inside an included file against that file's own directory, not the main project directory. This is why a shared file can be reused from many projects.
An included file is loaded as a separate unit and is not merged field by field into your services. If your main file defines a service with the same name as an included one, Compose reports a conflict instead of merging them. This differs from -f, where later files merge over earlier ones. To change an included service, edit its file or use the long form of include, which accepts path, project_directory, and env_file for the included model.
include:
- path: infra/db.yaml
env_file: infra/db.envReusing a service with extends
extends is set on a service. It names a base service and the file it comes from.
# common.yaml
services:
app:
image: example/web:1.0
environment:
LOG_LEVEL: info
restart: unless-stopped# compose.yaml
services:
web:
extends:
file: common.yaml
service: app
ports:
- "8000:8000"
worker:
extends:
file: common.yaml
service: app
command: ["python", "worker.py"]
environment:
LOG_LEVEL: debugBoth services start from app. The local keys are then merged over the inherited ones using the same rules as override files: the command replaces, environment entries are combined by key, and ports are appended. Omit file to extend a service in the same file.
Limits and common errors
- Services that use
extendscannot inheritdepends_on,volumes_from, orlinks. Declare those in the extending service. - Circular
extendsorincludechains fail with an error. - Named volumes and networks that a base service references are not carried along. Define them in the file that uses them, or bring them in with
include. - Two includes that define the same service name conflict.
Combine these with earlier techniques. A project can include shared infrastructure, extend a common service template, and still use compose.override.yaml and profiles for per-environment behavior.
Exercise
Your compose.yaml includes infra/db.yaml, and you also define a db service in compose.yaml to change its image. What happens, and what should you do instead?
Show answer
Compose reports a conflict because the same service name appears in the main file and an included file. Include does not merge them. Change the image in infra/db.yaml, or have the main file define its own db with extends pointing to the shared definition.