Skip to main content

Docker Image Scan (With Trivy)

ขั้นตอนการใช้งานบน Ubuntu 22.04 LTS​

  1. ติดตั้ง trivy โดยรันคำสั่งติดตั้งดังนี้
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb 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
  1. หากต้องการตรวจสอบ container image ที่อยู่บน registry อื่นๆ ที่ไม่ใช่ docker hub ให้ทำการ pull มายังเครื่องที่ใช้ run คำสั่งก่อน ตัวอย่างเช่น
docker login registry.in.psu.ac.th -u [YOUR_USERNAME] -p [YOUR_PASSWORD]
docker pull registry.in.psu.ac.th/api/logs:latest
  1. เริ่มการ scan โดยระบุ container image (ในตัวอย่าง container image ชื่อ registry.in.psu.ac.th/docs/hrms:latest) และใช้ template ที่สร้าง report ในรูปแบบ HTML ไฟล์ ด้วยคำสั่งดังนี้
sudo trivy image --format template --template "@/usr/local/share/trivy/templates/html.tpl" --scanners vuln -o report.html registry.in.psu.ac.th/docs/hrms:latest
  1. จากคำสั่งด้านบนจะได้ผลลัพธ์เป็น ไฟล์ HTML ที่สามารถเปิดดูได้ด้วย browser ต่างๆ ดังรูป
  2. เมื่อทำการตรวจสอบเสร็จแล้วสามารถลบ container image ได้โดยใช้คำสั่ง
docker rmi registry.in.psu.ac.th/docs/hrms:latest