Safeguard
Security Guides•

Should you scan the Dockerfile or the built image?

Scan both. The Dockerfile shows how an image is built and the built image shows what it contains. Each finds problems the other cannot. Table and steps.

Safeguard Security Team
3 min read

Scan both. A Dockerfile scan reads the build instructions and catches risky habits such as running as root or baking secrets into layers. An image scan reads the finished contents and finds vulnerable packages. Neither can see what the other sees.

What each one sees

CheckDockerfile scanImage scan
Runs as rootYesSometimes, from image config
Secret passed as build argument or envYesYes, in layers and metadata
Vulnerable OS packageNo, only the base nameYes
Vulnerable app dependencyNoYes, if the manifest or lockfile is in the image
Unpinned base tagYesNo
Files left in a layerNoYes

Why layers matter

Docker describes an image as a stack of immutable layers, each recording filesystem changes such as additions, deletions or modifications. Because earlier layers cannot be changed, deleting a file in a later step marks it removed in the stack, but the earlier layer still holds it. This is an inference from immutability rather than a quote, and it is why a secret removed by a later step can still be found in the image.

Secrets in builds

Docker's documentation says build arguments and environment variables persist in the final image, so anyone with the image can read them. Secret mounts expose the value only while a single build step runs:

RUN --mount=type=secret,id=aws     AWS_SHARED_CREDENTIALS_FILE=/run/secrets/aws     aws s3 cp ...

A Dockerfile scan can flag the unsafe pattern. An image scan can confirm nothing leaked.

Steps

  1. Lint the Dockerfile in the pull request.
  2. Build the image in CI.
  3. Scan the built image before it is pushed.
  4. Scan again on a schedule, since new advisories appear after you ship.
  5. Rebuild from a patched base when the base image is the source of findings.

How Safeguard helps

Safeguard covers SBOM and dependency scanning, reachability analysis, and policies and gates that apply in pull requests. The free scan is a quick way to see your dependency findings.

Try it yourself

Click through it at your own pace, or open it full screen.

Look at which findings come from the base layer and which from the application layer.

Quick check

Can an image scan replace Dockerfile review?

No. It sees contents, not intent. Build habits like unpinned tags are only visible in the Dockerfile.

Is a secret deleted in a later step gone from the image?

Not necessarily. The earlier layer is immutable and may still contain it.

Sources

  • Docker documentation, Understanding image layers (fetched 19 October 2026).
  • Docker documentation, Build secrets (fetched 19 October 2026).

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.