Skip to content

Environment variables and secrets in Compose

Select an icon to see what it is.

Step 4 of 6

A secret is a file, mounted read-only

With secrets: and file:, Compose adds a read-only bind mount of the host file at /run/secrets/<name> inside the container. The value is never put in Env. db is given the path instead, in POSTGRES_PASSWORD_FILE.

POST /v1.45/containers/create?name=shop-db-1 (a bind mount; the exact fields can differ between Compose versions)

{
"Env": [
"POSTGRES_PASSWORD_FILE=/run/secrets/db_password"
]
"HostConfig": {
"Mounts": [
{
"Type": "bind"
"Source": "<project dir>/db_password.txt"
"Target": "/run/secrets/db_password"
"ReadOnly": true
}
]
}
}