Docker Compose secrets - mount passwords and keys as files in /run/secrets
Answer: Declare each secret under the top-level secrets element with a file or environment source, then list it in the secrets attribute of each service that needs it. Secrets are mounted as a file in /run/secrets/<secret_name> inside the container.
Example
services:
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_root_password
- db_password
myapp:
build:
context: .
secrets:
- npm_token
secrets:
db_password:
file: db_password.txt
db_root_password:
file: db_root_password.txt
npm_token:
environment: NPM_TOKEN
Why not environment variables
Environment variables are often available to all processes, it can be difficult to track access, and they can be printed in logs when debugging. Using secrets mitigates these risks.
Rules
- Services can only access secrets when explicitly granted by a
secretsattribute. Compose grants access on a per-service basis, so standard filesystem permissions control access inside the container. - A
filesource uses the contents of the file at that path; anenvironmentsource uses the value of a host environment variable. It is not supported when deploying withdocker stack deploy; usefileorexternalthere. - When deployed, a secret such as
server-certificateis registered as<project_name>_server-certificate. - Secrets are supported on Linux containers only, because Compose bind-mounts a single file and Windows containers support bind-mounting directories only.
- The
_FILEenvironment variables are a convention used by some images, including Docker Official Images likemysqlandpostgres. - Under
build,secretsmakes a value available at build time, such asnpm_tokentaken fromNPM_TOKEN.
See also
Claims
- id: C1 claim: Secrets are mounted under /run/secrets in the container. source: S1 evidence: "Secrets are mounted as a file in /run/secrets/<secret_name> inside the container."
- id: C2 claim: Compose secrets work on Linux containers only. source: S1 evidence: "Secrets are supported on Linux containers only."
- id: C3 claim: Environment-sourced secrets do not work with docker stack deploy. source: S2 evidence: "environment secrets are not supported when deploying with docker stack deploy"
Sources
- [S1] Manage secrets securely in Docker Compose. Docker docs. https://docs.docker.com/compose/how-tos/use-secrets/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] Secrets top-level element. Docker Compose file reference. https://docs.docker.com/reference/compose-file/secrets/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Manage secrets securely in Docker Compose; Docker Compose file reference, Secrets top-level element. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Manage secrets securely in Docker Compose; Docker Compose file reference, Secrets top-level element. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker Compose secrets - mount passwords and keys as files in /run/secrets." iinventedlight Library, iil:note:kczv55bb, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-compose-secrets/. License: CC-BY-4.0.
@misc{iil_note_kczv55bb,
author = {{iinventedlight}},
title = {{Docker Compose secrets - mount passwords and keys as files in /run/secrets}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-compose-secrets/},
note = {Permanent ID iil:note:kczv55bb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-compose-secrets.md.