agentleFS
autheggauthegg/agent-skills/skills/docker-security-hardening/SKILL.md

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

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
  1. Distinctive:

    When the user mentions "container security", "Docker hardening", "image scanning", or "production readiness"

    line 18
  2. Very specific:

    Non-root USER directive: Every Dockerfile MUST include USER <non-root> before the final CMD/ENTRYPOINT.

    line 24
  3. Very specific:

    Slim base images only: NEVER use full base images in production (e.g., node:20, python:3.12).

    line 30
  4. Distinctive:

    No secrets in images: NEVER put passwords, API keys, tokens, or private keys in ENV, ARG, or COPY.

    line 58
  5. Distinctive:

    COPY over ADD: Prefer COPY; only use ADD for tar extraction.

    line 83
  6. Very specific:

    .dockerignore: Must exclude .git, .env, node_modules, *.key, *.pem, test files.

    line 85
  7. Very specific:

    No Docker socket mounts: NEVER mount /var/run/docker.sock into application containers.

    line 119
  8. Very specific:

    No privileged mode: Never use privileged: true.

    line 121
  9. Distinctive:

    Network isolation: Create separate frontend/backend networks:

    line 123
  10. Very specific:

    Dockerfile linting: Run hadolint Dockerfile in CI.

    line 133
  11. Distinctive:

    When reporting findings, classify as:

    line 212

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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)

  1. 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
  2. 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
  3. 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"]
  4. 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
  5. HEALTHCHECK directive: Include health checks for orchestrator integration:
    HEALTHCHECK --interval=30s --timeout=3s \
      CMD curl -f http://localhost:3000/health || exit 1
  6. Pin images by digest in production:
    # ❌ BAD: Mutable tag
    FROM node:20-slim
    
    # ✅ GOOD: Immutable digest
    FROM node@sha256:abc123def456...
  7. COPY over ADD: Prefer COPY; only use ADD for tar extraction.
  8. .dockerignore: Must exclude .git, .env, node_modules, *.key, *.pem, test files.

docker-compose / Runtime Requirements

  1. Drop all capabilities:
    cap_drop:
      - ALL
    cap_add:
      - NET_BIND_SERVICE  # Only what's needed
  2. Enable no-new-privileges:
    security_opt:
      - no-new-privileges:true
  3. Read-only filesystem:
    read_only: true
    tmpfs:
      - /tmp:noexec,nosuid
  4. Resource limits:
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
  5. No Docker socket mounts: NEVER mount /var/run/docker.sock into application containers.
  6. No privileged mode: Never use privileged: true.
  7. Network isolation: Create separate frontend/backend networks:
    networks:
      frontend:
      backend:
        internal: true

CI/CD Requirements

  1. Dockerfile linting: Run hadolint Dockerfile in CI.
  2. CVE scanning: Fail builds on CRITICAL/HIGH:
    trivy image --severity HIGH,CRITICAL --exit-code 1 <image>
  3. SBOM generation:
    syft <image> --output cyclonedx-json=sbom.json
  4. 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: true

Severity 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 pipeline
  • references/dockerignore-template.md — Production .dockerignore
  • scripts/dockerfile-security-check.sh — Automated Dockerfile security scanner
  • scripts/block-dangerous-docker-commands.sh — Dangerous Docker command blocker

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.