Trivy
Install Trivy and scan project files, Git repositories, container images and Kubernetes workloads for vulnerabilities and misconfigurations.
2 min readUpdated
Trivy is an open-source security scanner from Aqua Security. It finds known vulnerabilities in dependencies and operating system packages, misconfigurations in Dockerfiles, Kubernetes manifests and Terraform (Infrastructure as Code), and secrets committed by mistake. It scans a local directory, a remote Git repository, a container image or a running Kubernetes cluster.
Install Trivy
On macOS (and Linux with Homebrew):
brew install trivyThe Trivy documentation lists packages for Debian, Ubuntu, RHEL and others, and a container image.
The first time Trivy runs, it downloads the vulnerability database and creates a cache directory. Remove the cache and the databases with:
trivy clean --allOlder Trivy versions used trivy --reset for this.
Scan a project directory
Scan the dependencies of the project in the current directory. Trivy detects lock files and manifests such as Gemfile.lock, package-lock.json or pom.xml and looks up each dependency in its database:
trivy fs .By default trivy fs looks for vulnerabilities and secrets. Add misconfiguration checks with --scanners; the scan then includes any Dockerfiles, Kubernetes manifests and Terraform files in the directory:
trivy fs --scanners vuln,misconfig .The flag was called --security-checks vuln,config in older versions.
Scan a remote repository
Scan a Git repository without cloning it first:
trivy repo https://github.com/<owner>/<repository>Scan container images
Scan an image for vulnerabilities in its operating system packages and application dependencies:
trivy image my-test-image:versionShow only the findings that need action with --severity:
trivy image --severity HIGH,CRITICAL my-test-image:versiontrivy image --severity HIGH,CRITICAL docker_username/my-test-images:versionThe second form scans an image from a registry; Trivy pulls it if it is not available locally.
Scan Kubernetes
Scan a cluster with trivy k8s
Current Trivy versions scan a cluster directly. The command uses the current context from your kube configuration:
trivy k8s --report summaryLimit the scan to a namespace with --include-namespaces kube-system, or pass a context name as an argument.
Scan one workload with the kubectl plugin
The kubectl plugin scans the images used by a single Kubernetes resource, such as a pod or a deployment. Install it once:
trivy plugin install github.com/aquasecurity/trivy-plugin-kubectlThen scan a pod or a deployment:
trivy kubectl pod mypodtrivy kubectl deployment mydeployment -n mynamespacePass Trivy options after --, for example trivy kubectl job myjob -n mynamespace -- --severity CRITICAL. The plugin has not changed since 2024; prefer trivy k8s for new setups.