The Risk of Shipping Vulnerable Containers to Production
When first getting started with Docker, almost everyone just wants the image to build successfully and the app to run smoothly. It’s a familiar routine: write a few lines in a Dockerfile, pull a random base image from Docker Hub, toss the code in, and run docker build. Once the container runs fine locally, many breathe a sigh of relief and push the code straight to Git.
Real-world production environments aren’t so forgiving. Many public base images are riddled with known Common Vulnerabilities and Exposures (CVEs). On top of that, running containers as root by default or unnecessarily exposing ports is practically an open invitation to attackers. If a remote code execution vulnerability is exploited, an attacker can easily achieve a container breakout to compromise the host server.
Traditionally, teams waited until deployment was complete before bringing in security teams for penetration testing. If critical flaws were found, patching them post-release was not only labor-intensive but also risked downtime. DevSecOps transforms this approach through a Shift-Left mindset: scanning and catching issues right as you write code, build images locally, or run CI/CD pipelines.
How Does Trivy Fit into DevSecOps Workflows?
Trivy is an open-source security scanner developed by Aqua Security. It has gained widespread popularity thanks to its blazing-fast scan speeds (typically taking only 5–10 seconds for a medium-sized image) and lightweight setup that requires no auxiliary database server.
What Can Trivy Scan?
- Vulnerability Scanning (CVE): Audits both OS packages (Alpine, Debian, Ubuntu…) and application dependencies (npm, pip, Maven, Composer). It pinpoints vulnerable library versions alongside their specific CVE identifiers.
- Misconfiguration Scanning (IaC/Dockerfile): Inspects individual directives in Dockerfiles or Kubernetes and Terraform configurations. For instance, it flags containers running as root or configurations granting overly broad permissions.
- Secret Scanning: Hunts down API tokens, private keys, or database credentials accidentally hardcoded into source code or left behind in Docker image layers.
Vulnerability Database Updates
On each execution, Trivy automatically syncs the latest vulnerability database to your local machine. This database is continuously aggregated by Aqua Security from the NVD, GitHub Advisory Database, and security advisories from various Linux distributions.
Hands-on: Installing Trivy, Linting Dockerfiles, and Blocking Deploys in GitHub Actions
Here are hands-on steps you can run directly on Ubuntu or macOS.
Step 1: Install Trivy
On Ubuntu/Debian, run the following commands to install Trivy from the official apt repository:
sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install trivy -y
# Verify successful installation
trivy --version
On macOS, simply run brew install trivy.
Step 2: Create a Dockerfile with Common Misconfigurations
Create a test directory and add the following Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
This file contains two critical flaws. First, Node 14 has reached End of Life (EOL) and contains dozens of unpatched security vulnerabilities. Second, it never defines a USER directive, meaning the container will run as root by default.
Step 3: Scan for Misconfigurations Using trivy config
You don’t need to waste time building the image first. Ask Trivy to analyze static configuration files immediately:
trivy config Dockerfile
Within a couple of seconds, Trivy will output a detailed list of warnings, including:
AVD-DS-0002: Specify at least 1 USER command in Dockerfile with non-root user- Severity: HIGH
To fix these issues, switch to a supported LTS base image and create a dedicated non-root user:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
# Create a dedicated group and user to run the app
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
EXPOSE 3000
CMD ["node", "app.js"]
Step 4: Scan the Entire Built Docker Image
Once the Dockerfile is updated, build the image and scan its packages and dependencies:
docker build -t my-node-app:v1 .
trivy image my-node-app:v1
If the output table is overwhelming, use the --severity flag to focus on pressing risks:
trivy image --severity HIGH,CRITICAL my-node-app:v1
Many vulnerabilities may not have an upstream patch available yet. To reduce noise and focus on immediately actionable items, add the --ignore-unfixed flag:
trivy image --severity HIGH,CRITICAL --ignore-unfixed my-node-app:v1
Simply by switching from a full-fat image to Alpine and stripping unused packages, our team once reduced image size from 1.2GB down to 140MB while eliminating 45 HIGH-severity CVEs stemming from outdated OS packages.
Step 5: Set Up an Automated Quality Gate in GitHub Actions
Don’t rely on manual diligence. Let the CI pipeline enforce checks automatically on every pull request. Create .github/workflows/security-scan.yml:
name: Security Scan with Trivy
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
scan-and-build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Check Dockerfile configuration
uses: aquasecurity/[email protected]
with:
scan-type: 'config'
scan-ref: '.'
severity: 'HIGH,CRITICAL'
exit-code: '1'
- name: Build Docker Image
run: |
docker build -t my-app:${{ github.sha }} .
- name: Scan Image Vulnerabilities
uses: aquasecurity/[email protected]
with:
image-ref: 'my-app:${{ github.sha }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL'
The key configuration here is exit-code: '1'. When Trivy detects any fixable CVE meeting the CRITICAL threshold, the step exits with an error code. GitHub Actions immediately halts the pipeline, preventing the pull request from being merged until developers apply the necessary patches.
Conclusion
Security isn’t solely the security team’s responsibility right before a release. The developers writing code and containerizing applications serve as the very first line of defense. Spending a few extra minutes scanning Dockerfiles with Trivy before pushing code can save you from high-stress midnight incident calls.
