Sub topic 4
The .env File: Values for Interpolation
This chapter assumes you can write a basic compose.yaml, run docker compose up and docker compose down, and set a variable in your shell. It covers the .env file and the role it plays in interpolation.
What the .env file does
Compose reads a file named .env from the project directory. By default, the project directory is the folder that holds your compose.yaml. Compose uses the values in that file when it interpolates the Compose file. Interpolation means replacing ${NAME} placeholders with real values before Compose does anything else.
The .env file feeds the Compose file itself. It does not, on its own, put variables inside a container. A variable appears in a container only if you pass it through environment or env_file, or write it into the Compose file in a way that reaches the container.
# .env (in the project directory, next to compose.yaml)
POSTGRES_USER=app
POSTGRES_PASSWORD=change-me
DB_PORT=5432Each line is NAME=value. Lines that start with # are comments.
Using the values in compose.yaml
Reference the names with ${NAME}. The next file uses the values from .env and passes two of them into the container.
services:
db:
image: postgres:${POSTGRES_TAG:-16}
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set}
ports:
- "${DB_PORT:-5432}:5432"Here is how each placeholder resolves with the .env file above:
| Placeholder | Source | Result |
|---|---|---|
| `${POSTGRES_TAG:-16}` | Not in `.env`, so the default applies | `16` |
| `${POSTGRES_USER}` | `.env` | `app` |
| `${POSTGRES_PASSWORD:?...}` | `.env` | `change-me` |
| `${DB_PORT:-5432}` | `.env` | `5432` |
The :- form supplies a default when a variable is unset or empty. The :? form stops Compose with your error message when the variable is unset or empty. Later chapters cover these forms in full.
The shell wins over .env
A variable set in your shell takes precedence over the same variable in .env. This lets you override a value for one command without editing the file.
DB_PORT=5433 docker compose configThe docker compose config command prints the Compose file with every variable resolved. With the command above, the output shows 5433 as the published port, even though .env says 5432. Use this command whenever you want to confirm what Compose will actually use.
The project directory matters
Compose looks for .env in the project directory, not in your current working directory. If you run docker compose -f deploy/compose.yaml config from another folder, Compose uses the folder holding the Compose file as the project directory. A .env file placed elsewhere is not read, and placeholders may silently become empty strings. The --project-directory option changes the project directory explicitly.
Quick check
Exercise
Your .env file contains DB_PORT=5432. You run DB_PORT=5433 docker compose config. Which port does the output show, and which command would you use to see this without starting any containers?
Show answer
The output shows 5433, because a variable set in the shell takes precedence over the same variable in .env. The command is docker compose config, which prints the resolved Compose file without creating containers.
Exercise
A teammate puts API_KEY=abc123 in .env and expects echo $API_KEY inside the app container to print it. The service has no environment or env_file entries. Why does it print nothing?
Show answer
The .env file only supplies values for interpolation in the Compose file. Without a ${API_KEY} reference in environment (or an env_file entry), nothing passes the variable into the container.