Loading...
Loading...
Guidance for Docker security hardening across the full lifecycle — image security (minimal base images, non-root users, pinned versions), runtime security (seccomp/AppArmor/SELinux, capabilities, read-only rootfs, no-new-privileges), secrets management (BuildKit secrets, Docker secrets in Swarm), Docker Bench Security auditing, CIS compliance checklist, and supply-chain security (image signing, content trust, SBOM). Use when the user asks about Docker security, image hardening, non-root containers, seccomp, AppArmor, Docker Bench, secrets management, or securing containers. 使用场景:docker 安全、镜像安全、非root运行、seccomp、AppArmor、security、Docker Bench、secret管理.
npx skill4agent add full-stack-skills/docker-skills docker-securityLayer 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| # | 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 |
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"]# 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# 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# syntax=docker/dockerfile:1
FROM alpine
RUN \
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 .echo "mysecretpassword" | docker secret create db_password -
docker service create --secret db_password postgresdocker run --rm \
--pid host --network host \
-v /var/run/docker.sock:/var/run/docker.sock \
docker/docker-bench-securitydocker scout cves <image>docker run --rm docker/docker-bench-security--privileged--cap-add=NET_BIND_SERVICEdocker inspect --format='{{.HostConfig.Privileged}}'ENV API_KEY=xxxdocker run -e API_KEY=$KEY--mount=type=secretUSER 1000:1000docker exec myapp whoami-v /var/run/docker.sockdocker scout| 分类 | 场景 | 说明 |
|---|---|---|
| ✅ 能做 | Dockerfile 安全加固 | USER 非 root、COPY 优先 ADD、digest 固定 |
| ✅ 能做 | 运行时安全配置 | seccomp/AppArmor/capabilities/read-only |
| ✅ 能做 | Secrets 管理 | Docker secrets + BuildKit --secret + Vault |
| ✅ 能做 | 安全审计(Docker Bench Security) | CIS 检查清单 + 逐条修复 |
| ⚠️ 需条件 | 镜像签名(Notary) | 需 |
| ⚠️ 需条件 | 完整合规检查 | 需结合 Scout 扫描 + 组织安全策略 |
| ❌ 超范围 | CVE 漏洞扫描 | 使用 |
| ❌ 超范围 | 主机系统安全 | 操作系统层级 |
| ❌ 超范围 | 网络安全/防火墙 | 网络管理员范畴 |
| ❌ Skip | ✅ Use Instead |
|---|---|
| Vulnerability scanning | |
| Docker basics | |
| Production deployment | |
| Registry management | |
| 文档 | 地址 |
|---|---|
| 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/ |
📍 You are here:— 安全加固docker-security
docker-builddocker-scoutdocker-cicd