Docker Compose Secrets: Stop Passing Passwords as Environment Variables

A Compose file often starts harmlessly: one database, one application container, and one sensitive value copied into an environment block just to get the stack booting. Six months later, that same file defines backups, background workers, and a metrics sidecar, creating several places where the credential can leak into logs or shell history. What starts as a reasonable shortcut for a toy service ages poorly.

The practical question is not whether every small Docker stack requires a dedicated vault. Rather, when a Compose service needs a database login or API credential, should that value sit in the container environment, or be mounted as a secret file granted strictly to the services that read it? For new Compose projects, Docker’s documentation recommends secrets over plain environment variables for sensitive data.

On this page
  1. Why environment variables are a poor place for credentials
  2. How Compose secrets reach only the services you grant
  3. A safer migration pattern for a database-backed stack
  4. Where Compose secrets stop helping
  5. Make rotation boring, not heroic
  6. Run this audit before editing a live Compose stack

Why environment variables are a poor place for credentials#

Docker’s Compose environment-variable documentation gives a direct warning: do not use environment variables to pass sensitive information such as passwords into containers; use secrets instead. The same page explains that environment and env_file both set values inside the container environment, even if they make the Compose file cleaner.

That distinction matters. Moving a database login from the main Compose file into a separate environment file may reduce visual clutter, but the value still becomes part of the container environment. Docker’s Compose secrets guide calls out the risk: environment variables are often available to all processes, can be hard to track, and may be printed during debugging without you noticing.

Environment variables are still fine for non-sensitive configuration: feature flags, hostnames, log levels, cache sizes, and similar values. The boundary is secrecy. If disclosure would require rotation, notification, or incident response, treat it as a secret rather than a setting.

How Compose secrets reach only the services you grant#

Compose secrets use two parts. First, a top-level secrets section defines where the value comes from. Second, each service that needs the value lists that secret under its own secrets attribute. Docker’s guide says Compose mounts the secret in the container at /run/secrets/<secret_name>, and that services only receive secrets when explicitly granted.

The Compose file secrets reference documents two source types: file, where the secret is created from a file’s contents, and environment, where the secret is created from a host environment variable. It also notes an important portability limit: environment-sourced secrets are supported by Docker Compose, not by docker stack deploy.

One platform constraint matters before you adopt this pattern: Docker’s secrets guide notes that Compose secrets work on Linux containers only. Compose delivers each secret by bind-mounting a single file, whereas Windows containers only support bind-mounting directories. For Linux-based homelab and small production deployments, however, file-mounted secrets cleanly separate sensitive credentials from the standard process environment.

A safer migration pattern for a database-backed stack#

The weak pattern is not complicated: a service mixes ordinary settings with sensitive settings in the same environment block. A safer first step is to leave harmless configuration in the environment and grant the sensitive value as a separate secret file.

services:
  api:
    image: example/webapp
    environment:
      APP_MODE: production
      DATABASE_HOST: db
    secrets:
      - app_database_login

secrets:
  app_database_login:
    file: ./secrets/app_database_login.txt

This configuration does not rely on magic variable names inside your code. Certain Docker Official Images—Docker’s guide highlights MySQL and Postgres—accept `_FILE` environment variables pointing to the mounted path. For custom applications, build explicit file-reading logic and pass the path through an ordinary setting or a default configuration location.

The source file still needs careful handling: keep it outside version control, restrict local file permissions, back it up only in encrypted stores, and avoid copying it into issue trackers or chat. If the old value ever lived in Git, treat the work as a rotation, not a cosmetic cleanup. The related Orthogonal guide on catching leaked secrets before commit is the right companion task.

Where Compose secrets stop helping#

Compose secrets reduce one specific class of exposure: sensitive values sitting in environment variables for every process in a container to inherit or print. They do not replace a secret-management service with policy, approvals, history, and automatic rotation. OWASP’s Secrets Management Cheat Sheet recommends least privilege, automation where possible, rotation, revocation, expiration, and audit records for secret access.

Compose secrets act as a local delivery mechanism rather than a complete lifecycle manager. They work well when running a few containers on a single host where you want tighter isolation than environment variables allow. They fall short when multiple teams require delegated access, when compliance demands per-read audit logs, or when short-lived credentials must be minted dynamically per session.

Watch out for over-shared secrets as well. Mounting the same database credential into a web app, a worker, a migration job, and a backup container puts all four services inside the blast radius. Because Compose requires explicit per-service grants, your configuration file exposes that access list directly in code—making it easier to prune services that no longer need the credential.

Make rotation boring, not heroic#

The hardest part of secret cleanup is usually not writing the YAML. It is agreeing on what happens when the value changes. OWASP describes a secret lifecycle that includes creation, rotation, revocation, and expiration, and it calls for audit records such as who requested a secret, what system or role used it, when it expired, and whether there were authorization errors.

For a Compose stack, turn that into a small runbook. Generate the new value outside the repository. Place it in the secret source file or the upstream secret store that writes that file. Restart only the services that need the value during a planned window. Revoke the old credential at the database, API provider, or identity system. Record the date, the affected services, and the place where the old value was removed.

That runbook should sit next to your deployment notes, not inside the secret file. Pair it with image and dependency scanning as a separate control; the Orthogonal post on running Trivy across homelab containers covers that adjacent cleanup path.

Run this audit before editing a live Compose stack#

Start small. Open one Compose project and review the service environment and env_file entries for values whose disclosure would force rotation. Leave non-secret configuration alone. Pick one credential that has a clear owner and a known rotation path.

Extract that single credential into a top-level Compose secret, grant access strictly to the consuming service, and point the application or image configuration at the mounted file path. Immediately rotate the credential so the retired environment-variable value stops working. Migrate one service at a time; a controlled, verified change beats a sweeping edit where nobody knows which credential is live.