skills/docker-security-hardening/SKILL.md
Enforce Docker container security best practices during development. Use when creating or editing Dockerfiles, docker-compose files, Kubernetes manifests, or CI/CD pipelines involving containers. Covers non-root users, slim base images, multi-stage builds, CVE scanning with Trivy, secrets management, capability dropping, network isolation, SBOM generation, and production readiness gates. Activates on keywords like "Dockerfile", "docker-compose", "container security", "image hardening", "Docker deploy", or "production readiness".
npx skillsauth add authegg/agent-skills docker-security-hardeningInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Enforce Docker and container security best practices based on Bret Fisher's container security recommendations, the OWASP Docker Security Cheat Sheet, and CIS Docker Benchmark.
Non-root USER directive: Every Dockerfile MUST include USER <non-root> before the final CMD/ENTRYPOINT. Use existing users from official images (e.g., node, postgres) or create a dedicated one:
RUN addgroup --system appgroup && adduser --system --ingroup appgroup appuser
USER appuser
Slim base images only: NEVER use full base images in production (e.g., node:20, python:3.12). Always use -slim, -alpine, or distroless:
# ❌ BAD: Full image with unnecessary packages
FROM node:20
# ✅ GOOD: Minimal attack surface
FROM node:20-slim
FROM gcr.io/distroless/nodejs
FROM cgr.dev/chainguard/node
Multi-stage builds: Separate build and runtime stages. Dev dependencies, build tools, and test frameworks must NOT appear in the final stage:
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-slim AS production
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER node
CMD ["node", "dist/index.js"]
No secrets in images: NEVER put passwords, API keys, tokens, or private keys in ENV, ARG, or COPY. Use BuildKit secrets or runtime injection:
# ❌ BAD
ENV DATABASE_PASSWORD=mysecret
ARG API_KEY
# ✅ GOOD: BuildKit secret mount
RUN --mount=type=secret,id=db_pass cat /run/secrets/db_pass
HEALTHCHECK directive: Include health checks for orchestrator integration:
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/health || exit 1
Pin images by digest in production:
# ❌ BAD: Mutable tag
FROM node:20-slim
# ✅ GOOD: Immutable digest
FROM node@sha256:abc123def456...
COPY over ADD: Prefer COPY; only use ADD for tar extraction.
.dockerignore: Must exclude .git, .env, node_modules, *.key, *.pem, test files.
Drop all capabilities:
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE # Only what's needed
Enable no-new-privileges:
security_opt:
- no-new-privileges:true
Read-only filesystem:
read_only: true
tmpfs:
- /tmp:noexec,nosuid
Resource limits:
deploy:
resources:
limits:
memory: 512M
cpus: '0.5'
No Docker socket mounts: NEVER mount /var/run/docker.sock into application containers.
No privileged mode: Never use privileged: true.
Network isolation: Create separate frontend/backend networks:
networks:
frontend:
backend:
internal: true
Dockerfile linting: Run hadolint Dockerfile in CI.
CVE scanning: Fail builds on CRITICAL/HIGH:
trivy image --severity HIGH,CRITICAL --exit-code 1 <image>
SBOM generation:
syft <image> --output cyclonedx-json=sbom.json
Secret scanning:
trivy fs --scanners secret .
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force
COPY . .
RUN npm run build
FROM node:20-slim AS production
RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/package.json ./
HEALTHCHECK --interval=30s --timeout=3s \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]
services:
app:
build:
context: .
target: production
user: "1000:1000"
read_only: true
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
tmpfs:
- /tmp:noexec,nosuid
deploy:
resources:
limits:
memory: 512M
cpus: '0.5'
networks:
- backend
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 5s
retries: 3
networks:
frontend:
backend:
internal: true
When reporting findings, classify as:
For detailed hook scripts, CI pipeline templates, and the complete .dockerignore template, see the references/ and scripts/ directories in this skill.
references/ci-pipeline-template.md — GitHub Actions security pipelinereferences/dockerignore-template.md — Production .dockerignorescripts/dockerfile-security-check.sh — Automated Dockerfile security scannerscripts/block-dangerous-docker-commands.sh — Dangerous Docker command blockerdevelopment
Enforce secrets management best practices for containers and cloud-native applications. Use when configuring environment variables, Docker secrets, Kubernetes secrets, Vault integration, AWS SSM/Secrets Manager, or any credential handling in Dockerfiles, compose files, Kubernetes manifests, or CI pipelines. Activates on keywords like "secrets", "credentials", "API key", "password", "environment variables", ".env file", "Vault", "sealed secrets", or "secret manager".
testing
Enforce Kubernetes pod and workload security best practices. Use when creating or editing Kubernetes manifests, Helm charts, or Kustomize overlays involving pods, deployments, statefulsets, daemonsets, jobs, or cronjobs. Covers Pod Security Standards (Restricted), SecurityContext hardening, RBAC least privilege, network policies, resource quotas, and admission control. Activates on keywords like "pod security", "K8s manifest", "deployment.yaml", "Helm chart", "securityContext", or "RBAC".
development
Generate and enforce security scanning stages in CI/CD pipelines. Use when creating or editing GitHub Actions workflows, GitLab CI, CircleCI, Jenkins, or any CI pipeline that builds Docker images or deploys containers. Covers Dockerfile linting with Hadolint, CVE scanning with Trivy, secret detection, SBOM generation, image signing, and deployment gates. Activates on keywords like "CI pipeline", "GitHub Actions", "security scanning", "Trivy", "Hadolint", "SBOM", or "deploy gate".
development
Maintainer-only workflow for handling GitHub Secret Scanning alerts on OpenClaw. Use when Codex needs to triage, redact, clean up, and resolve secret leakage found in issue comments, issue bodies, PR comments, or other GitHub content.