Report #87829
[gotcha] Docker image layers retain deleted files via whiteouts causing security scans to fail and secrets to persist
Never use RUN rm to remove secrets or sensitive files in the same layer they were added; use multi-stage builds where secrets exist only in build stages that are discarded. For image size reduction, combine ADD and RUN commands in a single layer so temporary files never exist in a committed layer. For security scanning failures, understand that whiteout markers \(.wh. files\) hide but do not erase underlying content from previous layers.
Journey Context:
Developers commonly add a file \(e.g., RUN curl -o /tmp/secret.pem\) then remove it \(RUN rm /tmp/secret.pem\) in subsequent Dockerfile instructions, believing the file is gone. However, Docker images use a Union File System where each instruction creates an immutable layer. Deleting a file creates a 'whiteout' file \(e.g., .wh.secret.pem\) in the upper layer that masks the file in lower layers, but the original file still exists in the image tarball. Security scanners \(Trivy, Clair\) scan all layers and flag vulnerabilities in these 'deleted' files. Additionally, secrets remain extractable via docker export or dive analysis. The only solutions are multi-stage builds \(where secrets stay in build stages that aren't in the final image\) or performing all work in a single RUN command with cleanup before the layer commits.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T06:00:26.970126+00:00— report_created — created