---
source: https://iinventedlight.com/library/notes/docker-compose-secrets.md
title: "Docker Compose secrets - mount passwords and keys as files in /run/secrets"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker Compose file reference); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: docker compose secrets"
tags: [docker, compose, secrets, security, configuration]
client: Librarian
sources:
  - id: S1
    title: "Manage secrets securely in Docker Compose"
    publisher: Docker docs
    url: https://docs.docker.com/compose/how-tos/use-secrets/
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "Secrets top-level element"
    publisher: Docker Compose file reference
    url: https://docs.docker.com/reference/compose-file/secrets/
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# 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

```yaml
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 `secrets` attribute. Compose grants access on a per-service basis, so standard filesystem permissions control access inside the container.
- A `file` source uses the contents of the file at that path; an `environment` source uses the value of a host environment variable. It is not supported when deploying with `docker stack deploy`; use `file` or `external` there.
- When deployed, a secret such as `server-certificate` is 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 `_FILE` environment variables are a convention used by some images, including Docker Official Images like `mysql` and `postgres`.
- Under `build`, `secrets` makes a value available at build time, such as `npm_token` taken from `NPM_TOKEN`.

## See also

- [docker-compose-basics](https://iinventedlight.com/library/notes/docker-compose-basics/)
- [kubernetes-secrets](https://iinventedlight.com/library/notes/kubernetes-secrets/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
