include
As an application grows, one compose file starts to carry pieces that belong to different teams: the product services, a database setup, a monitoring stack, a local mail catcher. Override files don't help here, because they all have to be listed with -f and their relative paths resolve from the first file's folder. extends only copies a single service.
The top-level include element solves this: it pulls another complete Compose application (services, networks, volumes, configs, secrets) into your project, with paths and variables resolved relative to that application's own folder. Each included file stays a self-contained, testable unit owned by whoever maintains it.
A first include
The monitoring team owns a folder observability/ with its own compose file and config:
# $ docker compose config --no-path-resolution
include:
- observability/compose.yaml
services:
api:
image: ghcr.io/acme/api:3.1
volumes:
- ./src:/app/src
# --- observability/compose.yaml
services:
prometheus:
image: prom/prometheus:v3.5.0
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prom-data:/prometheus
volumes:
prom-data:name: demo
services:
api:
image: ghcr.io/acme/api:3.1
networks:
default: null
volumes:
- type: bind
source: ./src
target: /app/src
bind: {}
prometheus:
image: prom/prometheus:v3.5.0
networks:
default: null
volumes:
- type: bind
source: observability/prometheus.yml
target: /etc/prometheus/prometheus.yml
read_only: true
bind: {}
- type: volume
source: prom-data
target: /prometheus
volume: {}
networks:
default:
name: demo_default
volumes:
prom-data:
name: demo_prom-data
The included ./prometheus.yml became observability/prometheus.yml: relative to the file that wrote it, not to the main file. The included application also joined the same project: one demo_default network, one project name, so api can reach prometheus by name. The volume is still prefixed with the main project name (demo_prom-data).
compose.yaml ──include──▶ observability/compose.yaml
api prometheus (+ prom-data, ./prometheus.yml)
└──────────── one project "demo", one default network ────────────┘Variables and .env per included file
Each included application reads its own .env (next to its compose file) for interpolation. The main project's environment still wins for variables it sets, so the parent can steer its children:
# $ docker compose config --variables
# $ docker compose config
include:
- db/compose.yaml
services:
api:
image: ghcr.io/acme/api:${API_TAG:-latest}
depends_on: [db]
# --- db/compose.yaml
services:
db:
image: postgres:${PG_VERSION:-16}
# --- db/.env
PG_VERSION=17
# --- .env
API_TAG=3.1$ docker compose config --variables
NAME REQUIRED DEFAULT VALUE ALTERNATE VALUE
API_TAG false latest
PG_VERSION false 16
$ docker compose config
name: demo
services:
api:
image: ghcr.io/acme/api:3.1
networks:
default: null
depends_on:
db:
condition: service_started
required: true
db:
image: postgres:17
networks:
default: null
networks:
default:
name: demo_default
--variableslists the variables of the whole project, including the included file'sPG_VERSION, with their defaults.db/.envsetPG_VERSION=17for the included file.apicandepends_on: [db]even thoughdblives in another file. After inclusion it's one project.
If the main .env also set PG_VERSION=15, that value would win over db/.env.
The long syntax
include:
- path: # one file, or a list merged like -f a -f b
- db/compose.yaml
- db/compose.dev.yaml
project_directory: db # base for relative paths (default: the first file's folder)
env_file: db/settings.env # instead of db/.env (one file or a list)Listing several files under path merges them exactly like override files, before they are included. That lets a component ship its own development override:
include:
- path: [db/compose.yaml, db/compose.dev.yaml]
services:
api:
image: ghcr.io/acme/api:3.1
# --- db/compose.yaml
services:
db:
image: postgres:17
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
# --- db/compose.dev.yaml
services:
db:
ports: ["127.0.0.1:5432:5432"]name: demo
services:
api:
image: ghcr.io/acme/api:3.1
networks:
default: null
db:
image: postgres:17
networks:
default: null
volumes:
- type: volume
source: pgdata
target: /var/lib/postgresql/data
volume: {}
ports:
- mode: ingress
host_ip: 127.0.0.1
target: 5432
published: "5432"
protocol: tcp
networks:
default:
name: demo_default
volumes:
pgdata:
name: demo_pgdata
Binding the published port to 127.0.0.1 keeps the dev database off your Wi-Fi: only your laptop can connect.
Name clashes
Included applications share one namespace. What happens when two of them, or the main file, define the same service?
include:
- db.yaml
services:
db:
image: postgres:16
# --- db.yaml
services:
db:
image: postgres:17
environment:
POSTGRES_DB: shop
ports: ["5432:5432"]name: demo
services:
db:
environment:
POSTGRES_DB: shop
image: postgres:16
networks:
default: null
ports:
- mode: ingress
target: 5432
published: "5432"
protocol: tcp
networks:
default:
name: demo_default
With current Compose, a service in the including file is merged over the included one, like an override: the image became postgres:16 while the environment and ports came from db.yaml. That's useful for small local tweaks, but it's also an easy way to change a shared component by accident. Older Compose versions refused such a file with an error. Keep service names unique across included files (prefix them: obs-prometheus, db-postgres) and make overrides deliberate.
A model of include resolution
The two jobs include does, rebasing paths and choosing variable values, in a few lines:
import posixpath
def rebase(path, included_dir):
"""Relative paths in an included file are relative to that file's folder."""
if path.startswith("/") or not path.startswith("."):
return path # absolute path or named volume
return posixpath.normpath(posixpath.join(included_dir, path))
def variable(name, default, parent_env, child_env):
"""Main project's environment beats the included file's .env, which beats the default."""
for source, env in (("parent", parent_env), ("child .env", child_env)):
if name in env:
return env[name], source
return default, "default"
for p in ["./prometheus.yml", "../shared/ca.pem", "prom-data", "/etc/hosts"]:
print(f"{p:<20} -> {rebase(p, 'observability')}")
print()
child = {"PG_VERSION": "17"}
for parent in ({}, {"PG_VERSION": "15"}):
value, src = variable("PG_VERSION", "16", parent, child)
print(f"parent env {parent!s:<24} -> postgres:{value} (from {src})")
value, src = variable("PG_VERSION", "16", {}, {})
print(f"{'no .env anywhere':<35} -> postgres:{value} (from {src})")./prometheus.yml -> observability/prometheus.yml
../shared/ca.pem -> shared/ca.pem
prom-data -> prom-data
/etc/hosts -> /etc/hosts
parent env {} -> postgres:17 (from child .env)
parent env {'PG_VERSION': '15'} -> postgres:15 (from parent)
no .env anywhere -> postgres:16 (from default)
include vs extends vs -f
-f a -f b |
extends |
include |
|
|---|---|---|---|
| Unit of reuse | whole configuration | one service | whole application |
| Relative paths | from the first file's folder | from the file that defines them | from the included file's folder |
.env |
the project's only | the project's only | each included app has its own |
| Who decides | the person running the command | the extending service | the main compose file |
| Typical use | environments (dev/prod) | service templates | team-owned components |
include is a Compose-only feature; docker stack deploy rejects it ((root): additional property 'include' is not allowed), so render the full file with docker compose config before deploying a stack. See From Compose to Swarm.
Learn more: Override Files & Merging · extends · Variables & Interpolation
Test yourself
observability/compose.yaml mounts ./prometheus.yml. The main compose.yaml includes it. Which host file is mounted?
- ./prometheus.yml next to the main file
- observability/prometheus.yml
- The current directory's prometheus.yml
- It fails: relative paths aren't allowed in included files
Which .env does an included application use for interpolation by default?
- Only the main project's .env
- The .env next to the included compose file (the main environment still wins for variables it sets)
- None
- Only env_file entries
What is the main difference between include and extends?
- include copies one service, extends a whole project
- include brings in a complete application (services, volumes, networks), extends reuses a single service
- They are aliases
- extends only works across files