Sub topic 13
Passing secrets and configs to services
This chapter assumes you can read a compose.yaml, set environment variables with environment: and .env files, and use named volumes and bind mounts. Here you will pass sensitive values and configuration files to containers without baking them into images or the environment.
Why not just use environment variables?
Environment variables are easy, but they leak easily. Anyone who can run docker inspect sees them. docker compose config prints them after interpolation. Many applications also dump their environment into crash reports and logs, and child processes inherit them.
Compose offers two top-level elements for this:
- Secrets are sensitive values, such as passwords and API keys.
- Configs are non-sensitive files, such as a web server configuration.
Both are defined once at the top level and then granted to individual services. A service sees only the ones you list for it.
Secrets
Define the secret from a local file, then grant it to a service:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txtCompose mounts the file read-only inside the container at /run/secrets/db_password. The password is not in the image or the container's environment. The POSTGRES_PASSWORD_FILE variable holds only the path, and the official Postgres image reads the password from that file.
This depends on the application. It must be able to read a value from a file. Many official images support a _FILE suffix convention. If your own application only reads DB_PASSWORD, change it to read the file at startup.
The long syntax lets you rename the mount point and set ownership and permissions:
services:
web:
image: myapp:latest
secrets:
- source: api_key
target: api-key
mode: 0440
secrets:
api_key:
file: ./secrets/api_key.txtHere the file appears at /run/secrets/api-key. Ownership and mode options are only partly honored by Compose on a single host. Check the result with docker compose exec web ls -l /run/secrets rather than assuming.
Configs
Configs use the same pattern, but they are for content that is safe to share, such as an nginx configuration:
services:
proxy:
image: nginx:1.27
ports:
- "8080:80"
configs:
- source: nginx_conf
target: /etc/nginx/conf.d/default.conf
configs:
nginx_conf:
file: ./nginx/default.confCompared with a bind mount of the same file, a config is declared once and named. It is mounted read-only, and it does not expose a host directory layout to the service. If you omit target, the file is mounted at /<config name>.
Common errors
- A missing source file. Compose fails at
upwith an error naming the file. Paths are relative to the Compose file. - A trailing newline in the secret file. Some editors add one, and applications that compare the value literally then fail. Create the file with
printf 'change-me' > secrets/db_password.txt. - Granting a secret but not declaring it. Every name under a service's
secrets:must exist in the top-levelsecrets:section. - Changing a secret and expecting a live update. Recreate the container with
docker compose up -dafter editing the file.
Exercise
A service lists db_password under secrets:, but the application still fails with "password not set". The application reads the DB_PASSWORD environment variable. What is wrong, and what are two ways to fix it?
Show answer
Compose mounts the secret as a file at /run/secrets/dbpassword. It does not create an environment variable. Fix it by changing the application to read the password from that file, or by using an image or entrypoint that supports a `FILE variable. The same applies to DBPASSWORDFILE` if the application supports it.