Marcio Cunha

Continuous Docker Image Auditing with Vulnerability Scanning on Intermediate Layers

Learn how to implement a rigorous container security auditing strategy, focusing on the inspection of intermediate layers during the continuous integration pipeline.

Marcio Cunha•4 min
Also available in:EspañolPortuguês
Summary
  • Scanning only the final image results in false negatives caused by commands executed and discarded in prior layers.
  • Docker layer caching requires automated strategies to reevaluate old builds against newly discovered CVEs.
  • Static analysis tools integrated into CI/CD block the silent propagation of outdated packages into production environments.
  • Digital artifact signing complements scanning by ensuring that no tampered images are executed in the cluster.
  • Deep visibility into the container lifecycle drastically reduces the mean time to respond to security incidents.

The Silent Challenge of Container Security

When we build a Docker image, we rarely write everything from scratch. The common process uses a base image, installs libraries, compiles code, and packages the final application into a stacked layer structure. However, many known security flaws, known as CVEs or Common Vulnerabilities and Exposures, end up hidden between the lines of this assembly process. If the continuous delivery pipeline inspects only the final build result, critical vulnerabilities introduced and deleted in intermediate steps can go completely unnoticed, creating a false sense of protection.

In practice, this means that removing a sensitive file or updating a package right before closing the image does not erase the file history in previous layers. The Docker file system works like a ledger where each previous page remains recorded and accessible to anyone who knows how to inspect it. To mitigate this risk, we need to change how we view the development pipeline, turning security from a static event at the end of the cycle into a constant audit that accompanies each instruction executed in the container configuration file.

Understanding Layer Architecture and the Hidden Risk

Every command executed in a Dockerfile generates a new immutable layer that overlaps the previous one. Imagine this like building a house where each floor adds new materials, but the debris from previous floors remains stored in the structural corners. If a command downloads a flawed tool and the next command uninstalls it, the corrupted binary can still be extracted by an attacker with access to the command history or the cached intermediate layers in the artifact repository.

This architectural characteristic requires engineering teams to adopt scanning tools capable of opening each of these pages in the digital ledger before the artifact is promoted to sensitive environments. When we automate this scan in the pipeline, we can identify exactly on which line of the Dockerfile the weakness was introduced, allowing the developer to adjust the instruction at the root of the problem instead of trying to remedy symptoms in the final compiled image.

Implementing Scanning in the Continuous Integration Pipeline

To automate this process without stalling the team's delivery pace, the ideal approach is to insert the inspection tool right after the image build stage and before pushing to the central registry. Modern analysis tools can read the container manifest and cross-reference the versions of each installed library with global databases of known vulnerabilities. Below, we present an example script in a pipeline that performs this check and halts execution if critical issues are found.

version: '3.8'
jobs:
  security-scan:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
      
      - name: Build Docker Image
        run: docker build -t minha-aplicacao:${{ github.sha }} .
      
      - name: Run Layer Scan
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: 'minha-aplicacao:${{ github.sha }}'
          severity: 'CRITICAL,HIGH'
          exit-code: '1'

This workflow ensures that no compromised code advances in the production pipeline. If an outdated library is detected, the pipeline fails immediately and generates a detailed report pointing to the exact path for correction, educating the team on secure development best practices with every new release.

Mitigation Strategies and Operational Best Practices

In addition to scanning images at build time, it is essential to establish lifecycle policies for the base images used. Official images often receive silent security patches, meaning a build performed today might be secure but present severe vulnerabilities a month from now if the container keeps running without updates. Continuous auditing implies that scanning occurs not only in the pipeline, but also in the container registries where images are stored waiting for deployment time.

Adopting lean base images, such as specialized distributions or Alpine and distroless versions, drastically reduces the attack surface by eliminating unnecessary system packages and utilities that are often the primary targets of exploitation. Less code running means a lower probability of hidden flaws in intermediate layers, simplifying the audit work and ensuring much more stable and resilient production environments against modern cyber attacks.

Conclusion and Next Steps in Container Security

The security of modern container-based infrastructures demands constant vigilance and rigorous automation across all stages of software development. By extending vulnerability scanning beyond the final image and diving into detailed analysis of each intermediate layer, organizations eliminate dangerous blind spots and ensure technical compliance at scale. Investing in automated tools and ongoing education for engineering teams turns security from an operational burden into a sustainable competitive advantage.