Three things you must never do
ENV API_KEY=...in a Dockerfile — bakes the secret into a layer of the image. Anyone with the image has the key.- Hard-coded
environment:in compose.yaml committed to git — the secret is now in your repo history forever. - Pass secrets via
--build-argfor runtime use — ARG values are visible in the build history of the image.
Three things you should do
- Local dev: a
.envfile referenced by compose, with.envin.gitignore. - CI/CD: secrets injected by the pipeline (GitHub Actions secrets, GitLab CI variables) into the runtime environment, never into the image.
- Production: a real secrets manager (AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault, sealed-secrets in k8s) — secrets fetched at startup, never written to disk.