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
.dockerignorethat excludes.env,.git, keys and cloud config folders. - Pass build-time secrets with BuildKit’s
--secretmount instead ofARG/ENV. - Inject runtime secrets at
docker runtime, 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!