← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:kczv55bb

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.},
}

Markdown

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.