docker-security-hardening
Skill2 starsChanged 9 months agoMITView on GitHub
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".
- Reads credentials
- Deletes or force-pushes
In this repository4 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- ci-security-pipelineSkill
- kubernetes-pod-securitySkill
- secrets-managementSkill
The file
We split this skill into the rules it gives an agent
11 rules. About 0 are common, 5 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
When the user mentions "container security", "Docker hardening", "image scanning", or "production readiness"
- Very specific: line 24
Non-root USER directive: Every Dockerfile MUST include USER <non-root> before the final CMD/ENTRYPOINT.
- Very specific: line 30
Slim base images only: NEVER use full base images in production (e.g., node:20, python:3.12).
- Distinctive: line 58
No secrets in images: NEVER put passwords, API keys, tokens, or private keys in ENV, ARG, or COPY.
- Distinctive: line 83
COPY over ADD: Prefer COPY; only use ADD for tar extraction.
- Very specific: line 85
.dockerignore: Must exclude .git, .env, node_modules, *.key, *.pem, test files.
- Very specific: line 119
No Docker socket mounts: NEVER mount /var/run/docker.sock into application containers.
- Very specific: line 121
No privileged mode: Never use privileged: true.
- Distinctive: line 123
Network isolation: Create separate frontend/backend networks:
- Very specific: line 133
Dockerfile linting: Run hadolint Dockerfile in CI.
- Distinctive: line 212
When reporting findings, classify as:
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Docker Security Hardening
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.
When to Use
- Creating or editing any Dockerfile
- Writing or modifying docker-compose.yml / compose.yaml
- Configuring Kubernetes deployments with container specs
- Setting up CI/CD pipelines that build Docker images
- Reviewing container configurations before production deployment
- When the user mentions "container security", "Docker hardening", "image scanning", or "production readiness"
Security Checklist — Apply to Every Docker File
Dockerfile Requirements (Check All)
- 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.
docker-compose / Runtime Requirements
- 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.sockinto application containers. - No privileged mode: Never use
privileged: true. - Network isolation: Create separate frontend/backend networks:
networks: frontend: backend: internal: true
CI/CD Requirements
- Dockerfile linting: Run
hadolint Dockerfilein 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 .
Secure Templates
Complete Secure Node.js Dockerfile
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"]Complete Secure docker-compose Service
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: trueSeverity Classification
When reporting findings, classify as:
- CRITICAL (must block): Running as root + exposed, secrets in image, privileged mode, Docker socket mount
- HIGH (must fix before deploy): No USER directive, full base images in prod, no CVE scanning
- MEDIUM (should fix): No HEALTHCHECK, no resource limits, no .dockerignore
- LOW (best practice): Image not pinned by digest, no SBOM, missing labels
References
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 blocker
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

