docker-security
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker Security — 安全加固与防护
Docker Security — 安全加固与防护
Comprehensive guidance for securing Docker across the full lifecycle.
Docker全生命周期安全防护综合指南。
When to Use
使用场景
ALWAYS use this skill when the user mentions:
- "docker 安全", "镜像安全", "container security"
- "非root运行", "rootless", "least privilege"
- "seccomp", "AppArmor", "SELinux"
- "Docker Bench", "CIS"
- "secrets management", "密钥管理"
- "image signing", "content trust"
当用户提及以下内容时,务必使用本技能:
- "docker 安全"、"镜像安全"、"container security"
- "非root运行"、"rootless"、"最小权限"
- "seccomp"、"AppArmor"、"SELinux"
- "Docker Bench"、"CIS"
- "secrets management"、"密钥管理"
- "image signing"、"内容信任"
Security Model — Layered Defense
安全模型 — 分层防御
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 namespaceLayer 1: Image Security — 最小化基础镜像、非root用户、固定版本
Layer 2: Build Security — 密钥注入、禁止COPY密钥
Layer 3: Runtime Security — 权限能力、seccomp、AppArmor、只读模式
Layer 4: Registry Security — 内容信任、签名、漏洞扫描
Layer 5: Host Security — Docker Bench、CIS、用户命名空间Image Security Checklist
镜像安全检查清单
| # | Practice | How |
|---|---|---|
| 1 | Minimal base image | Use |
| 2 | Non-root user | |
| 3 | Pin versions | |
| 4 | COPY over ADD | ADD auto-extracts tar — unexpected behavior |
| 5 | No secrets in image | Use |
| 6 | | Exclude |
| # | 实践要点 | 操作方式 |
|---|---|---|
| 1 | 最小化基础镜像 | 使用 |
| 2 | 非root用户运行 | 在Dockerfile末尾添加 |
| 3 | 固定版本 | 使用 |
| 4 | 优先使用COPY而非ADD | ADD会自动解压tar包,可能导致意外行为 |
| 5 | 禁止在镜像中存储密钥 | 使用 |
| 6 | 配置 | 排除 |
Secure Dockerfile
安全的Dockerfile示例
dockerfile
FROM alpine:3.20
RUN apk add --no-cache ca-certificates
RUN addgroup -S app && adduser -S -G app app
COPY ./app /app
WORKDIR /app
USER app
CMD ["./server"]dockerfile
FROM alpine:3.20
RUN apk add --no-cache ca-certificates
RUN addgroup -S app && adduser -S -G app app
COPY ./app /app
WORKDIR /app
USER app
CMD ["./server"]Runtime Security
运行时安全
Capabilities (Least Privilege)
权限能力(最小权限原则)
bash
undefinedbash
undefinedDrop ALL capabilities, add only what's needed
移除所有权限,仅添加所需权限
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE nginx
Common needed caps: NET_BIND_SERVICE, CHOWN, DAC_OVERRIDE
常见所需权限:NET_BIND_SERVICE、CHOWN、DAC_OVERRIDE
NEVER: --privileged (gives full host access)
绝对禁止:--privileged(获取主机完整权限)
undefinedundefinedRead-Only Root Filesystem
只读根文件系统
bash
undefinedbash
undefinedPrevents container from writing anywhere (except volumes/tmpfs)
阻止容器写入任何位置(卷/tmpfs除外)
docker run --read-only --tmpfs /tmp --tmpfs /run nginx
undefineddocker run --read-only --tmpfs /tmp --tmpfs /run nginx
undefinedSeccomp Profile
Seccomp配置文件
bash
undefinedbash
undefinedCustom seccomp profile (block syscalls)
自定义seccomp配置文件(阻止特定系统调用)
docker run --security-opt seccomp=profile.json app
docker run --security-opt seccomp=profile.json app
Unconfined (NEVER in production)
无限制模式(生产环境绝对禁止)
docker run --security-opt seccomp=unconfined app
undefineddocker run --security-opt seccomp=unconfined app
undefinedNo New Privileges
禁止提升新权限
bash
undefinedbash
undefinedPrevent privilege escalation via setuid binaries
防止通过setuid二进制文件提升权限
docker run --security-opt no-new-privileges app
undefineddocker run --security-opt no-new-privileges app
undefinedSecrets Management
密钥管理
BuildKit Secrets (build-time)
BuildKit密钥(构建阶段)
dockerfile
undefineddockerfile
undefinedsyntax=docker/dockerfile:1
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 .
AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_creds)
aws s3 cp s3://bucket/file .
```bash
docker build --secret id=aws_creds,src=$HOME/.aws/credentials -t app .FROM alpine
RUN --mount=type=secret,id=aws_creds
AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_creds)
aws s3 cp s3://bucket/file .
AWS_ACCESS_KEY_ID=$(cat /run/secrets/aws_creds)
aws s3 cp s3://bucket/file .
```bash
docker build --secret id=aws_creds,src=$HOME/.aws/credentials -t app .Docker Secrets (Swarm runtime)
Docker密钥(Swarm运行阶段)
bash
echo "mysecretpassword" | docker secret create db_password -
docker service create --secret db_password postgresbash
echo "mysecretpassword" | docker secret create db_password -
docker service create --secret db_password postgresDocker Bench Security Audit
Docker Bench Security审计
bash
docker run --rm \
--pid host --network host \
-v /var/run/docker.sock:/var/run/docker.sock \
docker/docker-bench-securitybash
docker run --rm \
--pid host --network host \
-v /var/run/docker.sock:/var/run/docker.sock \
docker/docker-bench-securityWorkflow — 安全加固流程
工作流程 — 安全加固流程
Step 1: 镜像安全: 扫描漏洞 → 更新基础镜像/依赖
Step 2: Dockerfile 加固: USER 非 root、COPY 优于 ADD、固定 digest
Step 3: 运行时安全: --read-only、--cap-drop=ALL、--security-opt no-new-privileges
Step 4: 审计检查: 逐条修复
Step 5: CI 门禁: Scout 策略阻断 critical/high CVE 合并
docker scout cves <image>docker run --rm docker/docker-bench-securityStep 1: 镜像安全: 使用 扫描漏洞 → 更新基础镜像/依赖
Step 2: Dockerfile加固: 设置USER为非root用户、优先使用COPY、固定digest
Step 3: 运行时安全: 启用--read-only、--cap-drop=ALL、--security-opt no-new-privileges
Step 4: 审计检查: 运行 并逐条修复问题
Step 5: CI门禁: 通过Scout策略阻断critical/high级别的CVE合并
docker scout cves <image>docker run --rm docker/docker-bench-securityGotchas — Common Pitfalls
常见陷阱
- flag: Gives full host access. Never use in production. → Recovery: Use specific
--privilegedinstead; audit with--cap-add=NET_BIND_SERVICE.docker inspect --format='{{.HostConfig.Privileged}}' - API keys in Dockerfile: is baked into image layers forever. → Recovery: Use runtime injection:
ENV API_KEY=xxxor BuildKitdocker run -e API_KEY=$KEY.--mount=type=secret - Root container: Default user is root. Escaping the container means root on the host. → Recovery: Always in Dockerfile; verify with
USER 1000:1000.docker exec myapp whoami - Docker socket mount: gives container control over ALL containers. → Recovery: Use Docker API with TLS auth instead of socket mount; never mount socket in production.
-v /var/run/docker.sock - Ignoring CVE remediation: Running but never fixing findings. → Recovery: Set CI policy to block critical/high CVEs; update base images regularly.
docker scout
- 参数: 获取主机完整权限,生产环境绝对禁止使用。→ 修复方案: 使用特定权限如
--privileged替代;通过--cap-add=NET_BIND_SERVICE进行审计。docker inspect --format='{{.HostConfig.Privileged}}' - Dockerfile中存储API密钥: 会永久嵌入镜像层。→ 修复方案: 使用运行时注入:
ENV API_KEY=xxx或BuildKit的docker run -e API_KEY=$KEY。--mount=type=secret - Root容器: 默认用户为root,容器逃逸后将获得主机root权限。→ 修复方案: 始终在Dockerfile中添加;通过
USER 1000:1000验证。docker exec myapp whoami - 挂载Docker套接字: 会让容器获得所有容器的控制权。→ 修复方案: 使用带TLS认证的Docker API替代套接字挂载;生产环境绝不挂载套接字。
-v /var/run/docker.sock - 忽略CVE修复: 运行但不修复问题。→ 修复方案: 设置CI策略阻断critical/high级别的CVE;定期更新基础镜像。
docker scout
Boundary — 能力边界(适用与不适用场景)
能力边界(适用与不适用场景)
| 分类 | 场景 | 说明 |
|---|---|---|
| ✅ 能做 | Dockerfile 安全加固 | USER 非 root、COPY 优先 ADD、digest 固定 |
| ✅ 能做 | 运行时安全配置 | seccomp/AppArmor/capabilities/read-only |
| ✅ 能做 | Secrets 管理 | Docker secrets + BuildKit --secret + Vault |
| ✅ 能做 | 安全审计(Docker Bench Security) | CIS 检查清单 + 逐条修复 |
| ⚠️ 需条件 | 镜像签名(Notary) | 需 |
| ⚠️ 需条件 | 完整合规检查 | 需结合 Scout 扫描 + 组织安全策略 |
| ❌ 超范围 | CVE 漏洞扫描 | 使用 |
| ❌ 超范围 | 主机系统安全 | 操作系统层级 |
| ❌ 超范围 | 网络安全/防火墙 | 网络管理员范畴 |
| 分类 | 场景 | 说明 |
|---|---|---|
| ✅ 能做 | Dockerfile安全加固 | 设置USER为非root、优先使用COPY、固定digest |
| ✅ 能做 | 运行时安全配置 | seccomp/AppArmor/权限能力/只读模式 |
| ✅ 能做 | 密钥管理 | Docker密钥 + BuildKit --secret + Vault |
| ✅ 能做 | 安全审计(Docker Bench Security) | CIS检查清单 + 逐条修复 |
| ⚠️ 需条件 | 镜像签名(Notary) | 需要设置 |
| ⚠️ 需条件 | 完整合规检查 | 需要结合Scout扫描 + 组织安全策略 |
| ❌ 超范围 | CVE漏洞扫描 | 使用 |
| ❌ 超范围 | 主机系统安全 | 属于操作系统层级范畴 |
| ❌ 超范围 | 网络安全/防火墙 | 属于网络管理员范畴 |
When NOT to Use This Skill
不适用本技能的场景
| ❌ Skip | ✅ Use Instead |
|---|---|
| Vulnerability scanning | |
| Docker basics | |
| Production deployment | |
| Registry management | |
| ❌ 跳过 | ✅ 改用以下技能 |
|---|---|
| 漏洞扫描 | |
| Docker基础操作 | |
| 生产环境部署 | |
| 镜像仓库管理 | |
Security & Stability
安全性与稳定性
- All security practices are based on CIS Docker Benchmark and Docker official security guidance.
- Run Docker Bench Security regularly in CI/CD to detect configuration drift.
- Subscribe to Docker security advisories for CVE notifications.
- No executable scripts bundled. Guidance only.
- 所有安全实践均基于CIS Docker基准和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安全 | https://docs.docker.com/security/ |
| Docker加固镜像 | 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技能路线
📍 You are here:— 安全加固docker-security
← Previous: | → Next: /
docker-builddocker-scoutdocker-cicd📍 当前位置:— 安全加固docker-security
← 上一步: | → 下一步: /
docker-builddocker-scoutdocker-cicd