Lesson 5 of 6 Security and Compliance Scanning

Automating Container Scanning and CodeQL in a Container

This lesson shows how to scan images, and how to run CodeQL analysis in a container.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what container scanning examines.
  • Scan an image in a pipeline.
  • Describe registry and runtime scanning.
  • Run CodeQL in a job container.

What container scanning examines

An image contains layers: a base image with operating system packages, followed by runtime libraries and application code. A container scanner lists the packages in each layer, and compares them with vulnerability databases. Many findings come from the base image, not from your code, so using a smaller base, such as a distroless or Azure Linux image, and rebuilding regularly, removes a large part of them.

Scanning in a pipeline should happen after the image is built, and before it is pushed or promoted, so that a bad image does not reach a registry that production pulls from. Registries should also be scanned continuously, because a new vulnerability can affect an image that was clean when it was built.

True or false

Container scanning basics

Exam skills 4.3.5

Check where container vulnerabilities come from and when to scan.

Log in to try this activity Interactive activities are free with an account.

Scanning images in a pipeline

Trivy is a widely used open-source scanner. The following workflow builds an image, fails on critical or high findings, and sends results to the Security tab as SARIF.

jobs:
  image-scan:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      security-events: write
    steps:
      - uses: actions/checkout@v4

      - name: Build the image
        run: docker build -t web:${{ github.sha }} .

      - name: Scan the image
        uses: aquasecurity/trivy-action@57a97c7e7821a5776cebc9bb87c984fa69cba8f1   # v0.35.0
        with:
          image-ref: web:${{ github.sha }}
          format: sarif
          output: trivy.sarif
          severity: CRITICAL,HIGH
          limit-severities-for-sarif: true
          exit-code: '1'

      - name: Upload results
        if: ${{ !cancelled() }}
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: trivy.sarif

exit-code: '1' fails the job on findings, and the if condition uploads results even so. limit-severities-for-sarif applies the severity filter to SARIF output, which otherwise includes all severities. The action is pinned to a full commit SHA, for the reason given in the lesson on preventing leakage. The same approach works in Azure Pipelines through a script step, and through Microsoft Security DevOps, whose containers category runs Trivy. The Evaluate artifact check can then enforce image policies at the environment level.

The following diagram shows the two points at which container images are scanned.

Two points of container scanning. A pipeline scan before the push fails on critical findings, so new bad images never reach Azure Container Registry; Defender for Containers reassesses stored images as new vulnerabilities are published. Either alone leaves a gap.
The scenario is illustrative.
Spot the bug

A Trivy scan that hides its results

Exam skills 4.3.5

Find the lines that stop this image scan from reporting correctly.

Log in to try this activity Interactive activities are free with an account.

Scanning in the registry and at run time

Microsoft Defender for Containers scans images in Azure Container Registry, and it can assess running containers in AKS, with findings in Defender for Cloud. Defender for Cloud can also use a CI/CD integration to link scan results with registry images, which supports the code-to-cloud view. Azure Policy can restrict AKS to images from approved registries, which gives a final check at deployment.

Image signing strengthens this chain. Notary Project signatures, verified at deployment by Ratify in AKS, prove that an image came from your pipeline and has not been changed. Docker Content Trust in Azure Container Registry is deprecated, so new designs should use Notary Project signing.

Scenario questions

Registry, runtime and signing

Exam skills 4.3.5

Choose the control for each container security requirement.

Log in to try this activity Interactive activities are free with an account.

Running CodeQL in a container

The skills measured name a specific scenario: configuring an action to run CodeQL analysis in a container. This is needed when a project builds only with a specific toolchain, such as a particular compiler version, which a normal hosted runner does not provide. The job's container key runs all its steps inside that image, and the CodeQL action works as usual.

name: codeql-in-container
on:
  push:
    branches: [main]

jobs:
  analyze:
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/dotnet/sdk:9.0
    permissions:
      contents: read
      security-events: write
    steps:
      - name: Install required tools
        run: apt-get update && apt-get install -y git

      - uses: actions/checkout@v4

      - uses: github/codeql-action/init@v3
        with:
          languages: csharp
          build-mode: manual

      - name: Build
        run: dotnet build --configuration Release

      - uses: github/codeql-action/analyze@v3

Several details matter. The image needs what CodeQL requires, including Git and the language's build tools. security-events: write is required for uploading results. Self-hosted runners need Docker available to use this feature, and the image should be pinned for reproducibility.

True or false

CodeQL in a container

Exam skills 4.3.5

Check the requirements for running CodeQL analysis inside a job container.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing the point of scanning

A team pushes images to Azure Container Registry and deploys to AKS. Security requires that images with critical vulnerabilities must not reach the registry, and that images already stored must be reassessed as new vulnerabilities are published. What should you configure?

Add a container scan to the pipeline before the push, failing on critical findings, which prevents new bad images from reaching the registry. Enable Defender for Containers for ongoing assessment of stored images. A pipeline scan alone would never revisit an old image, and registry scanning alone would find an issue only after the push.

Check yourself

Why might a CodeQL job in a container need to install Git first?

Show answer

actions/checkout and CodeQL need Git, and a minimal image may not include it.

Exam Tips

Remember for the exam

  • Scan after building and before pushing; rescan stored images continuously.
  • Smaller base images remove many findings.
  • Trivy can output SARIF for upload to the Security tab.
  • Run CodeQL in a container with the job-level container key.
  • Defender for Containers assesses registry images and running containers.