How to scan Docker images for vulnerabilities with Trivy and act on the results
A container image contains an operating system and libraries, and they age like any server. Scanning tells you which known vulnerabilities your images ship with and which of them have a fix you can pull in.
Install Trivy
Use the official installation instructions for your system, then check the version.
trivy --versionScan an image
Start with what has a fix, at high and critical severity, otherwise the list is overwhelming.
trivy image --severity HIGH,CRITICAL --ignore-unfixed your-image:tagRead the result by package
Most findings come from the base image. Updating the base tag clears most of them.
trivy image --format table --severity CRITICAL your-image:tag | head -n 40Rebuild on a newer base
Pull the base again so the build does not reuse a cached old layer.
docker build --pull --no-cache -t your-image:new .Scan the running host's images in one go
List every image in use.
docker ps --format '{{.Image}}' | sort -u | xargs -n1 trivy image --severity HIGH,CRITICAL --ignore-unfixed --quietAdd it to CI
Fail the build when a critical, fixable issue appears.
trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed your-image:tag
Common mistakes
- Treating every finding as urgent: unreachable code paths and unfixed issues are noise.
- Rebuilding without --pull so the old base layer stays.
- Scanning once: new CVEs are published daily, so repeat on a schedule.
How to undo it
Keep the previous image tag and redeploy it if the rebuilt image misbehaves.
Sources
Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.