Sub topic 15
Troubleshooting: Blank Values, Unexpected Overrides, and Warnings
This chapter assumes you know the basics of compose.yaml, the difference between interpolation and container environment, and the precedence rules from earlier chapters. It covers the three problems you will meet most often and a repeatable way to diagnose them.
Start with docker compose config
Most variable problems show up in the resolved file. Run this before changing anything:
docker compose configIf a value is wrong here, the problem is in interpolation. If it is right here but wrong inside the container, the problem is in the container environment. You can check the container side with docker compose run --rm app env.
Blank values and the "variable is not set" warning
When Compose finds ${DB_PORT} and no source defines it, it substitutes an empty string and prints a warning:
WARN[0000] The "DB_PORT" variable is not set. Defaulting to a blank string.Common causes:
- The
.envfile is not in the project directory. Compose looks next tocompose.yaml, not in your current directory if you used-fwith a path elsewhere. - The name is misspelled or has different case. Names are case-sensitive.
- You used
--env-file, which replaces the default.envinstead of adding to it. - You put the value in a file listed under
env_file. That file feeds the container, not interpolation.
To turn a silent blank into a hard failure, use ${VAR:?message}. To supply a fallback, use ${VAR:-default}.
Unexpected overrides
If a value differs from what your .env says, a higher-precedence source is winning. Check these in order:
- A variable exported in your shell beats
.env. Runenv | grep DB_PORTto see it. - An
environmententry beats the same name inenv_file. - A variable in your shell session from an earlier
exportcan persist for days without you noticing.
You can prove a shell override with a one-off test:
env -u DB_PORT docker compose configThe env -u command removes DB_PORT from the environment for that single run. If the output changes, the shell was the source.
Dollar signs and other surprises
A $ in a value is read as the start of interpolation. A password such as pa$word is partly replaced or produces a warning. Write pa$$word in the Compose file. In a .env file, single quotes keep the value literal:
DB_PASSWORD='pa$word'Other frequent surprises:
| Symptom | Likely cause | Fix |
|---|---|---|
| Value contains quotes | Quotes inside an `env_file` are kept literally | Remove them in `env_file` files |
| Inline comment appears in value | A `#` without a leading space is part of the value | Put a space before `#` |
| Variable empty in container only | The name is set in `.env` but never listed under `environment` | Add it to `environment` or `env_file` |
| Changes ignored | Container was not recreated | Run `docker compose up -d` again |
A short checklist
- Run
docker compose configand read the value. - Check the shell with
env. - Confirm which env file Compose used.
- Add
:?to variables that must never be blank.
Exercise
docker compose config shows ports: "5432:5432", but your .env contains DB_PORT=5433 and the Compose file uses ${DB_PORT:-5432}. Name two likely causes and one command to find out.
Show answer
Likely causes are that .env is not in the project directory, or that DB_PORT is not read because --env-file replaced it. Another is that DB_PORT is set to an empty string in the shell, which :- also treats as unset. Run docker compose config again with env -u DB_PORT in front, or check which file Compose uses, to see whether the value changes.