scanning-containers-with-trivy-in-cicd

本技能涵盖将 Aqua Security 的 Trivy 扫描器集成到 CI/CD 流水线中,用于全面的容器镜像漏洞检测。包括扫描 Docker 镜像中的操作系统包和应用依赖 CVE、检测 Dockerfile 中的错误配置、扫描文件系统和 Git 仓库,以及建立基于严重性的质量门禁以阻止有漏洞的镜像部署。

9 stars

Best use case

scanning-containers-with-trivy-in-cicd is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

本技能涵盖将 Aqua Security 的 Trivy 扫描器集成到 CI/CD 流水线中,用于全面的容器镜像漏洞检测。包括扫描 Docker 镜像中的操作系统包和应用依赖 CVE、检测 Dockerfile 中的错误配置、扫描文件系统和 Git 仓库,以及建立基于严重性的质量门禁以阻止有漏洞的镜像部署。

Teams using scanning-containers-with-trivy-in-cicd should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/scanning-containers-with-trivy-in-cicd/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/scanning-containers-with-trivy-in-cicd/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/scanning-containers-with-trivy-in-cicd/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How scanning-containers-with-trivy-in-cicd Compares

Feature / Agentscanning-containers-with-trivy-in-cicdStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

本技能涵盖将 Aqua Security 的 Trivy 扫描器集成到 CI/CD 流水线中,用于全面的容器镜像漏洞检测。包括扫描 Docker 镜像中的操作系统包和应用依赖 CVE、检测 Dockerfile 中的错误配置、扫描文件系统和 Git 仓库,以及建立基于严重性的质量门禁以阻止有漏洞的镜像部署。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 在 CI/CD 中使用 Trivy 扫描容器

## 适用场景

- 在 CI/CD 中构建 Docker 容器镜像时,需要在推送到仓库前进行自动化漏洞扫描
- 需要建立质量门禁,阻止包含严重或高危 CVE 的镜像进入生产环境
- 合规要求在部署前对所有容器镜像进行漏洞扫描
- 需要在容器镜像扫描的同时扫描 IaC 文件(Dockerfile、Kubernetes 清单)
- 需要单一工具扫描操作系统包、语言特定依赖以及错误配置

**不适用于**:运行时容器安全监控(应使用 Falco)、扫描生产中正在运行的容器(应使用运行时代理)、或仅扫描未容器化的应用源代码(应使用 SAST 工具)。

## 前提条件

- 已安装 Trivy CLI(v0.50+),或可访问 aquasecurity/trivy-action GitHub Action
- CI/CD 中有可用的 Docker 守护进程,用于构建和扫描镜像
- 拉取基础镜像和推送已扫描镜像所需的容器仓库凭据
- 可访问 Trivy 漏洞数据库(自动下载或已缓存)

## 工作流程

### 步骤 1:在 GitHub Actions 中配置 Trivy 扫描

设置 GitHub Actions 工作流,在推送到容器仓库前构建 Docker 镜像并用 Trivy 进行扫描。

```yaml
# .github/workflows/container-security.yml
name: Container Security Scan

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
    paths:
      - 'Dockerfile'
      - 'docker-compose*.yml'
      - 'src/**'
      - 'requirements*.txt'
      - 'package*.json'

jobs:
  build-and-scan:
    runs-on: ubuntu-latest
    permissions:
      security-events: write
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: 构建 Docker 镜像
        run: docker build -t app:${{ github.sha }} .

      - name: 运行 Trivy 漏洞扫描器
        uses: aquasecurity/trivy-action@0.28.0
        with:
          image-ref: 'app:${{ github.sha }}'
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'CRITICAL,HIGH'
          exit-code: '1'
          ignore-unfixed: true

      - name: 上传 Trivy 扫描结果
        uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: 'trivy-results.sarif'
          category: 'trivy-container'

      - name: 运行 Trivy 错误配置扫描器
        uses: aquasecurity/trivy-action@0.28.0
        with:
          scan-type: 'config'
          scan-ref: '.'
          format: 'table'
          exit-code: '1'
          severity: 'CRITICAL,HIGH'
```

### 步骤 2:扫描 Dockerfile 中的错误配置

Trivy 可检测常见的 Dockerfile 安全问题,如以 root 身份运行、使用 latest 标签、暴露不必要的端口等。

```bash
# 扫描 Dockerfile 中的错误配置
trivy config --severity HIGH,CRITICAL ./Dockerfile

# 使用自定义策略目录扫描
trivy config --policy ./security-policies --severity MEDIUM,HIGH,CRITICAL .

# Trivy 检查的安全 Dockerfile 实践示例:
# - 存在 USER 指令(不以 root 运行)
# - 已定义 HEALTHCHECK 指令
# - 基础镜像使用特定标签,而非 :latest
# - ENV 或 ARG 指令中无密钥信息
# - 优先使用 COPY 而非 ADD
```

### 步骤 3:集成到 GitLab CI/CD

```yaml
# .gitlab-ci.yml
stages:
  - build
  - scan
  - push

variables:
  TRIVY_CACHE_DIR: .trivycache/

build:
  stage: build
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker save $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -o image.tar
  artifacts:
    paths:
      - image.tar

trivy-scan:
  stage: scan
  image:
    name: aquasec/trivy:latest
    entrypoint: [""]
  cache:
    paths:
      - .trivycache/
  script:
    - trivy image
        --input image.tar
        --exit-code 1
        --severity CRITICAL,HIGH
        --ignore-unfixed
        --format json
        --output trivy-report.json
    - trivy image
        --input image.tar
        --severity CRITICAL,HIGH,MEDIUM
        --format table
  artifacts:
    reports:
      container_scanning: trivy-report.json
    paths:
      - trivy-report.json
  allow_failure: false

push:
  stage: push
  needs: [trivy-scan]
  script:
    - docker load -i image.tar
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
```

### 步骤 4:配置 Trivy 忽略和例外处理

通过 Trivy 的忽略文件和 VEX 声明管理误报和已接受的风险。

```yaml
# .trivyignore.yaml
vulnerabilities:
  - id: CVE-2023-44487    # HTTP/2 快速重置 - 已在负载均衡器层面缓解
    statement: "已通过入口层 WAF 速率限制缓解"
    expires: 2026-06-01

  - id: CVE-2024-21626    # runc 容器逃逸 - 基础镜像更新中已修补
    statement: "在 JIRA-SEC-1234 中跟踪,基础镜像更新已计划"
    expires: 2026-03-15

misconfigurations:
  - id: DS002             # 未设置 User - init 容器需要 root 权限
    paths:
      - "docker/init-container/Dockerfile"
    statement: "Init 容器需要 root 权限设置卷权限"
```

### 步骤 5:实现数据库缓存和离线扫描

在 CI/CD 中缓存 Trivy 漏洞数据库,以减少扫描时间并支持隔离网络环境。

```yaml
# 带数据库缓存的 GitHub Actions
- name: 缓存 Trivy DB
  uses: actions/cache@v4
  with:
    path: /tmp/trivy-db
    key: trivy-db-${{ hashFiles('.github/workflows/container-security.yml') }}
    restore-keys: trivy-db-

- name: 使用缓存 DB 运行 Trivy
  uses: aquasecurity/trivy-action@0.28.0
  with:
    image-ref: 'app:${{ github.sha }}'
    cache-dir: /tmp/trivy-db
    format: 'json'
    output: 'trivy-results.json'
    severity: 'CRITICAL,HIGH'
    exit-code: '1'
```

```bash
# 隔离网络:手动下载 DB 并挂载
trivy image --download-db-only --cache-dir /path/to/cache
# 将缓存传输到隔离网络系统
trivy image --skip-db-update --cache-dir /path/to/cache myimage:tag
```

### 步骤 6:生成 SBOM 并扫描许可证合规性

使用 Trivy 在漏洞扫描的同时生成软件物料清单(SBOM)。

```bash
# 以 CycloneDX 格式生成 SBOM
trivy image --format cyclonedx --output sbom.cdx.json app:latest

# 以 SPDX 格式生成 SBOM
trivy image --format spdx-json --output sbom.spdx.json app:latest

# 扫描 SBOM 中的漏洞(将生成与扫描解耦)
trivy sbom sbom.cdx.json --severity CRITICAL,HIGH

# 带许可证检测的扫描
trivy image --scanners vuln,license --severity HIGH,CRITICAL app:latest
```

## 关键概念

| 术语 | 定义 |
|------|------------|
| CVE | 通用漏洞与披露 — 公开已知安全漏洞的标准化标识符 |
| 漏洞数据库 | Trivy 定期更新的数据库,汇聚来自 NVD、厂商公告和语言特定来源的 CVE 数据 |
| 错误配置 | Dockerfile、Kubernetes 清单或 IaC 模板中与安全相关的配置问题 |
| SBOM | 软件物料清单 — 容器镜像中所有组件和依赖项的完整清单 |
| 忽略未修复 | 跳过没有可用补丁的 CVE 的标志,减少无法采取行动漏洞带来的噪音 |
| VEX | 漏洞可利用性交换 — 关于漏洞在特定上下文中是否可被利用的机器可读声明 |
| 退出码 | Trivy 在发现超过严重性阈值时返回的非零状态码,用于使 CI/CD 流水线失败 |

## 工具与系统

- **Trivy**:Aqua Security 的开源漏洞扫描器,支持镜像、文件系统、仓库和 IaC
- **trivy-action**:在 GitHub Actions 工作流中运行 Trivy 扫描的官方 GitHub Action
- **Trivy Operator**:持续使用 Trivy 扫描集群工作负载的 Kubernetes 操作器
- **Grype**:Anchore 的备选镜像扫描器,用于对比和验证扫描结果
- **Harbor**:内置 Trivy 集成的容器仓库,推送时自动扫描镜像

## 常见场景

### 场景:多阶段构建,分离扫描与推送

**背景**:团队构建多阶段 Docker 镜像,需要在推送到 ECR 前扫描最终生产镜像,同时扫描构建阶段以防范供应链风险。

**方法**:
1. 使用 `--target production` 构建 Docker 镜像的最终阶段
2. 使用 `--severity CRITICAL,HIGH --exit-code 1 --ignore-unfixed` 运行 Trivy,阻断可利用的问题
3. 以 CycloneDX 格式生成 SBOM 并作为构建制品存储
4. 将 SARIF 结果上传到 GitHub Security 标签页以提升可见性
5. 仅在 Trivy 扫描以退出码 0 退出时才推送到 ECR
6. 以扫描时间戳和 Trivy DB 版本标记推送的镜像,便于审计追踪

**注意事项**:仅扫描最终阶段会遗漏构建阶段中存在的易受攻击包,这些包可能影响了构建过程。需单独对构建上下文运行 `trivy fs`。过度缓存 Trivy DB(每周一次)意味着新发布的 CVE 需要数天才能出现在扫描中。

## 输出格式

```
Trivy 容器扫描报告
=============================
镜像: app:a1b2c3d4
基础镜像: python:3.12-slim-bookworm
扫描日期: 2026-02-23
DB 版本: 2026-02-23T00:15:00Z

漏洞摘要:
  总计: 47
  严重: 2
  高危: 5
  中危: 18
  低危: 22
  未修复: 8 (已从门禁中排除)

严重发现:
  CVE-2025-12345  libssl3    3.0.11-1  3.0.13-1  OpenSSL 缓冲区溢出
  CVE-2025-67890  curl       7.88.1-10 7.88.1-12 curl HSTS 绕过

高危发现:
  CVE-2025-11111  zlib1g     1.2.13    1.2.13.1  zlib 堆缓冲区溢出
  CVE-2025-22222  python3.12 3.12.1    3.12.3    CPython 路径穿越
  CVE-2025-33333  requests   2.31.0    2.32.0    requests 重定向中的 SSRF

错误配置:
  DS002  [HIGH]   Dockerfile: 未设置 USER 指令(以 root 运行)
  DS026  [MEDIUM] Dockerfile: 未定义 HEALTHCHECK

质量门禁: 失败(2 个严重,5 个高危发现)
```

Related Skills

scanning-network-with-nmap-advanced

9
from killvxk/cybersecurity-skills-zh

使用 Nmap 的脚本引擎、时序控制、规避技术和输出解析,对授权目标网络执行高级网络侦察, 发现主机、枚举服务、检测漏洞并识别操作系统。

scanning-kubernetes-manifests-with-kubesec

9
from killvxk/cybersecurity-skills-zh

使用 Kubesec 对 Kubernetes 资源清单执行安全风险分析,识别错误配置、权限提升风险以及与安全最佳实践的偏差。

scanning-infrastructure-with-nessus

9
from killvxk/cybersecurity-skills-zh

Tenable Nessus 是业界领先的漏洞扫描器,用于识别网络基础设施(包括服务器、工作站、网络设备和操作系统)中的安全弱点。

scanning-docker-images-with-trivy

9
from killvxk/cybersecurity-skills-zh

Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规,并集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。

scanning-container-images-with-grype

9
from killvxk/cybersecurity-skills-zh

使用 Anchore Grype 扫描容器镜像的已知漏洞(Vulnerability),支持基于 SBOM 的匹配和可配置的严重性阈值。

performing-web-application-scanning-with-nikto

9
from killvxk/cybersecurity-skills-zh

Nikto 是一款开源 Web 服务器和 Web 应用程序扫描器,可针对超过 7,000 个潜在危险文件/程序进行测试,检查超过 1,250 个服务器的过期版本,并识别超过 270 个服务器的版本特定问题。

performing-vulnerability-scanning-with-nessus

9
from killvxk/cybersecurity-skills-zh

使用 Tenable Nessus 执行认证和未认证漏洞扫描,识别网络基础设施、服务器和应用程序中的已知漏洞、 错误配置、默认凭据和缺失补丁。扫描器将发现与 CVE 数据库和 CVSS 评分关联,生成优先级修复指导。 适用于漏洞扫描、Nessus 评估、补丁合规检查或自动化漏洞检测等请求场景。

performing-sca-dependency-scanning-with-snyk

9
from killvxk/cybersecurity-skills-zh

本技能涵盖使用 Snyk 实施软件成分分析(SCA, Software Composition Analysis),在 CI/CD 流水线中检测存在漏洞的开源依赖项。内容包括扫描包清单和锁文件、自动修复拉取请求生成、许可证合规检查、已部署应用程序的持续监控,以及与 GitHub、GitLab 和 Jenkins 流水线的集成。

performing-ot-vulnerability-scanning-safely

9
from killvxk/cybersecurity-skills-zh

使用被动监控、原生协议查询和经过精心控制的Tenable OT Security主动扫描,在OT/ICS环境中安全执行漏洞扫描,在不破坏工业过程或导致旧版控制器崩溃的情况下识别漏洞。

performing-container-security-scanning-with-trivy

9
from killvxk/cybersecurity-skills-zh

使用 Aqua Security Trivy 扫描容器镜像、文件系统和 Kubernetes 清单,检测漏洞(Vulnerability)、错误配置、暴露的密钥和许可证合规问题,并生成 SBOM(Software Bill of Materials,软件物料清单)及集成到 CI/CD 流水线。

performing-agentless-vulnerability-scanning

9
from killvxk/cybersecurity-skills-zh

配置并执行无代理漏洞扫描(agentless vulnerability scanning),利用网络协议、云快照分析和基于 API 的发现方式评估系统安全,无需在端点安装 Agent。

implementing-secrets-scanning-in-ci-cd

9
from killvxk/cybersecurity-skills-zh

将 gitleaks 和 trufflehog 集成到 CI/CD 管道中,在部署前检测泄露的机密信息