A compose file is something you copy, paste, commit and share. A password inside it will sooner or later end up somewhere it shouldn’t. I’d move every credential out of the file, and Compose gives you three ways to do it. I’ll go from the simplest to the one the Docker docs recommend for sensitive values, and then show how to check that it really worked. You need Docker Compose v2 (the docker compose plugin); Step 2 uses an option from Compose 2.24.0, and secrets only work with Linux containers.
- Time
- 9 min
- Steps
- 11
- Updated
- 19 Nov 2026
You will learn
- A .env file and variables
- Env_file for a whole list of settings
- Secrets as files
- The long syntax and the environment…
- Mind the _FILE convention
Step 1 of 11: A .env file and variables
Compose reads a .env file in the project directory and uses it to fill in ${VARIABLES} in the compose file itself:
# .env
DB_PASSWORD=change-me
services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
The :? form is my favourite detail: if the variable is unset or empty, Compose stops with your error message instead of starting a database with a blank password. ${VAR:-default} is the other form, and it falls back to a default value. Now the compose file is safe to commit, and the password lives in one file.
A few rules about the .env file itself, all from the Compose docs. Compose looks for it in the project directory, normally next to your compose file. Lines starting with # are comments. Unquoted and double-quoted values are interpolated, single-quoted values are taken literally. If you need a literal dollar sign in the compose file, write $$. To use another file, pass docker compose --env-file prod.env up -d; that replaces the default .env, and a missing file is an error.
The :? form has a sibling without the colon. ${VAR?msg} only fails when the variable is not set at all, while ${VAR:?msg} also fails when it is set but empty. For a password I’d always take the colon version, because an empty value is exactly the mistake you want to catch.
Step 2 of 11: Env_file for a whole list of settings
If an app wants many variables, pass a file straight into the container:
services:
app:
image: myapp:latest
env_file:
- app.env
Several files are read in order, and later ones can override earlier ones. From Compose 2.24.0 you can mark a file as optional with required: false, which Compose then ignores when the file is missing. Note the difference: .env is for interpolation in the compose file, while env_file sets the container’s own environment.
The long form looks like this, and it is the one that gives you the optional file:
services:
app:
image: myapp:latest
env_file:
- path: ./app.env
required: true
- path: ./local.env
required: false
Step 3 of 11: Secrets as files
Here is the catch with both steps above: they are environment variables, and the Docker docs say not to use environment variables to pass sensitive information like passwords into your containers. For that, use the secrets element. Compose mounts each secret as a file at /run/secrets/<secret_name> inside the container:
services:
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
secrets:
- db_root_password
secrets:
db_root_password:
file: ./db_root_password.txt
The top-level secrets block says where the value comes from. Besides file: there is environment:, which takes the value from a variable on your machine. The service-level list decides which containers may see it.
The animation shows what happens: the file stays on the host, Compose bind-mounts it as /run/secrets/db_root_password in the one service that asked for it, and the image reads it from there. The value never appears in the container’s environment list.

Step 4 of 11: The long syntax and the environment source
The short list form is enough most of the time. When you need another file name, or want to control who owns the file, use the long syntax:
services:
app:
image: myapp:latest
secrets:
- source: api_token
target: token
uid: "1000"
gid: "1000"
mode: 0440
secrets:
api_token:
environment: API_TOKEN
Here the app finds the value at /run/secrets/token. Per the Compose reference, target is the file name under /run/secrets/ (it defaults to the source name), and the default mode is world-readable, 0444. The catch I’d remember: uid, gid and mode only take effect when the secret comes from environment. For a file: secret they are silently ignored, so set the permissions on the file on the host instead.
The environment: source takes the value from a variable in your shell when you run Compose, which suits CI or a password manager that fills the variable for you. It is a Compose feature, so do not expect it to work with docker stack deploy.
Step 5 of 11: Mind the _FILE convention
The _FILE suffix is not a Compose feature. It is a convention used by some images, such as the official MySQL and Postgres images, where the container reads the password from the path you give instead of from a plain variable. Check the image’s own page on Docker Hub to see which variables support it. If an image has no _FILE option, you can still mount the secret, but the app itself must read the file. That is the trade-off I’d weigh: a plain variable is easy but visible to anyone who can inspect the container, while a file takes a bit more wiring. For a database password or an API key I’d take the file every time, and keep the simple .env route for non-sensitive settings such as a timezone or a port.
Step 6 of 11: Who wins when a variable is set twice
Sooner or later the same name is set in two places. The Compose docs give this order, highest first: docker compose run -e on the command line, then a value interpolated from your shell or an env file, then the environment attribute in the compose file, then env_file, and last an ENV in the image’s Dockerfile. For interpolation of ${VARIABLES} in the compose file itself the order is: your shell first, then a file given with --env-file, then the project’s .env.
The practical result is that a variable exported in your shell silently beats the value in .env. If a compose file seems to ignore your file, this is the first thing I’d check. The cheat sheet below puts the three routes next to each other.

Step 7 of 11: Don’t forget .gitignore
None of this helps if the secret files are committed. Add them before the first commit:
.env
*.env
secrets/
db_root_password.txt
A committed file stays in git history even after you delete it, so if a password was ever pushed, change it. I also like to commit a .env.example with fake values, so you and anyone else know which variables exist. If you share the repo with others, say in the README that they have to copy it to .env and fill in their own values.
Step 8 of 11: Check it
Run docker compose config to see what Compose resolved, then docker compose up -d. Be careful with the config output: it prints interpolated values, so don’t paste it into a forum or chat. These variants help:
docker compose config --environment # values Compose uses for interpolation
docker compose config --no-interpolate # the file with ${VARS} left as they are
docker compose config -q # only validate, print nothing
docker compose exec db ls /run/secrets # the secret file should be listed
Then the real test: docker compose exec db env should not show your password, and docker inspect on the container should not either. If the password does show up there, it travelled as a plain variable. For a secret file, docker compose exec db cat /run/secrets/db_root_password prints the value, so run it on your own screen only.
Step 9 of 11: Troubleshooting
Compose stops with your :? message. That is the guard doing its job. Check that .env sits in the project directory, or pass the right file with --env-file, and look at docker compose config --environment to see what Compose saw.
The container still sees an old password. A shell variable beat your file (see the order above), or the database already initialised its data directory. Official database images read the password variable only on the first start with an empty data directory, so changing it later in the compose file does not change the stored password.
The app reads the file but fails with permission denied. Look at the file’s owner and mode on the host. For file: secrets the uid, gid and mode options do not apply, so the file on the host decides.
An image has no _FILE variable. Then the app must read the path itself, or a small entrypoint script has to export the variable from the file before it starts the app.
A password with a $ in it breaks. Write $$ in the compose file, or use single quotes in .env so the value is not interpolated.
Step 10 of 11: My advice
Use .env for settings that are allowed to leak, put every real credential in a secret file or a _FILE variable, and guard the rest with :? so a forgotten value fails loudly. Add .gitignore before the first commit, not after. This is a habit and no vault: anyone with root on the host can read the files, and for a team or many hosts I would look at a proper secrets manager.
Step 11 of 11: Where to go next
Two related guides of mine: a healthcheck that tells the truth pairs well with a database that needs its password to start, and log rotation keeps container logs from filling the disk, which matters because logs are another place where secrets leak.
Further reading
- Docker Docs: How to use secrets in Docker Compose
- Docker Docs: Set environment variables in Compose
- Docker Docs: Compose file interpolation
- Docker Docs: Environment variables precedence in Compose
- Docker Docs: Interpolation in Compose (variable-interpolation how-to)
- Docker Docs: Compose file reference, services (secrets, env_file)
- On this site: Docker Compose healthchecks, and how to see why a container is unhealthy
- On this site: Make Docker Compose wait until the database is really ready, not just started
- On this site: Replace a cron job with a systemd timer, and see when it runs next
