关于 docker-security
docker-security 是面向 Docker 使用者的安全加固指南,覆盖镜像构建、运行时、密钥和主机审计等环节,并提供配置示例与检查清单。
主要功能
- 01镜像与 Dockerfile 加固
- 02运行时权限限制
- 03BuildKit 与 Docker secrets
- 04Docker Bench 安全审计
- 05安全加固流程与常见陷阱
适用场景
- 01将容器配置为非 root 运行
- 02限制容器能力并启用只读文件系统
- 03在构建或运行时注入密钥
- 04依据 CIS 检查 Docker 配置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ full-stack-skills/ docker-skills --skill docker-security安装后在 Claude Code 中输入/docker-security确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
agents/openai.yamlexamples/01-secure-dockerfile.md02-docker-bench.mdreferences/01-capabilities.md02-cis-checklist.md03-secrets-management.md详细介绍
Docker 安全 — 安全加固与防护
全面介绍如何在整个生命周期中保障 Docker 安全。
使用时机
用户提到以下内容时,务必使用此技能:
- “docker 安全”、“镜像安全”、“容器安全”
- “非 root 运行”、“rootless”、“最小权限”
- “seccomp”、“AppArmor”、“SELinux”
- “Docker Bench”、“CIS”
- “密钥管理”
- “镜像签名”、“内容信任”
安全模型 — 纵深防御
Layer 1: Image Security — minimal base, non-root, pinned versions
Layer 2: Build Security — secret injection, noCOPY secrets
Layer 3: Runtime Security — capabilities, seccomp, AppArmor, read-only
Layer 4: Registry Security — content trust, signing, vulnerability scan
Layer 5: Host Security — Docker Bench, CIS, user namespace
镜像安全检查清单
| # | 实践 | 方法 |
|---|---|---|
| 1 | 最小化基础镜像 | 使用 alpine 或 distroless(Go → scratch) |
| 2 | 使用非 root 用户 | 在 Dockerfile 末尾添加 USER 1000:1000 |
| 3 | 固定版本 | 使用 FROM alpine:3.20@sha256:...,而非 alpine:latest |
| 4 | 优先使用 COPY,而非 ADD | ADD 会自动解压 tar,可能产生意外行为 |
| 5 | 镜像中不包含密钥 | 使用 --secret 或在运行时注入 |
| 6 | .dockerignore | 排除 .env、.git 和凭据 |
安全的 Dockerfile
FROM alpine:3.20
RUN apk add --no-cache ca-certificates
RUN addgroup -S app && adduser -S -G app app
COPY --chown=app:app ./app /app
WORKDIR /app
USER app
CMD ["./server"]
运行时安全
功能(最小权限)
# Drop ALL capabilities, add only what's needed
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
# Common needed caps: NET_BIND_SERVICE, CHOWN, DAC_OVERRIDE
# NEVER: --privileged (gives full host access)
只读根文件系统
# Prevents container from writing anywhere (except volumes/tmpfs)
docker run --read-only --tmpfs /tmp --tmpfs /run nginx
Seccomp 配置文件
# Custom seccomp profile (block syscalls)
docker run --security-opt seccomp=profile.json app
# Unconfined (NEVER in production)
docker run --security-opt seccomp=unconfined app
禁止提升权限
# Prevent privilege escalation via setuid binaries
docker run --security-opt no-new-privileges app
密钥管理
BuildKit 密钥(构建时)
# syntax=docker/dockerfile:1
FROM alpine
RUN --mount=type=secret,id=aws_creds \
AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_creds) \
aws s3 cp s3://bucket/file .
docker build --secret id=aws_creds,src=$HOME/.aws/credentials -t app .
Docker 密钥(Swarm 运行时)
echo "mysecretpassword" | docker secret create db_password -
docker service create --secret db_password postgres
Docker Bench 安全审计
docker run --rm \
--pid host --network host \
-v /var/run/docker.sock:/var/run/docker.sock \
docker/docker-bench-security
工作流 — 安全加固流程
步骤 1:镜像安全:使用 docker scout cves <image> 扫描漏洞 → 更新基础镜像/依赖项
步骤 2:加固 Dockerfile:USER 使用非 root 用户、优先使用 COPY 而非 ADD、固定 digest
步骤 3:运行时安全:--read-only、--cap-drop=ALL、--security-opt no-new-privileges
步骤 4:审计检查:运行 docker run --rm docker/docker-bench-security 并逐项修复
步骤 5:CI 门禁:通过 Scout 策略阻止合并含 critical/high CVE 的变更
注意事项 — 常见陷阱
--privileged标志:会授予对主机的完全访问权限。切勿在生产环境中使用。→ 解决方法:改用特定的--cap-add=NET_BIND_SERVICE;使用docker inspect --format='{{.HostConfig.Privileged}}'进行审计。- Dockerfile 中的 API 密钥:
ENV API_KEY=xxx会永久写入镜像层。→ 解决方法:在运行时注入:使用docker run -e API_KEY=$KEY或 BuildKit--mount=type=secret。 - 以 root 身份运行容器:默认用户是 root。容器逃逸意味着可在主机上获得 root 权限。→ 解决方法:始终在 Dockerfile 中使用
USER 1000:1000;使用docker exec myapp whoami验证。 - 挂载 Docker 套接字:
-v /var/run/docker.sock会让容器控制所有容器。→ 解决方法:使用带 TLS 身份验证的 Docker API,而不是挂载套接字;切勿在生产环境中挂载套接字。 - 忽略 CVE 修复:运行
docker scout后却从不修复发现的问题。→ 解决方法:设置 CI 策略,阻止存在 critical/high CVE 的变更;定期更新基础镜像。
边界 — 能力范围(适用与不适用场景)
| 分类 | 场景 | 说明 |
|---|---|---|
| ✅ 能做 | Dockerfile 安全加固 | USER 使用非 root 用户、优先使用 COPY 而非 ADD、固定 digest |
| ✅ 能做 | 运行时安全配置 | seccomp/AppArmor/capabilities/只读 |
| ✅ 能做 | 密钥管理 | Docker secrets + BuildKit --secret + Vault |
| ✅ 能做 | 安全审计(Docker Bench Security) | CIS 检查清单 + 逐项修复 |
| ⚠️ 需条件 | 镜像签名(Notary) | 需要设置环境变量 DOCKER_CONTENT_TRUST=1 |
| ⚠️ 需条件 | 完整合规检查 | 需要结合 Scout 扫描和组织安全策略 |
| ❌ 超范围 | CVE 漏洞扫描 | 使用 docker-scout |
| ❌ 超范围 | 主机系统安全 | 操作系统层级 |
| ❌ 超范围 | 网络安全/防火墙 | 属于网络管理员职责范围 |
不要使用此技能的情况
| ❌ 跳过 | ✅ 改用 |
|---|---|
| 漏洞扫描 | docker-scout |
| Docker 基础知识 | docker-basics |
| 生产环境部署 | docker-production |
| Registry 管理 | docker-hub |
安全与稳定性
- 所有安全实践均基于 CIS Docker Benchmark 和 Docker 官方安全指南。
- 在 CI/CD 中定期运行 Docker Bench Security,以检测配置偏差。
- 订阅 Docker 安全公告,及时获取 CVE 通知。
- 未附带可执行脚本。仅提供指南。
📚 官方文档参考
| 文档 | 地址 |
|---|---|
| Docker 安全 | https://docs.docker.com/security/ |
| Docker Hardened Images | https://docs.docker.com/dhi/ |
| Docker Scout | https://docs.docker.com/scout/ |
| Docker Bench Security | https://docs.docker.com/engine/security/bench/ |
| seccomp 配置 | https://docs.docker.com/engine/security/seccomp/ |
| AppArmor | https://docs.docker.com/engine/security/apparmor/ |
🧭 Docker Skills Journey
📍 您正在这里:
docker-security— 安全加固
← 上一步:docker-build | → 下一步:docker-scout / docker-cicd
评论
支持 Markdown