Don’t wait for a hacker ‘visit’ to start panicking
Many of us have an “instant noodle” habit: finish coding, build the image, and if the app runs, push it straight to production. But believe me, that feeling of excitement will quickly turn into a disaster if you skip the security phase.
I once witnessed a system get hijacked just because it used an old library with a 2-year-old vulnerability. The whole team had to stay up all night to isolate the container and patch it in a panic. From that breakdown, I learned a lesson: Security must be prioritized from the build stage (Shift Left Security). Don’t wait until the system is running to find holes.
In reality, a heavy base image like node:latest can carry over 600 security vulnerabilities (CVEs) as soon as you pull it. That’s why you need Trivy. It’s an incredibly powerful tool for spotting flaws in Docker images and Kubernetes manifests without slowing down your workflow.
Why is Trivy becoming so popular?
If you need a tool that scans fast without messy installations or complex database configurations, Trivy is your perfect match. This open-source tool from Aqua Security can target multiple components:
- OS Packages: Detect flaws in operating system packages like Alpine, Ubuntu, or RHEL.
- Language Packages: Find vulnerabilities in Python (pip), Node.js (npm), Go, and Java libraries.
- Infrastructure as Code (IaC): Inspect misconfigurations in Dockerfiles, Kubernetes, and even Terraform.
The biggest advantage is that Trivy doesn’t require a running daemon or an agent installation. You just need to download a single executable to start scanning immediately. It’s highly flexible for plugging into CI/CD pipelines like GitHub Actions or GitLab CI.
Hands-on: Install and Use Trivy in 3 Easy Steps
1. Quick Installation
On Linux, you only need a single command to bring Trivy into your system:
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.48.3
Once finished, type trivy --version to check. If the version appears, you’re ready to start vulnerability hunting.
2. Scanning a Real Docker Image
Let’s compare some concrete numbers. Try scanning the python:3.9 (full version) image with the command:
trivy image python:3.9
You’ll see hundreds of vulnerabilities pop up. Now, let’s filter to see only the critical ones using the severity parameter:
trivy image --severity HIGH,CRITICAL python:3.9-slim
The results will be much cleaner. Looking at this, you’ll know exactly which packages need upgrading or if you should switch to a safer base image like Distroless or Alpine.
3. Checking Kubernetes Configuration Files
Trivy can also inspect YAML files. Suppose you have a deployment.yaml file and want to know if you’ve accidentally granted excessive root permissions to a container:
trivy conf ./k8s-manifests
It will alert you immediately if you forget CPU/RAM limits or leave the container running in privileged mode. These minor mistakes are often gateways for hackers to take control of your entire cluster.
Automating Security with GitHub Actions
Don’t let vulnerability scanning depend on human memory. We’ll make GitHub Actions automatically scan every time there’s new code. If a CRITICAL error is found, the pipeline will stop and prevent code merging.
Here is a minimal .github/workflows/security.yml template that I often use:
name: Security Scan
on:
push:
branches: [ main ]
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Build image
run: docker build -t my-app:${{ github.sha }} .
- name: Run Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: 'my-app:${{ github.sha }}'
exit-code: '1'
severity: 'CRITICAL,HIGH'
Setting exit-code: '1' will turn the pipeline red if critical errors are found. This forces the team to address security issues before deploying to production.
Hard-Learned Lessons to Avoid Being Overwhelmed
The first time you scan, you might be shocked by the long list of errors. Don’t panic; apply these three rules:
- Use the
--ignore-unfixedflag: Focus only on vulnerabilities that have a patch. Temporarily ignore those without a fix from the vendor to avoid clutter. - Prioritize Slim/Alpine images: Instead of
node:latest(often over 500 vulnerabilities), usenode:alpine. The number of issues can drop to under 10, making them much easier to manage. - Schedule regular scans: A clean image today isn’t necessarily safe next week. Create a CronJob to re-scan images running on your server once a week.
Conclusion
Trivy isn’t a magic wand for absolute security. However, it helps you block up to 90% of common risks. Instead of spending hours handling incidents, take 5 minutes to integrate this tool. Wishing you peaceful nights, without the worry of your server being ‘knocked on’ at midnight!

