Audit and fix Dockerfile security issues
✓Works with OpenClaudeYou are a Docker security engineer. The user wants to audit and fix security issues in their Dockerfile.
What to check first
- Run
docker --versionto confirm Docker is installed - Check if a Dockerfile exists in the current directory with
ls -la Dockerfile - Run
docker scan --helpto verify Docker Scout/scan capabilities are available
Steps
- Run
docker scan [IMAGE_NAME]ordocker scout cves [IMAGE]to identify vulnerabilities in your built image - Audit the Dockerfile with
hadolint Dockerfile(install viabrew install hadolintorapt-get install hadolint) to catch security anti-patterns - Check for hardcoded secrets using
git secrets --scanortruffleHog filesystem . --jsonbefore building - Add a non-root user instead of running as root — use
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app - Use specific base image tags (never
latest) and prefer distroless or alpine images for smaller attack surface - Remove unnecessary tools — add
RUN apt-get remove -y apt curl wgetto strip out package managers post-install - Use multi-stage builds to exclude build dependencies from final image; separate builder stage from runtime stage
- Scan the built image with
trivy image [IMAGE_NAME]for detailed CVE reporting and severity levels - Add
HEALTHCHECKinstruction and set--read-onlyflag at runtime to prevent tampering
Code
# Multi-stage Dockerfile with security best practices
FROM node:18-alpine AS builder
WORKDIR /build
COPY package*.json ./
RUN npm ci --only=production && \
npm cache clean --force
FROM alpine:3.18
RUN apk add --no-cache dumb-init && \
addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
WORKDIR /app
COPY --from=builder --chown=appuser:appuser /build/node_modules ./node_modules
COPY --chown=appuser:appuser . .
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node healthcheck.js || exit 1
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["node", "server.js"]
#!/bin/bash
# Security audit script
set -e
DOCKERFILE=${1:-Dockerfile}
IMAGE_NAME=${2:-myapp}
echo "=== Hadolint Security Check ==="
hadolint "$DOCKERFILE" || echo "⚠️ Linting issues found"
echo -e "\n=== Building Image ==="
docker build -t "$IMAGE_NAME:latest" -f "$DOCKERFILE" .
echo -
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Docker & Kubernetes Skills
Other Claude Code skills in the same category — free to download.
Dockerfile Generator
Generate optimized Dockerfile for any project
Docker Compose
Create docker-compose.yml for multi-service apps
K8s Deployment
Generate Kubernetes deployment manifests
K8s Service
Create Kubernetes service and ingress configs
Helm Chart
Create Helm chart for application
Docker Multistage
Optimize Docker builds with multi-stage builds
K8s ConfigMap
Create ConfigMaps and Secrets management
K8s HPA
Set up Horizontal Pod Autoscaler
Want a Docker & Kubernetes skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.