vibe-audit.lab

guides · 2026-11-14

Secrets in Dockerfiles: Why ARG and ENV Bake Keys Into Your Image Forever

Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team

The short answer

Docker images remember everything: an ARG or ENV carrying a key becomes part of the image's metadata and layer history, extractable with one docker history or a config dump by anyone who can pull the image. Keep secrets out of the image entirely — BuildKit secret mounts for builds, runtime environment injection for running containers — and treat any published image with a baked secret as a key leak.

Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.

Where the value actually lives

Two distinct hiding places, both permanent. ENV values are stored in the image's config JSON — docker inspect prints them in plain text to anyone with the image. ARG values used during build appear in the image history, and files copied in one layer and 'deleted' in the next remain in the earlier layer's tarball: the final filesystem looks clean while the image carries the secret in its archaeology. Vibe-Code Audit's detector family flags Dockerfile ARG and ENV carrying secrets specifically, because AI-generated Dockerfiles — optimised for 'the build passes' — pass API keys as build args constantly, especially for frontend builds that 'need' the key at compile time.

That frontend case deserves its own sentence: if a key is needed to build the client bundle, the key ends up in the bundle too — the image is the second leak of a value that should never have been client-side at all. Fix the architecture (server-side proxy) and both leaks close together.

The correct mechanisms

For build-time secrets, BuildKit secret mounts: docker build --secret id=key,src=... and RUN --mount=type=secret — the value is available during that one build step and recorded nowhere in the image. For runtime secrets, inject via the orchestrator's environment or secret store at container start; the image stays credential-free and the same image can be promoted from staging to production safely. Multi-stage builds help hygiene (build tools and intermediate files stay out of the final stage) but do not rescue a secret copied into the final stage or set with ENV there — the stage boundary is not an eraser for the layers that ship.

CI deserves the same audit: registry credentials and cloud keys passed as build args 'just for the publish step' end up in history exactly like application keys.

What is Baked-in secret?

a credential written into a container image through a Dockerfile ARG, ENV or a copied file in any layer. Image layers are immutable and cumulative: later deletions hide the value from the filesystem view but not from the layer tarball or image config, where it remains readable.

Check your own images today

Run docker history on your production image and read every line; run docker inspect and read the Env array; and use a layer browser (dive or manual tar extraction) on any image that ever contained a config file. Grep the Dockerfile itself for ARG/ENV lines whose names contain KEY, TOKEN, SECRET or PASSWORD — the scanner's patterns do this statically, but the history check catches secrets that entered through copied files and base images. Check base images too: 'someone else's' published image with a baked credential becomes your incident when you build on it.

If the image was pushed anywhere shared

Registries are forever-ish: pulled copies, caches and mirrors persist after you delete a tag. A secret in a pushed image is a leaked secret — rotate it, rebuild clean, and republish; then fix the Dockerfile pattern and add a CI gate (image-history scan or a simple grep of docker history output for your secret patterns) so the next generated Dockerfile cannot reintroduce it silently.

Worked example: PackRoute, a shipping-label API

PackRoute (fictional), generating about 30,000 labels a month, built its image with the carrier API key as a build ARG so the app could 'read it at startup' from an ENV the ARG fed. A scan of the repo flagged both Dockerfile secret patterns. docker history on the published image showed the key's variable name in the build steps, and the config dump printed a related token in plain text. The team rotated the carrier key, rebuilt with BuildKit secret mounts for the one build step that needed a token and runtime injection for the app key, and added a CI check that fails if history or config contains key-shaped strings. The old image tags were deleted from the registry — after rotation, as hygiene, not as the fix.

The five-step fix checklist

  1. Grep every Dockerfile for ARG and ENV lines naming keys, tokens, secrets or passwords.
  2. Inspect your production images: docker history and the config Env array must contain no credential values.
  3. Move build-time secrets to BuildKit secret mounts and runtime secrets to environment/secret-store injection at container start.
  4. Rebuild from multi-stage Dockerfiles so build artefacts and config files never reach the final image.
  5. If a tainted image was pushed to any shared registry, rotate the secret first, then rebuild, republish and delete old tags.

Sources

Frequently asked questions

I deleted the secret in a later Dockerfile layer. Gone?

No. Layers are cumulative and individually extractable; the earlier layer still carries the file. The final filesystem view hides it from the running container, not from anyone inspecting the image.

Are ENV values really readable by anyone?

By anyone who can pull the image, yes — they are in the image config, printed by standard tooling. Private registries narrow the audience; they do not change the mechanism, and images have a way of travelling.

What if the build genuinely needs a key, e.g. a private package registry?

That is exactly what BuildKit secret mounts are for: the credential is mounted for the single RUN step that needs it and is not recorded in any layer or in the history.

Does this apply to docker-compose files committed to the repo?

Compose files with real values are a repo secret leak (same family as .env in git). Reference environment variables or a gitignored env file, and rotate anything that was committed.

Related guides

Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back