# Docker 容器安全掃描實戰:用 Trivy 幫你啲 Container 做體檢
呢排成日聽到有 Linux kernel 漏洞、container escape、supply chain attack,成個 IT 界都人心惶惶。如果你公司有用 Docker 嘅話,容器安全好大機會係你最應該關心但仲未夠重視嘅課題。今日就同大家 step-by-step 示範點樣用 Trivy 做容器安全掃描,等你啲 container 唔會變成黑客嘅後門。
👉 延伸閱讀:Linux Server 安全加固 6 步搞掂 — Defense in Depth 實戰指南
## 容器安全:點解你一定要做掃描?
Container image 係由好多層 layer 疊出嚟嘅,每一層都可以有漏洞。你可能會話:「我啲 image 係 official 㗎喎,仲要 scan?」老老實實,上年 Node.js official image 都試過有 critical CVE,官方 docker library 嘅 image 平均有成 180 個已知漏洞。容器安全唔係 optional,係 must。
Trivy 係 Aqua Security 出嘅 open source 掃描器,專掃 container image、filesystem、git repo 嘅漏洞同錯誤設定。支援 Alpine、RHEL、CentOS、Ubuntu 等常見 base image。而且佢快、輕量、CLI 友好,啱晒 CI/CD pipeline 整合。
## 容器安全 Step 1:安裝 Trivy
macOS 用戶最簡單用 Homebrew:
brew install aquasecurity/trivy/trivy
Linux 用 apt 或者直接 download binary:
# Ubuntu/Debian
sudo apt-get install -y wget apt-transport-https gnupg
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo gpg --dearmor -o /usr/share/keyrings/trivy.gpg
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" | sudo tee /etc/apt/sources.list.d/trivy.list
sudo apt-get update && sudo apt-get install -y trivy
裝完 verify 下:
trivy --version
## 容器安全 Step 2:基本掃描
最簡單就係直接 scan 一個 image。例如 scan nginx:
trivy image nginx:latest
Output 會 show 晒所有 detected vulnerabilities 連 severity level(CRITICAL、HIGH、MEDIUM、LOW),仲會話你知邊個 package 有問題同 fix version 係咩。呢個已經係好好嘅容器安全起點。
想淨係睇 critical 同 high?
trivy image --severity CRITICAL,HIGH nginx:latest
想 export 做 JSON report 俾 compliance audit?
trivy image --format json --output nginx-report.json nginx:latest
## 容器安全 Step 3:Scan 自己的 Dockerfile
Trivy 仲可以 scan Dockerfile 本身嘅 misconfiguration。呢個功能對於容器安全尤其重要,因為好多時危險設定先係被 hack 嘅主因:
trivy config ./Dockerfile
常見嘅 Dockerfile 安全問題包括:用 root user run container(應該用 USER 指令 switch 去 non-root)、COPY 咗 secret 入 image、冇做 healthcheck、port expose 太多。Trivy 會逐個指出嚟。
## 容器安全 Step 4:整合 CI/CD Pipeline
將容器安全檢查放落 CI/CD 係最有效嘅做法,確保每次 build 都自動 scan。GitHub Actions example:
name: Container Security Scan
on: [push]
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build -t myapp:${{ github.sha }} .
- name: Run Trivy scan
uses: aquasecurity/trivy-action@master
with:
image-ref: 'myapp:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
exit-code: '1'
severity: 'CRITICAL,HIGH'
如果 scan 到 CRITICAL 或 HIGH 漏洞,pipeline 會 fail,阻止有問題嘅 image 被 deploy。呢個係真正嘅 Shift Left Security — 將容器安全責任推前到開發階段。
## 容器安全 Step 5:定期排程掃描
Image scan 完一次唔代表以後都安全。新漏洞每日都有,禽日冇事嘅 image 聽日可能中招。建議 set up cron job 定期 scan 已經 deploy 咗嘅 production image:
# crontab -e
0 2 * * * /usr/local/bin/trivy image --severity CRITICAL,HIGH myapp:production --ignore-unfixed >> /var/log/trivy-cron.log 2>&1
另外 Trivy 有內置 database auto-update,每次 scan 前會自動 pull 最新 vulnerability DB,唔使自己維護。
## 容器安全總結
容器安全唔係一朝一夕嘅事,但起碼要由最基本做起:scan 你嘅 image、fix critical/high 漏洞、scan Dockerfile misconfig、整合落 CI/CD、定期排程檢查。呢幾步做齊,你啲 container 已經安全過市面上 80% 嘅 deployment。
而家就行第一步啦:
trivy image $(docker images --format '{{.Repository}}:{{.Tag}}' | head -5)
Scan 完見到紅色唔好驚,慢慢 fix 就得。容器安全係 continuous process,最緊要係開始做!
—
#容器安全 #Docker #Trivy #DevSecOps #ContainerSecurity #漏洞掃描



