no-secrets-in-env
ENV and ARG values are baked into Docker image layers and readable with docker history. Where to put API keys and passwords instead.
On this page
ENV API_KEY=... feels like configuration, but it is storage: the value is written into the image's metadata and distributed with every push and pull. ARG is no safer — build arguments are recorded in the layer history. Anyone who can pull the image can read them back with docker history --no-trunc or docker inspect. This rule flags ENV and ARG keys that look like credentials (PASSWORD, SECRET, TOKEN, API_KEY, …) with literal values. A URL without embedded credentials (AUTH_URL=http://auth:8080) names an endpoint, not a secret, so the rule skips it. The rule also skips a key whose last word describes a credential without holding it: where it lives (file, path, dir, sock, socket), how long it lasts or how long it is (ttl, timeout, seconds, minutes, hours, days, expire, expires, expiry, expiration, expires_in, length), how many or how large (limit, budget, count, size), which one it is or which scheme guards it (name, type, mode, driver, method, policy, provider, algorithm), or whether it is on (enabled, disabled). POSTGRES_PASSWORD_FILE=/run/secrets/pg, SSH_AUTH_SOCK, TOKEN_TTL_SECONDS, ACCESS_TOKEN_EXPIRE_MINUTES, TOKEN_LIMIT and POSTGRES_HOST_AUTH_METHOD are not reported. A key that starts with MAX_ or MIN_ names a bound, so MAX_TOKEN is not reported either. A key that ends in a word that can hold the credential itself, such as AUTH_HEADER, PASSWORD_HASH, SECRET_KEY or API_TOKEN_ID, is still reported. The rule skips on/off values too: true, false, yes, no, on, off, 0 and 1, as in MYSQL_RANDOM_ROOT_PASSWORD=1. A key that names a credential by its role or provider is reported too, such as OPENAI_KEY, LITELLM_MASTER_KEY or N8N_ENCRYPTION_KEY. The _KEY names covered start with a word such as master, encryption, signing, access, service, admin, license, hmac, jwt or a model provider. The names PRIMARY_KEY, PUBLIC_KEY and AWS_ACCESS_KEY_ID are not enough to report a value. A value that has the shape of a provider token is reported under any key name, those included. A value with no digit, no long unbroken run of characters, or the EXAMPLE suffix of the AWS sample ID is a placeholder and is not treated as a token. The shapes are sk-, hf_, the GitHub prefixes ghp_, gho_, ghu_, ghs_ and ghr_, github_pat_, and AWS access key ids (AKIA, ASIA), each followed by a long literal run of characters. A report names the key and never the value. A URL value that carries a literal password in its user part, such as ENV DATABASE_URL=postgres://app:hunter2@db/app, is reported under any key name, and so is a development password like postgres://postgres:postgres@db. A URL with only a user name (ssh://git@host), an empty password, a password that names a variable or a template slot ($PW, #{pw}, {{pw}}, %s), and a key that ends in _FILE are not reported. The rule reads ENV and ARG values only, not RUN, CMD, HEALTHCHECK or LABEL.
What the rule catches
FROM node:22-slim
ENV DATABASE_PASSWORD=sup3rs3cret
ARG NPM_TOKEN=npm_abc123Scanning this file reports:
✖ ERROR [docker-doctor/no-secrets-in-env]
Potential secret found in ENV: 'DATABASE_PASSWORD'. Secrets baked into images can be extracted easily by anyone with image access.Why it matters
An image is an artifact, not a runtime. It gets pushed to registries, cached on CI runners, copied to laptops, and mirrored — every copy carries the secret, forever, in plain text. Leaked registry credentials are one of the most common causes of cloud compromises precisely because images travel further than the people who build them expect. A secret that reaches an image layer must be treated as leaked and rotated.
How to fix it
Use Docker Secrets, build arguments passed at runtime, or environment variables at runtime instead of baking them into the image.
FROM node:22-slim
# Needed at build time? Use a BuildKit secret mount — never a layer:
RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ciPick the mechanism by when the secret is needed:
- Run time (database passwords, API keys): pass at startup —
docker run -e, a Composeenv_file, or your orchestrator's secret store (Docker/Kubernetes secrets). None of these touch the image. - Build time (private registry tokens): use a BuildKit secret mount as above — the file is available during that single
RUNand leaves no trace in any layer.
If a real secret has already been committed into an image, rotate it. Deleting the image or the ENV line later does not un-publish the layers.
Rule details
- Rule key —
docker-doctor/no-secrets-in-env - Category — Security
- Default severity —
error - Applies to — Dockerfiles
Explain this rule from the CLI:
npx @docker-doctor/cli@latest rules explain docker-doctor/no-secrets-in-envChange its severity — or turn it off — in your config file:
// docker-doctor.config.ts
export default {
rules: {
"docker-doctor/no-secrets-in-env": "off",
},
};Severity affects the health score: error findings cost more points than warning, and info costs the least.