implementing-container-image-minimal-base-with-distroless
通过在 Google distroless 基础镜像上构建应用镜像来减少容器攻击面,这些镜像仅包含应用运行时,没有 shell、包管理器或不必要的 OS 工具。
Best use case
implementing-container-image-minimal-base-with-distroless is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
通过在 Google distroless 基础镜像上构建应用镜像来减少容器攻击面,这些镜像仅包含应用运行时,没有 shell、包管理器或不必要的 OS 工具。
Teams using implementing-container-image-minimal-base-with-distroless 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
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/implementing-container-image-minimal-base-with-distroless/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How implementing-container-image-minimal-base-with-distroless Compares
| Feature / Agent | implementing-container-image-minimal-base-with-distroless | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
通过在 Google distroless 基础镜像上构建应用镜像来减少容器攻击面,这些镜像仅包含应用运行时,没有 shell、包管理器或不必要的 OS 工具。
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
# 使用 Distroless 实施容器镜像最小基础 ## 概述 Google distroless 镜像仅包含应用程序及其运行时依赖项,不包含包管理器、shell 或标准 Linux 发行版中的其他程序。通过消除不必要的 OS 组件,distroless 镜像与 ubuntu 或 debian 等传统基础镜像相比,攻击面减少高达 95%。Kubernetes 本身、Knative 和 Tekton 等主要项目在生产中使用 distroless 镜像。截至 2025 年,Docker 也提供了 Hardened Images (DHI) 作为最小容器基础的开源替代方案。 ## 前提条件 - Docker 20.10+ 或兼容的容器构建工具(Buildah、Kaniko) - 多阶段 Dockerfile 知识 - 应用程序编译为静态二进制文件或已捆绑运行时 - 用于镜像存储的容器仓库 ## 可用的 Distroless 镜像 | 镜像 | 使用场景 | 大小 | |------|---------|------| | `gcr.io/distroless/static-debian12` | 静态编译的二进制文件(Go、Rust) | ~2MB | | `gcr.io/distroless/base-debian12` | 需要 glibc 的动态链接二进制文件 | ~20MB | | `gcr.io/distroless/cc-debian12` | 需要 libstdc++ 的 C/C++ 应用 | ~25MB | | `gcr.io/distroless/java21-debian12` | Java 21 应用 | ~220MB | | `gcr.io/distroless/python3-debian12` | Python 3 应用 | ~50MB | | `gcr.io/distroless/nodejs22-debian12` | Node.js 22 应用 | ~130MB | ## 多阶段构建模式 ### Go 应用 ```dockerfile # 构建阶段 FROM golang:1.22-bookworm AS builder WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /server ./cmd/server # 运行阶段 - 静态 distroless FROM gcr.io/distroless/static-debian12:nonroot COPY --from=builder /server /server USER nonroot:nonroot ENTRYPOINT ["/server"] ``` ### Java 应用 ```dockerfile # 构建阶段 FROM maven:3.9-eclipse-temurin-21 AS builder WORKDIR /app COPY pom.xml . RUN mvn dependency:go-offline COPY src ./src RUN mvn package -DskipTests # 运行阶段 - Java distroless FROM gcr.io/distroless/java21-debian12:nonroot COPY --from=builder /app/target/app.jar /app.jar USER nonroot:nonroot ENTRYPOINT ["java", "-jar", "/app.jar"] ``` ### Python 应用 ```dockerfile # 构建阶段 FROM python:3.12-bookworm AS builder WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir --target=/deps -r requirements.txt COPY . . # 运行阶段 - Python distroless FROM gcr.io/distroless/python3-debian12:nonroot WORKDIR /app COPY --from=builder /deps /deps COPY --from=builder /app /app ENV PYTHONPATH=/deps USER nonroot:nonroot ENTRYPOINT ["python3", "/app/main.py"] ``` ### Node.js 应用 ```dockerfile # 构建阶段 FROM node:22-bookworm AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --production COPY . . # 运行阶段 - Node distroless FROM gcr.io/distroless/nodejs22-debian12:nonroot WORKDIR /app COPY --from=builder /app . USER nonroot:nonroot CMD ["server.js"] ``` ## 安全优势 ### 攻击面对比 | 组件 | Ubuntu | Alpine | Distroless | |------|--------|--------|-----------| | Shell(bash/sh) | 有 | 有 | 无 | | 包管理器 | apt | apk | 无 | | coreutils | 完整 | BusyBox | 无 | | curl/wget | 有 | 有 | 无 | | 用户管理 | 有 | 有 | 无 | | 已知 CVE(典型) | 50-200+ | 5-20 | 0-5 | | 镜像大小(基础) | ~77MB | ~7MB | ~2-20MB | ### 安全影响 - **无 shell**:攻击者无法进入容器执行命令 - **无包管理器**:无法安装额外工具或恶意软件 - **无 coreutils**:无 `cat`、`ls`、`find`、`curl` 可用于侦察 - **最少 CVE**:包越少,需要修补的漏洞越少 - **默认非 root**:`:nonroot` 标签以 UID 65534 运行 ## 调试 Distroless 容器 由于 distroless 没有 shell,使用以下技术进行调试: ### Debug 镜像变体 ```dockerfile # 仅在非生产环境中使用 debug 变体 FROM gcr.io/distroless/base-debian12:debug # 包含位于 /busybox/sh 的 busybox shell ``` ```bash # 进入 debug 变体 kubectl exec -it pod-name -- /busybox/sh ``` ### 临时调试容器(Kubernetes 1.25+) ```bash # 附加一个带完整工具的调试容器 kubectl debug -it pod-name --image=busybox:1.36 --target=app-container ``` ### 使用 Crane/Dive 进行镜像检查 ```bash # 不运行即可检查镜像层 crane export gcr.io/distroless/static-debian12 - | tar -tf - | head -50 # 分析镜像层 dive gcr.io/distroless/static-debian12 ``` ## 镜像扫描结果 使用 Trivy 的典型漏洞对比: ```bash # 扫描基于 Ubuntu 的镜像 trivy image myapp:ubuntu # 结果: 47 个漏洞(3 严重, 12 高危) # 扫描基于 Distroless 的镜像 trivy image myapp:distroless # 结果: 2 个漏洞(0 严重, 0 高危) ``` ## 参考资料 - [GoogleContainerTools/distroless GitHub](https://github.com/GoogleContainerTools/distroless) - [Distroless 镜像 - Docker 文档](https://docs.docker.com/dhi/core-concepts/distroless/) - [Alpine、Distroless 还是 Scratch?- Google Cloud](https://medium.com/google-cloud/alpine-distroless-or-scratch-caac35250e0b) - [Docker Hardened Images](https://www.infoq.com/news/2025/12/docker-hardened-images/)
Related Skills
securing-container-registry-with-harbor
Harbor 是一个开源容器镜像仓库,提供安全功能包括漏洞扫描(集成 Trivy)、镜像签名(Notary/Cosign)、RBAC、内容信任策略(Content Trust)、复制和审计日志。配置这些功能以强制执行镜像来源验证、防止有漏洞镜像部署并维护仓库访问控制。
securing-container-registry-images
通过使用 Trivy 和 Grype 实施漏洞扫描、使用 Cosign 和 Sigstore 强制执行镜像签名、配置镜像仓库访问控制,以及构建阻止部署未扫描或未签名镜像的 CI/CD 流水线,来保护容器仓库(Container Registry)中的镜像安全。
scanning-docker-images-with-trivy
Trivy 是 Aqua Security 开源的综合性漏洞扫描器,用于检测容器镜像中操作系统软件包、语言特定依赖项的漏洞、错误配置、密钥和许可证违规,并集成到 CI/CD 流水线,支持 SARIF、CycloneDX 和 SPDX 等多种输出格式。
scanning-containers-with-trivy-in-cicd
本技能涵盖将 Aqua Security 的 Trivy 扫描器集成到 CI/CD 流水线中,用于全面的容器镜像漏洞检测。包括扫描 Docker 镜像中的操作系统包和应用依赖 CVE、检测 Dockerfile 中的错误配置、扫描文件系统和 Git 仓库,以及建立基于严重性的质量门禁以阻止有漏洞的镜像部署。
scanning-container-images-with-grype
使用 Anchore Grype 扫描容器镜像的已知漏洞(Vulnerability),支持基于 SBOM 的匹配和可配置的严重性阈值。
performing-sqlite-database-forensics
对 SQLite 数据库执行取证分析,从空闲列表(Freelist)和 WAL 文件中恢复已删除记录,解码编码时间戳,并从浏览器历史、即时通讯应用和移动设备数据库中提取证据。
performing-container-security-scanning-with-trivy
使用 Aqua Security Trivy 扫描容器镜像、文件系统和 Kubernetes 清单,检测漏洞(Vulnerability)、错误配置、暴露的密钥和许可证合规问题,并生成 SBOM(Software Bill of Materials,软件物料清单)及集成到 CI/CD 流水线。
performing-container-image-hardening
本技能涵盖通过最小化攻击面、移除不必要软件包、实施多阶段构建、配置非 root 用户, 以及应用 CIS Docker 基准建议来加固容器镜像,生成安全的生产就绪镜像。
performing-container-escape-detection
通过分析命名空间配置、特权容器检查、危险能力分配和宿主机路径挂载,使用 kubernetes Python 客户端检测容器逃逸尝试。识别通过 cgroup 滥用的 CVE-2022-0492 类型逃逸。 适用于审计容器安全态势或调查逃逸尝试。
implementing-zero-trust-with-hashicorp-boundary
使用 HashiCorp Boundary 实现具备动态凭据代理、会话录制和 Vault 集成的身份感知零信任基础设施访问管理。
implementing-zero-trust-with-beyondcorp
使用身份感知代理(IAP,Identity-Aware Proxy)、上下文感知访问策略、设备信任验证和 Access Context Manager,部署 Google BeyondCorp Enterprise 零信任访问控制,对 GCP 资源和内部应用强制执行基于身份和安全态势的访问。
implementing-zero-trust-network-access
通过配置身份感知代理、微分段、基于条件访问策略的持续验证,以及在 AWS、Azure 和 GCP 环境中以 BeyondCorp 风格的架构替代传统 VPN 访问,在云环境中实施零信任网络访问(ZTNA)。