Stay Updated with the Latest in Cybersecurity & Tech. Explore Anosion ↗

How to Scan Docker Images for Vulnerabilities with Trivy

Scan container images with Trivy to find known vulnerabilities, review severity and fixes, and add image security checks to CI/CD.

Container images package applications and their dependencies into artifacts that can be distributed and deployed across different environments. However, an image may also contain outdated operating system packages, vulnerable libraries, exposed secrets, or other security concerns.

Scanning container images before deployment helps teams identify known vulnerabilities and investigate potential risks before an image reaches production.

Trivy is an open-source security scanner that can inspect container images and other targets for several types of security issues. This guide explains how to scan Docker images, interpret findings, export reports, and introduce container security checks into a development workflow.

What Is Trivy?

Trivy is a security scanner developed by Aqua Security. Its supported targets include container images, filesystems, repositories, and infrastructure configuration, among others.

For container images, Trivy can identify known vulnerabilities in operating system packages and supported language-specific dependencies. Secret scanning is also available, while some other checks require explicit configuration.

A vulnerability finding generally indicates that a detected software component matches information in the scanner's vulnerability data. It does not automatically mean that the application is exploitable in its current environment.

The result should be reviewed alongside the affected component, available fixes, application usage, and deployment context.

Prerequisites

Before starting, prepare:

  • Docker or another supported way to make the image available.

  • Trivy installed according to its official documentation.

  • Access to the container image you are authorized to assess.

  • Network access if the scanner needs to download vulnerability databases or image layers.

Official installation instructions are available at:

https://trivy.dev/latest/getting-started/installation/

For reproducible assessments, record the Trivy version, image reference, and scan date.

Step 1: Select a Container Image

Choose an image that you own, maintain, or are authorized to evaluate.

For example, you can use a locally built application image:

docker build -t myapp:1.0 .

This command builds an image from the Dockerfile in the current directory and tags it as myapp:1.0.

The example assumes that a suitable Dockerfile and application build context already exist. It does not create a working application by itself.

For production workflows, prefer a specific version tag or immutable image digest when tracking which artifact was assessed. A mutable tag may refer to different image contents at different times.

Step 2: Run a Basic Vulnerability Scan

Use the following command to scan the image:

trivy image myapp:1.0

Trivy examines the image and reports detected vulnerabilities based on its supported scanners and available vulnerability data.

Depending on the image, the output may include:

  • Package or library name.

  • Vulnerability identifier.

  • Severity.

  • Installed package version.

  • A fixed version when the data source identifies an applicable fix.

  • A short vulnerability description.

The first scan may take longer while the scanner downloads or updates its databases.

For a public example image, you can substitute an image from a trusted registry that you are permitted to assess. Always review the source and intended use of an image before incorporating it into a production workflow.

Step 3: Filter Findings by Severity

Large images may contain many findings. Severity filtering can help prioritize the first review.

trivy image \
  --severity HIGH,CRITICAL \
  myapp:1.0

This command limits displayed findings to the selected severity levels.

Filtering is a prioritization mechanism, not a complete risk assessment. Lower-severity vulnerabilities may still matter, particularly when they affect sensitive components or can be combined with other weaknesses.

For a comprehensive baseline, retain a record of the full results even when the immediate review focuses on higher-severity findings.

Step 4: Generate a Machine-Readable Report

Trivy supports structured output formats that can be archived or processed by other tools.

For example, generate a JSON report:

mkdir -p trivy-reports

trivy image \
  --format json \
  --output trivy-reports/myapp-report.json \
  myapp:1.0

The report is written to:

trivy-reports/myapp-report.json

JSON output can support internal dashboards, audit records, and automated processing. It is not inherently a complete remediation plan; teams still need to interpret the results and assign corrective actions.

Reports may contain sensitive operational details. Store them with appropriate access controls and retention policies.

Step 5: Review Vulnerability Findings

Avoid treating every reported CVE as equally urgent. A structured review should establish what the scanner found and whether the finding is relevant to the application.

Identify the affected component

Determine which package or library is associated with the vulnerability. Confirm the installed version and whether the component belongs to the operating system or the application's dependency stack.

Examine the vulnerability details

Review the vulnerability identifier and available advisory information. Check the affected version range, relevant conditions, severity rationale, and vendor guidance.

Check whether a fix is available

Trivy may report a fixed version when one is known from its data sources. Confirm that the proposed update is appropriate for the package manager, operating system, and application.

An empty fixed-version field does not necessarily mean that no mitigation exists. It may reflect the available advisory information or the absence of a known fix in the data source.

Assess application context

A vulnerability's practical risk depends on more than its severity score. Consider whether the affected component is reachable, whether the vulnerable functionality is used, the privileges available to the process, and the consequences of exploitation.

Document the reasoning behind remediation priority and any accepted exceptions.

Step 6: Scan the Dockerfile and Configuration

Vulnerable packages are only one part of container security. The way an image is built and configured can introduce additional risks.

Trivy provides configuration scanning for Dockerfiles and supported infrastructure-as-code formats.

For example, scan a directory containing a Dockerfile:

trivy config .

This checks supported configuration files in the selected directory and reports applicable misconfiguration findings.

Depending on the files and rules involved, results may highlight issues such as using a mutable base-image tag or failing to configure a non-root user.

Configuration checks have their own scope and rule behavior. Review the finding descriptions before changing a build file, and test proposed changes to ensure the application continues to work correctly.

Do not assume that every configuration scanner runs by default during an image vulnerability scan. Consult the current Trivy documentation for the specific command and scanner options.

Step 7: Integrate Scanning into CI/CD

A container security check can be added to the build pipeline so that developers receive feedback before publishing or deploying an image.

The following GitHub Actions example illustrates the approach:

name: Container Image Security

on:
  workflow_dispatch:

jobs:
  scan:
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Build image
        run: docker build -t myapp:ci .

      - name: Set up Trivy
        uses: aquasecurity/setup-trivy@v0.2.4
        with:
          version: latest

      - name: Scan image
        run: trivy image --severity HIGH,CRITICAL myapp:ci

This is an illustrative starting point, not a verified production workflow. Before adopting it, check the official action documentation, confirm the current action versions, and pin dependencies according to your organization's supply-chain policy.

The workflow uses a manually triggered event to allow the build and scan process to be reviewed before automation is expanded.

Decide how findings affect releases

A security scan is more useful when the team defines what happens after a finding appears.

A policy might require investigation of critical findings, block releases for confirmed high-risk issues, or allow documented exceptions with an owner and review date.

Be cautious about implementing a strict failure threshold without testing it against real project results. A threshold that generates excessive noise may encourage developers to ignore the scanner, while a permissive policy may allow serious issues to pass unnoticed.

A useful policy balances risk, remediation capacity, and the reliability of the findings.

Step 8: Remediate and Rescan

When a relevant vulnerability is identified:

  1. Confirm the affected package and version.

  2. Review the vendor advisory and available fix.

  3. Update the base image or application dependency as appropriate.

  4. Rebuild the container image.

  5. Scan the newly built artifact.

  6. Record the outcome and verify that the expected finding is resolved.

Updating the Dockerfile alone does not prove that the deployed image contains the intended fix. The rebuilt image should be the artifact that is tested and promoted.

Similarly, a clean scan does not prove that an image is free of all vulnerabilities. Detection depends on scanner coverage, vulnerability data, package identification, and the image contents.

Common Mistakes to Avoid

Scanning only the base image. Application dependencies added during the build may introduce separate vulnerabilities.

Using mutable tags without recording image identity. This makes it harder to reproduce an earlier scan.

Treating severity as the only priority signal. Assess application context, exposure, available fixes, and business impact.

Ignoring findings without documentation. Exceptions should have a rationale, owner, and review process.

Assuming a clean report means the image is secure. A scanner can only detect issues covered by its capabilities and available data.

Failing to rescan after rebuilding. A remediation change should be verified against the actual new artifact.

Trivy's Limitations

Trivy can identify known vulnerabilities in supported packages and report other configured security checks, but it does not replace every form of application security testing.

It cannot reliably prove the absence of unknown vulnerabilities, every exploitable code path, or all runtime security weaknesses.

Some packages may not be recognized, vulnerability records may be incomplete, and fix information can vary by ecosystem. Misconfiguration detection also depends on supported formats and enabled scanners.

For stronger assurance, combine image scanning with secure build practices, dependency management, secrets protection, configuration review, and runtime controls.

Conclusion

Trivy provides a practical way to assess container images before deployment. It can identify known vulnerabilities, produce machine-readable reports, and support repeatable checks in CI/CD workflows.

The most valuable results come from a disciplined process: scan the artifact that will actually be deployed, validate important findings, prioritize remediation using context, and rescan after rebuilding.

Container scanning is one layer of defense, not a guarantee of security. When integrated into a broader software supply-chain process, it gives teams better visibility into the risks introduced by application dependencies and image contents.

Official References

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.