How-Tos

How-To: Scan a Container Image for Secrets Before You Push It

Leave a comment

Leaked keys in public Docker Hub images get used in seconds, so the scan has to happen before the push, not after. Here’s the routine I use.

1. Look at what each layer did

docker history --no-trunc myimage:tag

Watch for COPY . ., ARG or ENV lines carrying tokens, and any RUN that writes a credential file. Deleting a file in a later layer does not remove it from the earlier one.

2. Scan the image with TruffleHog

trufflehog docker --image myimage:tag --only-verified

Drop --only-verified for a broader (noisier) sweep. Podman users can do podman save -o img.tar myimage:tag and point TruffleHog at the tarball with --image file://img.tar.

3. Keep secrets out of the build in the first place

  • Use a .dockerignore that excludes .env, .git, keys and cloud config folders.
  • Pass build-time secrets with BuildKit’s --secret mount instead of ARG/ENV.
  • Inject runtime secrets at docker run time, never bake them in.

4. If something already went out

Rotate the key first, then clean up the image. Deleting the tag doesn’t bring the key back.

God Bless, and Happy hacking!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.