Sub topic 11
Replacing or removing inherited values
Earlier chapters showed how Compose layers files. A later file adds to an earlier file, or replaces a value in it. This chapter covers the cases where neither behavior is what you want. Sometimes the override must discard an inherited list and use its own. Sometimes it must delete an inherited setting completely.
This chapter assumes you can read Compose YAML. It also assumes you know how compose.yaml and compose.override.yaml combine, and that you can run docker compose config.
The default merge rules, briefly
When Compose merges two files, it treats each kind of value differently.
| Kind of value | Example attributes | Default merge behavior |
|---|---|---|
| Scalar | `image`, `restart`, `user` | The later file replaces the value. |
| Mapping | `environment` (map form), `labels` | Keys merge. A later value wins when both files set the same key. |
| Sequence | `ports`, `expose`, `dns`, `depends_on` | Entries from both files are combined. |
| Sequence replaced as a whole | `command`, `entrypoint` | The later file replaces the entire value. |
| Keyed sequence | `volumes`, `environment` (list form) | Entries merge by key, such as the container mount path or the variable name. |
The defaults have two gaps:
- You cannot shrink a list. If the base publishes port 8000 and your override should publish only port 9000, a plain override gives you both.
- You cannot delete a setting. An override can set a key to a different value. It cannot make the key disappear.
The YAML tags !override and !reset fill those gaps.
Replacing a list with `!override`
Start with a base file that publishes one port:
# compose.yaml
services:
web:
image: example/web:1.0
ports:
- "8000:8000"A developer wants the app on host port 9000 instead, so they write this override:
# compose.override.yaml
services:
web:
ports:
- "9000:8000"docker compose config shows that both mappings survive:
services:
web:
image: example/web:1.0
ports:
- mode: ingress
target: 8000
published: "8000"
protocol: tcp
- mode: ingress
target: 8000
published: "9000"
protocol: tcpThe container is now reachable on both 8000 and 9000. If your goal was to move the port, this is a bug. If another process already uses 8000 on the host, the stack may fail to start.
Add !override after the key to say "ignore what the earlier files had for this attribute and use only what I write here":
# compose.override.yaml
services:
web:
ports: !override
- "9000:8000"The merged result now contains one port, published 9000 and targeting 8000.
!override works on any attribute, not only lists. It also works on a mapping, such as environment. It discards every inherited key and keeps only the keys in the overriding file. This is useful when a test environment must not inherit any of the base variables.
Removing a value with `!reset`
!reset removes an inherited value as if it had never been set. The usual form is !reset null. For a list you can also write !reset [].
Here is a base file with two settings you may want to remove:
# compose.yaml
services:
web:
image: example/web:1.0
command: ["gunicorn", "app:app"]
environment:
LOG_LEVEL: info
TRACE: "1"
depends_on:
- db
db:
image: postgres:16Now the override:
# compose.override.yaml
services:
web:
command: !reset null
environment:
TRACE: !reset null
depends_on: !reset []The merged result contains three changes:
commandis gone, so the container runs the image's own default command instead ofgunicorn.TRACEis removed fromenvironment, butLOG_LEVELstays.depends_onis empty, sowebno longer waits fordb. A developer who runs the database on the host machine might want exactly this.
You can check the result:
docker compose configThe output has no command line and no TRACE entry, and web has no depends_on block.
Reset versus setting an empty value
Setting a value to an empty string is not the same as removing it. For example, TRACE: "" leaves a variable named TRACE in the container, and the application may read it as "set". TRACE: !reset null removes the variable, so a check such as "is TRACE defined?" returns false.
The same distinction applies to command. An empty override does not reliably restore the image default, but !reset null does.
Choosing between the two tags
| You want to | Use |
|---|---|
| Replace an inherited list or mapping with a new one | `!override` |
| Delete an inherited attribute or key | `!reset` |
| Change a scalar or `command` | A plain override. No tag is needed. |
Putting it together
The next file combines all three behaviors. A plain override for command, !override for ports, and !reset for one environment key all sit in the same service:
# compose.override.yaml
services:
web:
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports: !override
- "9000:8000"
environment:
DEBUG: !reset nullThe following diagram shows how to pick between the behaviors for one attribute:
Gotchas and edge cases
Version support
!override and !reset are Compose v2 features, and older releases do not know them. Check your version before you rely on them:
docker compose versionUse a recent Compose v2 release. Version 2.24.4 or later is a safe baseline. An older release stops with a YAML error about an unknown tag, rather than ignoring the tag silently. The fix is to upgrade, and it should be done on CI runners too, not only on your laptop.
Tags only matter when files are merged
A tag changes how a file combines with earlier files. If you run a file alone, there is nothing to override. A !reset null in a standalone file simply leaves the attribute unset. This means an override file that contains tags works with the base file but may not make sense without it. Document which files belong together.
Order decides what is reset
Files merge in the order you list them. A tag only acts on what came before it. Take this command:
docker compose -f compose.yaml -f compose.ci.yaml -f compose.prod.yaml configA !reset in compose.ci.yaml removes values from compose.yaml. If compose.prod.yaml then sets the same attribute, the value comes back. Reset happens at the point of merging, not at the end.
The tag goes on the key you want to change
Put the tag on the attribute's own line:
services:
web:
ports: !override
- "9000:8000"You cannot put it on the service or on a list item to get the same effect. Tagging web itself would replace or reset the whole service definition, which is rarely what you want.
Removing a whole service
!reset on a service removes the service from the merged model:
services:
adminer: !reset nullThis helps when a base file defines a service that one environment does not need. For optional services you control, a profile is usually cleaner. Reserve !reset for services defined in a file you do not own, such as one pulled in from another team.
Inspect, don't guess
Each tag changes the merged model, so verify the model after every edit. docker compose config runs the merge without starting any containers:
docker compose -f compose.yaml -f compose.override.yaml configCheck that the attribute you reset is absent and that the list you overrode has the entries you expect.
Summary of when to use each tool
- A plain override is enough for scalars,
command, and new mapping keys. !overridereplaces an inherited list or mapping, such asports, when appending would be wrong.!reset nullor!reset []removes an inherited setting, such as an environment variable, acommand, or adepends_onentry.- Always confirm with
docker compose config, and make sure every environment that loads the file runs a Compose version that understands the tags.
Exercise
A base file publishes ports "8000:8000" for the web service. compose.override.yaml contains only ports: ["9000:8000"]. After docker compose up, the app answers on both 8000 and 9000. Change compose.override.yaml so that only 9000 is published.
Show answer
Add the !override tag to the key: ports: !override followed by the list - "9000:8000". By default Compose appends the entries of ports from both files. !override makes it discard the base list and use only the entries in the override file. Confirm the result with docker compose config.
Exercise
The base file sets environment: {LOG_LEVEL: info, TRACE: "1"} for web. In the test environment, the variable TRACE must not exist in the container at all, while LOG_LEVEL stays. Why is TRACE: "" in the test override not enough, and what should you write instead?
Show answer
TRACE: "" leaves a variable named TRACE in the container with an empty value, and code that checks whether it is defined still sees it. Write TRACE: !reset null under environment in the override. That removes only the TRACE key from the merged configuration, and LOG_LEVEL is still inherited.
Exercise
A teammate's CI job fails right after you add depends_on: !reset [] to an override file, with an error about an unknown tag. Their laptop works. What is the likely cause, and how do you confirm it?
Show answer
The CI runner has an older Compose version that does not support the !reset and !override tags, so its YAML parser rejects them. Run docker compose version on the runner to confirm. Fix it by upgrading Compose there, to 2.24.4 or later.