/devsecops-skills:container-image-hardeningfrom devsecops-skills
Guides building minimal, non-root, hardened container images using multi-stage builds, distroless base images, read-only root filesystems, dropped Linux capabilities, and digest-pinned base images. Use when asked to harden a Dockerfile, reduce attack surface, run as non-root, or set Kubernetes securityContext restrictions.
Ships in devsecops-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A container image built from a full general-purpose base image (e.g. a default Ubuntu or Debian image with a shell, package manager, and dozens of unrelated utilities) running as root gives an attacker who compromises the application inside it a much larger toolkit and a much easier path to container breakout or lateral movement than a minimal, non-root image with no shell and only the application's actual runtime dependencies. Container image hardening is about shrinking that attack surface at build time: using multi-stage builds so build tooling never ships in the final image, choosing minimal or distroless base images, running as a non-root user, making the root filesystem read-only, dropping unnecessary Linux capabilities, and pinning base images by digest so "the same image" can't silently change out from under you. None of this replaces vulnerability scanning of what is in the image — it reduces what's there to scan and what's exploitable if a vulnerability is found.
securityContext fields (runAsNonRoot,
readOnlyRootFilesystem, allowPrivilegeEscalation: false, dropped
capabilities) consistently across workloads.18.09+, and
in essentially all current tooling).gcr.io/distroless/*) — no shell, no package
manager, only the language runtime and its direct OS-level
dependencies; smallest attack surface, but harder to debug (no
exec-into-shell) and requires a compatible language runtime image
per stack (distroless/java, distroless/nodejs, distroless/cc
for compiled binaries with a C library, distroless/static for
fully static binaries e.g. Go with CGO disabled).apk package manager), widely used,
but musl's subtle differences from glibc occasionally break
compiled dependencies (e.g. some Python/Node native modules); still
ships a shell and package manager unless explicitly stripped.-slim variants (e.g. python:3.12-slim, node:20-slim) — a
reasonable middle ground: much smaller than the full image, glibc-based
(fewer compatibility surprises than Alpine), still has a shell.ubuntu, debian, default python:3.12)
— largest attack surface and image size; rarely justified for a
final runtime image, sometimes needed as a build stage.securityContext) with
admission-policy enforcement optional but recommended — see
policy-as-code-guardrails to
make these properties mandatory rather than best-effort.Split the build into stages, so build-only tooling (compilers,
dev package variants, source code not needed at runtime) never
reaches the final image:
# syntax=docker/dockerfile:1
FROM golang:1.22 AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
Pin the base image by digest, not just tag, for anything you consider "the same" across rebuilds — tags are mutable and can be repointed by the upstream maintainer:
FROM python:3.12-slim@sha256:<digest> AS base
Balance this against staying current on security patches: pinning by digest requires an explicit, deliberate bump process (e.g. a Renovate/Dependabot rule that tracks base-image digests) rather than silently freezing on a vulnerable base forever.
Run as a non-root user explicitly — don't rely on a base image's
default (distroless/*-nonroot variants set this for you; for others,
create and switch to a dedicated user):
RUN addgroup --system app && adduser --system --ingroup app app
USER app:app
Set a read-only root filesystem at deploy time, with an explicit writable volume only where genuinely needed (temp files, cache):
# Kubernetes pod securityContext
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
volumes:
- name: tmp
emptyDir: {}
volumeMounts:
- name: tmp
mountPath: /tmp
Drop all Linux capabilities and add back only what's required — most application containers need none of the default capability set:
securityContext:
capabilities:
drop: ["ALL"]
# add: ["NET_BIND_SERVICE"] # only if binding to a port < 1024
Remove build-time secrets and unnecessary files from the final
image — use BuildKit secret mounts for anything credential-like during
build (see secrets-management), and
a .dockerignore to keep .git, local env files, and test fixtures
out of the build context entirely:
# .dockerignore
.git
.env*
**/__pycache__
node_modules
*.md
Scan the built image, not just the Dockerfile, since the final image is what actually ships:
trivy image --severity CRITICAL,HIGH myorg/myapp:1.4.2
See software-composition-analysis-sca for the full scanning workflow.
Verify hardening holds at runtime, not just at build time — a
Dockerfile with USER app can still be overridden by a Kubernetes pod
spec with runAsUser: 0; enforce the deploy-time properties via an
admission policy (see
policy-as-code-guardrails)
so the Dockerfile's intent can't be silently undone at deploy time.
-slim glibc-based image for interpreted
languages with native extensions where Alpine's musl libc causes
friction, Alpine where compatibility is confirmed and size matters
most.:latest drifts unpredictably, but
a digest pinned once and never revisited silently accumulates
unpatched base-image CVEs, which is not obviously better.runAsUser: 0, capabilities added back, writable root
filesystem) by pod spec alone, and only cluster-side enforcement
guarantees the properties actually hold at runtime.kubectl debug with an ephemeral
container attached to the running pod — rather than discovering during
an incident that you can't exec in at all.Symptom: Switching to a distroless base image breaks the ability to
kubectl exec -it into the container to debug an issue.
Fix: This is expected — distroless images have no shell by design.
Use kubectl debug <pod> -it --image=busybox --target=<container> to
attach an ephemeral debug container with a shell to the running pod's
namespaces instead of relying on exec-into-the-app-container, or
maintain a separate -debug image tag with a shell for troubleshooting
builds specifically.
Symptom: An Alpine-based image works in local Docker Desktop testing
but a compiled Python/Node native dependency fails or behaves
differently in the CI/production Alpine build.
Fix: This is usually musl libc vs. glibc incompatibility in a
native extension; either switch that stage to a glibc-based -slim
image, or confirm the specific native dependency has verified musl
support before committing to Alpine for that service.
Symptom: Setting readOnlyRootFilesystem: true causes the
application to crash on startup with a permission or "read-only file
system" error.
Fix: The app is writing somewhere in the root filesystem at runtime
(temp files, a cache directory, a PID file) — identify the path (check
the crash log/strace) and mount a dedicated emptyDir (or persistent
volume, if it must survive restarts) at exactly that path rather than
reverting the read-only setting entirely.
Symptom: A Dockerfile sets USER app but the running pod in
Kubernetes still shows the process running as root (UID 0).
Fix: Something in the deploy path (runAsUser: 0 in the pod spec,
or a Helm chart default) is overriding the image's default user;
Kubernetes pod spec takes precedence over the Dockerfile USER
instruction. Set and enforce runAsNonRoot: true in the pod
securityContext and consider an admission policy to prevent this
override cluster-wide (see
policy-as-code-guardrails).
Symptom: Image size and vulnerability count barely improve after
switching base images, and the SCA scan still shows the same dozens of
CRITICAL findings.
Fix: Check whether build-stage tooling or dev dependencies are
still leaking into the final stage (missing COPY --from=builder
scoping, or installing dev packages in the final stage instead of only
the builder stage) — the base image swap alone doesn't help if the
Dockerfile still installs a compiler or full package set in the
runtime stage.
A Go HTTP service is rewritten from a single-stage, root, full-Debian
Dockerfile to a hardened multi-stage, distroless, non-root build, with
matching Kubernetes securityContext.
Before:
FROM golang:1.22
WORKDIR /app
COPY . .
RUN go build -o server ./cmd/server
CMD ["./server"]
(Runs as root, ships the full Go toolchain and source tree, ~900MB image, default Debian base with a shell and package manager.)
After:
# syntax=docker/dockerfile:1
FROM golang:1.22 AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /out/server ./cmd/server
FROM gcr.io/distroless/static-debian12:nonroot@sha256:<digest>
COPY --from=builder /out/server /server
USER nonroot:nonroot
ENTRYPOINT ["/server"]
Corresponding Kubernetes pod hardening:
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myorg/myapp:1.4.2@sha256:<image-digest>
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir: {}
Result: image size drops from ~900MB to under 20MB (statically-linked Go
binary plus a near-empty distroless base), the final image ships no
shell/package manager/source tree for an attacker to leverage after a
compromise, a trivy image scan on the new image shows a dramatically
smaller finding count because there's no OS package layer to accumulate
CVEs, and the pod-level securityContext guarantees the container can't
be started as root or with a writable filesystem even if someone edits
the deployment manifest later — assuming an admission policy is in place
to keep that guarantee from being quietly overridden.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin devsecops-skills17 skills in this plugin
/checkov-and-tfsec-iac-security-scanningConfigures Checkov and tfsec to scan Terraform/CloudFormation/Kubernetes IaC source for misconfigurations — hardcoded secrets, overly permissive IAM policies, unencrypted storage, open security groups — before `terraform apply` or `aws cloudformation deploy` ever runs. Use when a user asks to "scan my Terraform for security issues," "add Checkov/tfsec to CI," "find overly permissive IAM in my IaC," "write a Checkov suppression," "compare Checkov and tfsec," or "catch misconfigured cloud resources before they're provisioned."
/complete-devsecops-pipeline-for-kubernetes-from-scratchBuilds the complete security-gate sequence for a Kubernetes-targeted DevSecOps pipeline from scratch — SAST, SCA, container image scanning, a policy-as-code (OPA/Kyverno) admission gate placed before the GitOps handoff, and a secrets model where plaintext credentials never touch the pipeline because an External Secrets Operator pulls them cluster-side instead. Use when the user asks to "build a DevSecOps pipeline for Kubernetes from scratch," "add a policy gate before our GitOps commit," "make sure secrets never touch CI for our K8s deploys," or "sequence SAST/SCA/image-scan/policy gates for a container pipeline end-to-end."
/complete-devsecops-pipeline-for-serverless-from-scratchBuilds the complete security-gate sequence for a serverless (Lambda- style) DevSecOps pipeline from scratch — SAST, SCA against a much larger bundled transitive-dependency surface, package/artifact signing, and least-privilege execution-role scoping as the primary release gate (rather than a Kubernetes-style admission policy), with runtime secrets fetched from a managed secrets service instead of baked into the deployment package. Use when the user asks to "build a DevSecOps pipeline for a Lambda function from scratch," "gate a serverless release on execution-role scope instead of admission policy," "sign a Lambda deployment package," or "make sure a Lambda function's secrets aren't baked into its zip."
/complete-devsecops-pipeline-for-vm-based-workloads-from-scratchBuilds the complete security-gate sequence for a VM-based DevSecOps pipeline from scratch — SAST, SCA, golden-image scanning and CIS benchmark hardening baked into the machine image itself, config- management-applied secrets via Ansible Vault (never plaintext), and an ongoing patch/drift-detection gate that runs independently of any single deploy, distinct from the per-deploy scanning in the Kubernetes and serverless variants. Use when the user asks to "build a DevSecOps pipeline for EC2/VM workloads from scratch," "harden a golden AMI to CIS benchmarks in the pipeline," "apply secrets with Ansible Vault instead of plaintext," or "add ongoing patch/drift detection for a VM fleet."
/critical-vulnerability-emergency-responseGuides the accelerated, out-of-band response when a critical CVE is publicly disclosed in a widely-used dependency, library, or platform component — rapid exposure assessment across the fleet, patch or temporary mitigation, verification, and closeout. Use when the user says "a critical CVE just dropped in X", "we need to check if we're affected by CVE-YYYY-NNNNN", "log4shell-style emergency patching", "zero-day just disclosed in our dependency", or "run an emergency vulnerability response". Distinct from the steady-state finding-queue management in security-finding-backlog-triage — this is the fire-drill process for one urgent, time-critical disclosure.
/dast-integrationGuides adding Dynamic Application Security Testing (DAST) — automated black-box scanning of a running web application or API — using tools such as OWASP ZAP or Nuclei, in CI/CD or on a scheduled cadence against a staging environment. Use when the user asks to "add DAST scanning", "run a ZAP baseline scan against staging", "automate OWASP scanning in the pipeline", "test the running app for injection/auth/config vulnerabilities", or "scan our API for security issues before release". Complements, but does not replace, SAST or SCA static analysis.
Other plugins' skills for the same job