Secrets

Do Not Bake Secrets into Images

  • Anything in a layer can be extracted, even if a later step deletes it.
  • ENV API_KEY=... and ARG values are visible in docker history / docker inspect.
  • Pass secrets at run time: environment variables from a secret manager, mounted files, or orchestrator secrets.

Build-time Secrets with BuildKit

# Dockerfile: the secret is available only during this RUN and is not stored in any layer
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci

# build command
docker build --secret id=npmrc,src=$HOME/.npmrc -t web:1.0 .

Tip: In the cloud, use the platform's secret store: Google Secret Manager, AWS Secrets Manager, Kubernetes Secrets.