Splitting Docker Compose stacks with profiles and overrides
Cut local memory usage and port conflicts by combining Docker Compose profiles with git-ignored compose.override.yaml files and config verification.
As a backend grows, a single
compose.yamlquickly balloons to 10+ containers that eat 14 GB of RAM just to test one CRUD endpoint. This post shows how to combine Docker Composeprofileswith git-ignoredcompose.override.yamlfiles so every engineer runs only the services and local overrides they need.
Tested with Docker Engine 27, Docker Compose v2, PostgreSQL 17, Redis 7, and Apache Kafka 3.9.
The problem with one giant compose.yaml
Early in a project, putting everything into one compose.yaml feels great: new teammates run docker compose up -d and get the whole stack. Six months later, that same file holds PostgreSQL, Redis, Kafka, Kafka UI, Elasticsearch, Kibana, Prometheus, Grafana, and Mailpit.
At that point, daily development degrades fast:
- Memory exhaustion: Starting 10–15 containers claims 12–14 GB of your Docker Desktop VM memory before IntelliJ IDEA even finishes indexing or Gradle runs a build.
- Unnecessary noise: If you’re fixing a simple user profile query, you don’t need Kafka brokers or Grafana dashboards burning CPU cycles and battery.
- Duplicate-file sprawl: Splitting the stack into
docker-compose-backend.yml,docker-compose-kafka.yml, anddocker-compose-search.ymlcreates copy-pasted network and volume definitions that drift out of sync within weeks.
Different tasks genuinely need different subsets of infrastructure:
| Task | Required services | Unneeded overhead |
|---|---|---|
| Core CRUD / REST API | PostgreSQL, Redis | Kafka, Elasticsearch, Grafana |
| Event consumer / EDA | PostgreSQL, Redis, Kafka | Elasticsearch, Kibana, Grafana |
| Search query tuning | PostgreSQL, Elasticsearch | Kafka, Mailpit |
| Frontend integration | Auth, Core API, Mock server | Prometheus, Grafana, background workers |
Dividing responsibilities between profiles and overrides
Docker Compose v2 gives you two built-in mechanisms that solve different halves of this problem: Profiles and Override merging.
flowchart TD
subgraph GitRepo["Tracked in Git (Team Standard)"]
Base["compose.yaml (Core + Profiles)"]
Core["Core (no profile): PostgreSQL, Redis"]
Event["Profile event: Kafka, Kafka-UI"]
Debug["Profile debug: Mailpit"]
Base --- Core
Base --- Event
Base --- Debug
end
subgraph LocalOnly["Git-Ignored (Per-Developer)"]
Override["compose.override.yaml"]
Port["Custom ports & query logging"]
Override --- Port
end
Merge{"Compose Deep Merge"}
Base --> Merge
Override --> Merge
CLI["docker compose --profile event up -d"] --> Merge
Merge --> Final["Running: Core + Event + Local Overrides"]
Keep their boundaries strict:
- Profiles (
profiles: [...]) decide which services start. They live incompose.yaml, tracked in Git and shared by the whole team. - Override (
compose.override.yaml) decides how your local machine tweaks those services—alternate ports, SQL statement logging, extra GUI tools, or personal debug flags. It stays out of Git via.gitignore.
When not to use overrides
Don’t use compose.override.yaml to store staging or production configuration. Because Compose auto-loads compose.override.yaml whenever no -f flag is passed, mixing local tweaks with deployment overrides causes accidental leaks. For environment-specific deploys, use explicit files (-f compose.yaml -f compose.prod.yaml).
Starting only what you need with Compose profiles
Any service with a profiles list is skipped during a plain docker compose up unless you explicitly activate that profile via --profile or COMPOSE_PROFILES. Services without a profiles key always start as your core baseline.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# compose.yaml
name: myapp-local
services:
# ==========================================================
# [Core] No profile: always started for every developer
# ==========================================================
postgres:
image: postgres:17-alpine
container_name: local-postgres
restart: unless-stopped
environment:
POSTGRES_DB: myapp
POSTGRES_USER: appuser
POSTGRES_PASSWORD: appsecret
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U appuser -d myapp"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
container_name: local-redis
restart: unless-stopped
ports:
- "6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 5
# ==========================================================
# [Profile: event] Event streaming and message queue work
# ==========================================================
kafka:
image: apache/kafka:3.9.0
container_name: local-kafka
profiles: ["event"]
ports:
- "9092:9092"
environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_LOG_DIRS: /tmp/kraft-combined-logs
kafka-ui:
image: provectuslabs/kafka-ui:latest
container_name: local-kafka-ui
profiles: ["event"]
ports:
- "8989:8080"
environment:
KAFKA_CLUSTERS_0_NAME: local-cluster
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
depends_on:
- kafka
# ==========================================================
# [Profile: debug] Local email capture and inspection
# ==========================================================
mailpit:
image: axllent/mailpit:latest
container_name: local-mailpit
profiles: ["debug"]
ports:
- "8025:8025" # Web UI
- "1025:1025" # SMTP
volumes:
pgdata:
Now every engineer starts only the slice they need:
1
2
3
4
5
6
7
8
9
10
11
# 1. Core API/DB work only (PostgreSQL + Redis, <300 MB RAM, ~2s boot)
docker compose up -d
# 2. Event consumer work (Core + Kafka + Kafka UI)
docker compose --profile event up -d
# 3. Combine multiple profiles
docker compose --profile event --profile debug up -d
# 4. Full-stack integration test (all profiles)
docker compose --profile "*" up -d
If you work inside the same profile all week, set
COMPOSE_PROFILES=event,debugin your local.envfile. A plaindocker compose up -dwill activate those profiles automatically without repeating--profileflags.
Personalizing local config with compose.override.yaml
Profiles solve service selection, but individual developers still run into machine-specific needs:
- Port
5432is already taken by another local PostgreSQL instance. - You want
log_statement=allenabled on PostgreSQL while tuning slow queries. - You want a lightweight browser UI like
pgwebwithout forcing it on the rest of the team.
When you run docker compose without -f, Compose reads compose.yaml and automatically deep-merges compose.override.yaml if it exists in the same directory. Add override files to .gitignore immediately:
# .gitignore
compose.override.yaml
docker-compose.override.yml
.env.local
How Compose merges different YAML types
Knowing the exact merge rules prevents subtle surprises:
| YAML type | Examples | Merge behavior |
|---|---|---|
| Scalar | image, container_name, restart | Overwrites the base value completely |
| Mapping | environment, labels | Merges key by key; matching keys overwrite, new keys append |
| Sequence (List) | ports, volumes | Appends to the base list instead of replacing it |
Watch out for
portslist merging: Because lists append, declaring5432:5432incompose.yamland5433:5432incompose.override.yamlbinds both host ports—it does not remove5432. If you need to replace a host port cleanly, use an environment variable (${POSTGRES_HOST_PORT:-5432}:5432) incompose.yamlor use Compose’s!overrideYAML tag (ports: !override ["127.0.0.1:5433:5432"]) available in Compose v2.24+.
Example compose.override.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# compose.override.yaml (git-ignored, personal local tweaks)
services:
postgres:
# Bind an additional loopback-only port
ports:
- "127.0.0.1:5433:5432"
# Merged with existing POSTGRES_DB / USER / PASSWORD keys
environment:
POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
command: >
postgres
-c log_statement=all
-c log_duration=on
-c logging_collector=on
kafka:
# Shorten local retention to save disk space
environment:
KAFKA_LOG_RETENTION_HOURS: 1
# Personal GUI client not present in the shared compose.yaml
pgweb:
image: sosedoff/pgweb:latest
container_name: local-pgweb
ports:
- "8081:8081"
environment:
DATABASE_URL: "postgres://appuser:appsecret@postgres:5432/myapp?sslmode=disable"
depends_on:
- postgres
With this file in place, I get full SQL query logging, a loopback port on 5433, and pgweb at http://localhost:8081—with zero diff in git status.
Verifying merged output with docker compose config
Once profiles, .env variables, and compose.override.yaml interact, don’t guess what Docker will actually launch. Run docker compose config to inspect the fully resolved YAML without starting any containers:
1
docker compose --profile event config
Trimmed output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
name: myapp-local
services:
kafka:
container_name: local-kafka
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_LOG_RETENTION_HOURS: "1" # <-- merged from override
KAFKA_NODE_ID: "1"
KAFKA_PROCESS_ROLES: broker,controller
image: apache/kafka:3.9.0
ports:
- mode: ingress
target: 9092
published: "9092"
protocol: tcp
postgres:
command:
- postgres
- -c
- log_statement=all # <-- merged from override
- -c
- log_duration=on
container_name: local-postgres
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: appsecret
POSTGRES_USER: appuser
image: postgres:17-alpine
ports:
- mode: ingress
target: 5432
published: "5432"
protocol: tcp
- mode: ingress
target: 5432
host_ip: 127.0.0.1
published: "5433" # <-- appended from override
protocol: tcp
redis:
container_name: local-redis
image: redis:7-alpine
ports:
- mode: ingress
target: 6379
published: "6379"
protocol: tcp
In seconds, you can confirm that mailpit (debug profile) was excluded, kafka (event profile) was included, and the PostgreSQL command and ports merged as intended.
Team workflow, caveats, and checklist
Provide a compose.override.yaml.example
Commit an example override file so teammates don’t have to guess the structure:
1
2
3
cp compose.override.yaml.example compose.override.yaml
# Edit local ports or debug flags as needed
docker compose up -d
Isolate CI/CD from local override files
Passing -f compose.yaml explicitly disables automatic loading of compose.override.yaml. Always use -f in CI pipelines so stray local files can never alter test runs, and pair it with --wait to block until healthchecks pass:
1
2
# CI script: ignores compose.override.yaml and waits for healthy containers
docker compose -f compose.yaml --profile event up -d --wait
Checklist
- Keep core dependencies (
postgres,redis) profile-free and group optional stacks (event,search,debug) under clearprofiles. - Add
compose.override.yamlanddocker-compose.override.ymlto.gitignore, and commitcompose.override.yaml.example. - Remember that list fields (
ports,volumes) append during merges; use.envvariables or!overridewhen you need replacement. - Run
docker compose --profile <name> configwhenever merged behavior looks unexpected. - Pass
-f compose.yamlexplicitly in CI scripts to guarantee deterministic builds.