Marcio Cunha

How to audit vulnerabilities in Docker containers with Trivy

Learn how to integrate automated container image security scanning using Trivy directly into your CI/CD pipeline to block vulnerable deployments.

Marcio Cunha•4 min
Also available in:EspañolPortuguês
Summary
  • Container security requires automated verification before code ever reaches the production environment.
  • Trivy acts as a lightweight scanner that identifies known flaws in operating system packages and libraries.
  • Inserting audits into the CI/CD pipeline prevents critical vulnerabilities from slipping past human oversight.
  • Automatically blocking deployments based on flaw severity protects infrastructure against active exploits.
  • Keeping the vulnerability database updated ensures accuracy in detecting newly discovered threats.

Why container security starts long before production

When we build applications and package them into containers using tools like Docker, we usually focus solely on making the code work. However, every base image we choose brings along dozens or hundreds of operating system packages that may contain known security flaws. In practice, this means putting a system live without checking these components is like locking the front door and leaving the window wide open. If an attacker discovers an old flaw in a forgotten library inside your container, they can compromise the entire server.

To avoid this kind of unpleasant surprise, the software engineering community has adopted the practice of scanning container images for vulnerabilities before they even reach official environments. Just as we learn to debug Docker container network failures with tcpdump to understand internal traffic, we need dedicated tools to inspect the internal content of installed packages. This is exactly where Trivy comes in, a modern, fast vulnerability scanner deeply integrated into the development ecosystem.

What is Trivy and how does it inspect images

Trivy is an open-source tool developed by Aqua Security that stands out for its ease of use and scanning speed. Instead of requiring complex configurations, it analyzes container image files, local filesystems, and even code repositories for vulnerabilities in package managers like Node.js, Python, Rust, and Linux dependencies. When executed, it compares the discovered packages against a global database of known vulnerabilities known as CVE.

In practice, Trivy acts as a meticulous inspector that opens each layer of your container and verifies the list of installed ingredients. If a library has a documented flaw that allows unauthorized remote access, the scanner issues a detailed alert indicating the risk level and the available patched version. This immediate visibility turns security from an abstract concept into a clear metric that can be measured and automatically blocked.

Automating security in the CI/CD pipeline

Continuous Integration and Continuous Delivery pipelines, known as CI/CD, are the automated tracks through which code travels from the moment it is pushed to the repository until it goes live. Automating security scanning within this workflow means no vulnerable container image can sneak into production without passing the scanner's scrutiny. Just as we ensure that pipelines do not fail due to low disk space by cleaning Docker regularly, we must ensure it blocks critical security flaws before finalizing the packaging.

When we configure Trivy inside tools like GitHub Actions, GitLab CI, or Jenkins, we create a specific stage right after building the container image. If the scanner finds vulnerabilities above a pre-established threshold, such as critical or high severity, the pipeline halts immediately. This prevents defective code from being sent to customer servers, forcing developers to update dependencies before attempting a new deployment.

Implementing step-by-step scanning in your pipeline

To put this strategy into practice, we can use a straightforward command-line approach that fits easily into any current automation script. The first step is to download or use the official Trivy image directly in your CI/CD pipeline to analyze the newly built container image. The command below demonstrates how to perform this scan and fail the process if critical flaws are found.

Run the verification using the following command in your automation environment:

  1. Build your container image locally or on the build server using the standard build command.
    docker build -t my-application:latest .
  2. Run the Trivy container pointing to the generated image, configuring the parameter to fail on critical vulnerabilities.
    trivy image --exit-code 1 --severity CRITICAL my-application:latest
  3. Analyze the generated report in the terminal and update the targeted packages before attempting another push to production.
    echo 'Scan complete. Fix the listed packages before deploying.'

Using the exit code parameter is essential because it informs the CI/CD server that the process failed, automatically interrupting the delivery cycle. This way, security no longer relies on the team's manual attention and becomes an unnegotiable system rule.

Handling false positives and operational exceptions

In the real world of software development, not every vulnerability pointed out by a scanner represents a genuine risk to your specific application. Sometimes, a flaw is detected in an operating system package that your application does not even use, or whose attack vectors are blocked by other security layers. In these cases, excessive false alarms can frustrate the team and cause real alerts to be ignored.

To solve this dilemma without compromising security, Trivy allows the use of exception configuration files known as .trivyignore files. In these files, you can list specific vulnerabilities that have been analyzed and deemed low risk for your scenario, formally justifying the decision. Keeping this list lean and periodically reviewed ensures that the pipeline remains strict against real threats without paralyzing delivery flow due to excessive bureaucracy. To explore these architectural concepts further in production, see our related technical analysis on How to use local embeddings with ChromaDB for semantic search.

Final thoughts on container hardening

Automating vulnerability scanning in containers with Trivy represents an essential evolution in the technical maturity of any engineering team. By integrating this verification directly into the CI/CD pipeline, we eliminate reliance on manual checking and ensure that no compromised code reaches production servers. Security stops being a bottleneck at the end of the project and becomes an integral, invisible part of the daily development flow, protecting both the company and end users from unforeseen threats.