Compose interpolates the values in an env_file too, so a value with a dollar sign in it, such as a generated password, can come out different from what the file says.
The long form of env_file takes a format key:
services:
app:
image: example/app:latest
env_file:
- path: ./app.env
format: raw
With format: raw, Compose reads the key=value pairs without interpolation, so quotes and $ signs go to the container exactly as written. In app.env this stays as it is:
DB_PASSWORD=pa$sw0rd'x
In the compose file itself you need $$ for a literal dollar. Inside a raw env file you write the value once and it arrives once.
Caveat: raw also means no variables. A line like URL=http://${HOST}/api is not expanded either, so keep the default format for files that rely on that. To check what the container really gets, run docker compose run --rm app env, and remember that it prints your secrets to the terminal.
The project’s .env file is a separate job. Compose reads it for substitutions inside the compose file, while env_file entries feed the container.
Further reading: Compose container spec: env_file