Back to articles
Security & Anti-DDoS

Container Security: Automating Docker Image Malware and Vulnerability Scanning Before Deploying to VPS with Trivy

A detailed guide on integrating Trivy into CI/CD pipelines to automatically scan Docker images for malware and security vulnerabilities before deployment to VPS. Covers...

DPTCloud Engineering4 min read
Container Security: Automating Docker Image Malware and Vulnerability Scanning Before Deploying to VPS with Trivy

Introduction

In modern containerized environments, deploying Docker images to VPS is a core part of the CI/CD workflow. However, Docker images may contain malware or security vulnerabilities from external libraries, posing serious safety risks. Trivy, an open-source scanning tool, helps detect security vulnerabilities and malware in Docker images before they are deployed. This guide will walk you through integrating Trivy into your CI/CD pipeline to automatically scan Docker images on VPS.

Integrating Trivy into CI/CD

To integrate Trivy into your CI/CD process, you need to install Trivy on the CI server or in the build environment. Trivy can be installed on various platforms, including Linux, macOS, and Windows.

Installing Trivy

On Linux, you can install Trivy using the following command:

curl -sSfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin

After installation, verify the Trivy version to ensure it is working correctly:

trivy version

Configuring and Scanning Docker Images

Trivy can scan local Docker images or images from a registry. Below is an example of scanning a Docker image from a registry:

trivy image --severity HIGH,CRITICAL --exit-code 1 --format json docker.io/library/nginx:latest

In the command above:

  • --severity HIGH,CRITICAL: Only scan vulnerabilities with high and critical severity.
  • --exit-code 1: If a critical or high severity vulnerability is found, the command returns exit code 1, allowing CI/CD to detect and stop deployment.
  • --format json: Outputs results in JSON format for easy analysis.

Running Scans in CI/CD

In a CI/CD environment, you can add the Trivy scan command to your build pipeline. For example, in GitHub Actions, you can add a step like this:

steps:
  - name: Scan Docker image with Trivy
    run: |
      trivy image --severity HIGH,CRITICAL --exit-code 1 --format json docker.io/library/nginx:latest
      if [ $? -ne 0 ]; then
        echo "Critical vulnerability detected. Deployment stopped." >&2
        exit 1
      fi

If a critical vulnerability is found, the pipeline will stop and report an error. This helps prevent deploying containers with security risks.

Analyzing Results and Handling

Trivy provides scan results in JSON format, making it easy to analyze and handle. Here is an example of a scan result:

{
  "Target": "docker.io/library/nginx:latest",
  "Type": "docker",
  "Vulnerabilities": [
    {
      "VulnerabilityID": "CVE-2023-1234",
      "PkgName": "nginx",
      "Version": "1.23.3",
      "Severity": "HIGH",
      "Title": "Buffer overflow vulnerability in Nginx",
      "Description": "Buffer overflow vulnerability may allow remote code execution",
      "References": [
        "https://nvd.nist.gov/vuln/detail/CVE-2023-1234"
      ]
    }
  ]
}

From this result, you can see that the Nginx Docker image has a critical vulnerability (CVE-2023-1234). You need to update Nginx to a newer version or replace the image.

Requirements and Verification

To successfully run Trivy scans, ensure the following conditions are met:

  • Trivy is correctly installed.
  • The path to the Docker image or registry is accurate.
  • Access rights to the registry if scanning remotely.

To verify, run the scan command on a local Docker image or from a registry. If the command returns exit code 1, it means a critical vulnerability was found and needs to be addressed.

Troubleshooting

If Trivy does not work as expected, check the following:

  • Trivy version may be outdated. Update Trivy using trivy version and install the latest version.
  • No connection to the registry or the image does not exist. Check the path and access rights.
  • Lack of access permissions. Ensure you have read permissions for the Docker image.

For other errors, refer to the official Trivy documentation at https://aquasecurity.github.io/trivy/.

References

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478