A database password baked into a Dockerfile lives in the image layer forever. A password passed as -e DB_PASS= shows up in docker inspect, in ps aux, and in any process audit. A password mounted as a tmpfs file at /run/secrets/db_password is visible only inside the container, never on the command line, and never in the image. The difference matters every time someone asks “where did this credential leak from?”
How to verify
Audit a running container for secret hygiene:
# Are credentials on the command line?
docker inspect <container> --format '{{json .Args}}' | jq
docker inspect <container> --format '{{json .Config.Env}}' | jq
# Are credentials in the image's history?
docker history <image> --no-trunc --format "table {{.CreatedBy}}" | grep -iE "pass|secret|token|key"
# What's mounted at /run/secrets?
docker exec <container> ls -la /run/secrets/ 2>/dev/null
docker exec <container> mount | grep secret
# Linting your own files
trivy config Dockerfile compose.yaml
If Env contains anything resembling a password and not the name of a secret file, that secret is one docker inspect away from being leaked to anyone with daemon access.
What’s happening
Docker offers three primary places to put a secret, in order of increasing safety:
- Environment variables (
-e,environment:in Compose). Visible to anyone who candocker inspectthe container. Persisted in process listings if your app forks. Easy and obvious, which is why everyone starts here and stays too long. - Files mounted as tmpfs (Docker secrets, Compose
secrets:). The secret material lives in a tmpfs inside the container at/run/secrets/<name>. Not in env, not indocker inspect, not on disk on the host. The standard pattern. - External secret stores (Vault, AWS Secrets Manager, GCP Secret Manager, etc.) fetched at startup or via a sidecar. The Docker layer never sees the secret material; the application code reads it via API at startup. The right answer when secrets need rotation, audit trail, or fine-grained access.
For most single-node Compose deployments, option 2 (file-based) is the right altitude. Option 3 is for fleets and regulated environments.
The procedure
- The simplest version — a Compose file-backed secret:
# compose.yaml
services:
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
secrets:
db_password:
file: /etc/myapp/secrets/db_password
The host file /etc/myapp/secrets/db_password contains exactly the password, no newline at end, owned by root and 0400. Compose mounts it into the container at /run/secrets/db_password. Postgres’ POSTGRES_PASSWORD_FILE env var tells the image to read the password from that file.
# Set up the host secret with the right permissions
sudo install -d -m 0700 /etc/myapp/secrets
sudo bash -c 'printf "%s" "$(openssl rand -base64 32)" > /etc/myapp/secrets/db_password'
sudo chmod 0400 /etc/myapp/secrets/db_password
sudo chown root:root /etc/myapp/secrets/db_password
Use printf "%s" not echo — echo adds a trailing newline that Postgres will treat as part of the password.
- For applications that don’t read
_FILEenv vars natively, write an entrypoint shim:
#!/bin/sh
# entrypoint.sh — reads /run/secrets/* and exports as env at runtime
for f in /run/secrets/*; do
[ -f "$f" ] || continue
name=$(basename "$f" | tr '[:lower:]' '[:upper:]')
export "$name=$(cat "$f")"
done
exec "$@"
The secret reaches the process via env, but it never appeared on the command line, never in docker inspect, never in the image. unset after use if your application supports it.
- For Swarm or Kubernetes, the native secrets API is the right answer. With Swarm:
# Create a secret from stdin
printf "%s" "$(openssl rand -base64 32)" | docker secret create db_password -
# Attach in the service definition
docker service create --name db \
--secret db_password \
--env POSTGRES_PASSWORD_FILE=/run/secrets/db_password \
postgres:16
docker secret ls lists them. docker secret rm removes them. Swarm stores them in the Raft log, encrypted on disk if you’ve enabled autolock.
- For external secret managers, the pattern is:
- A startup script (or sidecar) authenticates to Vault/AWS/etc. via the host’s IAM role or a workload identity.
- It fetches the secret and writes it to a tmpfs path the app reads from.
- The secret never lives in the orchestrator’s config.
# Example with AWS Secrets Manager and IAM role
aws secretsmanager get-secret-value \
--secret-id prod/myapp/db_password \
--query SecretString --output text \
> /run/secrets/db_password
chmod 0400 /run/secrets/db_password
exec "$@"
- Build secrets — secrets needed only during
docker build. Use BuildKit’s--mount=type=secret:
# syntax=docker/dockerfile:1.7
FROM ubuntu:24.04
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
npm install
docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp .
The secret is available during the RUN step but does not persist in any image layer. Standard pattern for private registry credentials and signing keys.
- Rotate secrets on a schedule. The pattern:
- Generate a new secret value.
- Update the secrets store (file, Vault, Swarm secret).
- Roll the dependent services so they pick up the new value.
- Verify, then revoke the old credential in the upstream system.
Common pitfalls
- Committing
.envfiles. They contain secrets, they belong in.gitignore, and a leaked .env is the most common source of credential breaches we see. - Using
ARGfor secrets in Dockerfiles —ARGvalues are visible indocker history. Use--mount=type=secretfor build-time secrets. - Logging the contents of env vars at startup (“DB_PASSWORD=hunter2”). Log the existence of the secret, not its value.
- Sharing the same secret across environments. Rotate independently per environment, and audit which environment a credential comes from when responding to a leak.
- Putting secrets in image labels or in
docker compose configoutput. Both are visible without container access. - Treating
127.0.0.1binding as a security boundary that obviates secrets management — anyone on the host or in another container with host networking can still reach the service.
In the engagements we run, every secret has a known store, an owner, and a rotation cadence. The managed operations baseline includes the audit job that flags any container running with a literal password in Env.