Trivy installieren
# Ubuntu/Debian
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
# Oder via apt
apt-get install -y apt-transport-https gnupg
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor > /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" > /etc/apt/sources.list.d/trivy.list
apt update && apt install -y trivy
trivy version
Docker Image scannen
# Image scannen (lädt Vulnerability DB automatisch)
trivy image nginx:stable-alpine
# Nur kritische und hohe CVEs
trivy image --severity HIGH,CRITICAL nginx:latest
# JSON-Output für CI/CD
trivy image --format json --output scan-results.json nginx:latest
# Nur fixierte Vulnerabilities (ignoriere "will not fix")
trivy image --ignore-unfixed nginx:latest
# Lokal gebautes Image
trivy image meine-app:latest
# Öffentliche Images aus verschiedenen Registries
trivy image gcr.io/google-containers/pause:3.9
trivy image mcr.microsoft.com/dotnet/aspnet:8.0
Dockerfile scannen (IaC)
# Dockerfile auf Fehlkonfigurationen
trivy config --severity HIGH,CRITICAL Dockerfile
# Beispiel-Output:
# Dockerfile (dockerfile)
# MEDIUM: Last USER command is not to a non-root user
# → FROM nginx AS final / USER nginx
trivy config ./kubernetes/
trivy config ./terraform/
In GitHub Actions integrieren
# .github/workflows/security-scan.yml
name: Container Security Scan
on: [push, pull_request]
jobs:
trivy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build Image
run: docker build -t meine-app:ci .
- name: Trivy Scan
uses: aquasecurity/trivy-action@master
with:
image-ref: 'meine-app:ci'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '1' # Fehler bei Fund!
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results.sarif'
Filesystem und Code scannen
# Lokales Verzeichnis scannen (Node.js, Python, Go, etc.)
trivy fs .
# Secrets im Code finden
trivy fs --security-checks secret .
# Findet: API-Keys, Passwörter, Private Keys in Code!
# Git-Repository scannen
trivy repo https://github.com/firma/meine-app
FAQ
Wie oft wird die Vulnerability-Datenbank aktualisiert?
Trivy lädt die DB automatisch täglich. Im CI kann man mit --skip-db-update und vorgeladenem Cache beschleunigen.
Fazit
Trivy ist der einfachste Weg CVE-Scanning in CI/CD zu integrieren: ein Befehl scannt Image, Dockerfile, IaC und Code auf Schwachstellen.
Container-Sicherheit und DevSecOps für KMU in Heidelberg, Mannheim und der Rhein-Neckar-Region. Anfragen.