Sub topic 11
Replacing or removing inherited values
This chapter assumes you know how Compose merges multiple files (scalars replaced, mappings merged key by key, some lists appended) and that you can run docker compose config. Here you will learn how to change those defaults when merging does the wrong thing.
When the default merge gets in the way
Merging is convenient until an override needs to discard something from the base file. Two cases come up often:
- The base file publishes a port, and the development file needs a different one instead of an extra one.
- The base file sets a variable that a particular environment must not have at all.
Compose provides two YAML tags for these cases: !override and !reset. These tags need a Compose version that supports them. Check with docker compose version and update if the tags are rejected.
Default behavior first
Take the base file from earlier in this tutorial. The web service publishes 8000:8000, and the override file adds a port.
# compose.override.yaml
services:
web:
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
ports:
- "5678:5678"After merging, web has both ports, 8000:8000 and 5678:5678, because ports is a list that is appended. The command value is simply replaced, since it is a single value.
| Attribute type | Example | Default merge result |
|---|---|---|
| Scalar or command | `command`, `image` | Later file replaces the earlier value |
| Mapping | `environment` | Keys are merged, later file wins on a conflict |
| Sequence such as ports | `ports` | Items are appended |
Replacing a whole list with !override
Put !override after the key to throw away the inherited value and use only what the current file says.
# compose.override.yaml
services:
web:
ports: !override
- "9000:8000"The merged web service publishes only 9000:8000. The base 8000:8000 is gone. Without the tag, both would be published, and a port conflict is possible if two services claim the same host port.
Removing a value with !reset
Use !reset to return an attribute to its empty state, as if the base file had never set it.
# compose.override.yaml
services:
web:
environment:
LOG_LEVEL: !reset nullAfter the merge, LOG_LEVEL is no longer set for web. This differs from setting it to an empty string, which would still define the variable with an empty value. Services and applications often treat those two states differently.
You can also reset a whole attribute. This is useful when the base file declares depends_on and a test file runs the service on its own:
services:
web:
depends_on: !reset []Checking the outcome
Do not trust the tags by reading the files alone. Print the merged result and look at the service you changed.
docker compose -f compose.yaml -f compose.override.yaml configIn the output, confirm that ports contains only the entries you expect and that a reset variable no longer appears under environment.
Common mistakes
- Using
!overrideon a mapping such asenvironmentand losing variables you wanted to keep. Replace only what you mean to. - Writing
!reset ""when you want the value gone. Usenullfor scalars and[]for lists. - Running an older Compose that fails to parse the tags. The error mentions an unknown tag, so update Compose.
Exercise
Your base file publishes "8000:8000" for web. A development override must publish only "9000:8000". Which tag do you put on ports, and why does leaving it off cause a problem?
Show answer
Write ports: !override followed by - "9000:8000". Without the tag, ports is a list that is appended, so web would publish both 8000:8000 and 9000:8000. Confirm the final list with docker compose config.