Trivy 镜像漏洞扫描实践教程

容器安全是 Docker 运维中不可忽视的环节。Docker 镜像中可能包含具有已知漏洞的操作系统包或应用依赖,如果不加以检查,可能成为安全攻击的入口。Trivy 是由 Aqua Security 开发的综合性安全扫描工具,能够检测容器镜像、文件系统、Git 仓库中的漏洞和配置问题。本文介绍如何在搬瓦工 VPS 上使用 Trivy 保障容器安全。

一、安装 Trivy

1.1 使用安装脚本

curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
trivy --version

1.2 Ubuntu/Debian 包管理器安装

apt-get install -y wget apt-transport-https gnupg
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb generic main" | tee /etc/apt/sources.list.d/trivy.list
apt-get update && apt-get install -y trivy

1.3 通过 Docker 运行

docker run --rm \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v trivy-cache:/root/.cache/ \
  aquasec/trivy:latest image nginx:latest

二、扫描 Docker 镜像

# 扫描本地镜像
trivy image nginx:latest

# 扫描远程镜像(无需先拉取)
trivy image docker.io/library/python:3.12

# 只显示已修复的漏洞(有补丁可用)
trivy image --ignore-unfixed nginx:latest

# 按严重级别过滤
trivy image --severity HIGH,CRITICAL nginx:latest

# 输出 JSON 格式(便于程序处理)
trivy image --format json -o results.json nginx:latest

# 输出表格格式(默认)
trivy image --format table nginx:latest

三、漏洞严重级别

Trivy 将漏洞分为以下级别:

  • CRITICAL:严重漏洞,可能导致远程代码执行等高危风险,必须立即修复。
  • HIGH:高危漏洞,存在较大安全风险,应尽快修复。
  • MEDIUM:中危漏洞,有一定风险但利用条件较苛刻。
  • LOW:低危漏洞,风险较小。
  • UNKNOWN:严重级别未知。

四、扫描文件系统和代码仓库

# 扫描项目目录(检查依赖文件中的漏洞)
trivy fs /opt/myproject/

# 扫描 Git 仓库
trivy repo https://github.com/user/myrepo

# 扫描 Dockerfile 配置问题
trivy config /opt/myproject/Dockerfile

# 扫描 Kubernetes YAML 配置
trivy config /opt/k8s-manifests/

五、生成 SBOM(软件物料清单)

# 生成 CycloneDX 格式的 SBOM
trivy image --format cyclonedx -o sbom.json nginx:latest

# 生成 SPDX 格式的 SBOM
trivy image --format spdx-json -o sbom-spdx.json nginx:latest

# 扫描已有的 SBOM 文件
trivy sbom sbom.json

六、CI/CD 集成

6.1 设置退出码

# 发现 CRITICAL 或 HIGH 漏洞时返回非零退出码
trivy image --exit-code 1 --severity CRITICAL,HIGH myapp:latest

# 在构建脚本中使用
docker build -t myapp:latest .
trivy image --exit-code 1 --severity CRITICAL myapp:latest
if [ $? -ne 0 ]; then
    echo "Security scan failed! Critical vulnerabilities found."
    exit 1
fi

6.2 GitHub Actions 集成

cat > .github/workflows/trivy.yml <<'EOF'
name: Trivy Scan
on: [push, pull_request]
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Build image
      run: docker build -t myapp:${{ github.sha }} .
    - name: Trivy scan
      uses: aquasecurity/trivy-action@master
      with:
        image-ref: myapp:${{ github.sha }}
        severity: CRITICAL,HIGH
        exit-code: 1
EOF

七、定期扫描运行中的容器

cat > /opt/trivy-scan.sh <<'EOF'
#!/bin/bash
echo "=== Trivy Security Scan: $(date) ==="
for image in $(docker ps --format '{{.Image}}' | sort -u); do
    echo "--- Scanning: $image ---"
    trivy image --severity HIGH,CRITICAL --ignore-unfixed "$image"
done
echo "=== Scan Complete ==="
EOF

chmod +x /opt/trivy-scan.sh

# 每周扫描一次
# crontab 中添加:
# 0 4 * * 0 /opt/trivy-scan.sh >> /var/log/trivy-scan.log 2>&1

八、修复策略

  • 更新基础镜像:升级到最新版本的基础镜像通常能修复大量 OS 层面的漏洞。
  • 更新依赖:定期更新应用的第三方依赖。
  • 使用精简镜像:Alpine、Distroless 等精简镜像包含的组件少,漏洞也少。
  • 忽略不可修复的漏洞:使用 .trivyignore 文件忽略暂无补丁的漏洞。
# 创建忽略文件
cat > .trivyignore <<'EOF'
# 忽略特定 CVE(备注原因)
CVE-2023-xxxxx  # 已评估,不影响我们的使用场景
CVE-2024-xxxxx  # 等待上游修复
EOF

trivy image --ignorefile .trivyignore myapp:latest

总结

Trivy 是保障搬瓦工 VPS 上 Docker 容器安全的重要工具。建议在 CI/CD 流程中集成 Trivy 扫描,并定期对运行中的容器进行安全检查。配合 Hadolint 进行 Dockerfile 质量检查,以及 Dive 进行镜像层分析,形成完整的镜像安全保障体系。选购搬瓦工 VPS 请访问 bwh81.net,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。