/devops-skills:dockerfile-validatorfrom devops-skills
Validates, lints, and secures Dockerfiles using hadolint and Checkov. Runs syntax checks, security scanning, best practices enforcement, and build optimization analysis.
Ships in devops-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.
Read when SKILL.md points to them; scripts run unread.
Comprehensive toolkit for validating Dockerfiles with syntax checking, security scanning, best practices enforcement, and build optimization analysis. This skill uses a single self-contained script (dockerfile-validate.sh) that handles everything: tool installation, validation, and cleanup.
Key Features:
Invoke this skill when:
Single command to validate any Dockerfile:
bash scripts/dockerfile-validate.sh Dockerfile
That's it! The script automatically:
The dockerfile-validate.sh script runs a comprehensive 4-stage validation:
┌─────────────────────────────────────────────────────────┐
│ Auto-Install (if needed) │
│ ├─> Check for hadolint and Checkov │
│ ├─> Install in Python venvs if not found │
│ └─> Set TEMP_INSTALL=true (triggers cleanup on exit) │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ [1/4] Syntax Validation (hadolint) │
│ ├─> Dockerfile syntax checking │
│ ├─> Instruction validation │
│ ├─> Shell script validation (via ShellCheck) │
│ └─> 100+ built-in linting rules │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ [2/4] Security Scan (Checkov) │
│ ├─> Security policy validation │
│ ├─> Hardcoded secret detection │
│ ├─> Port exposure checks │
│ ├─> USER directive validation │
│ └─> 50+ security policies │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ [3/4] Best Practices Validation (custom) │
│ ├─> Base image tag validation (:latest check) │
│ ├─> USER directive enforcement (non-root) │
│ ├─> HEALTHCHECK presence │
│ ├─> Layer efficiency (RUN command count) │
│ ├─> Package cache cleanup verification │
│ ├─> Hardcoded secrets detection │
│ └─> COPY ordering for build cache efficiency │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ [4/4] Optimization Analysis (custom) │
│ ├─> Base image size analysis (Alpine suggestions) │
│ ├─> Multi-stage build opportunities │
│ ├─> Layer count optimization │
│ ├─> .dockerignore file check │
│ └─> Build structure recommendations │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ Auto-Cleanup (bash trap - always runs) │
│ └─> Remove temp venvs if TEMP_INSTALL=true │
└─────────────────────────────────────────────────────────┘
Cleanup Guarantee:
Uses trap cleanup EXIT INT TERM to ensure cleanup runs on:
Purpose: Lint Dockerfile syntax and catch common mistakes before building.
Tool: hadolint - A Dockerfile linter that validates instructions and embedded bash commands using ShellCheck.
Installation:
Tools are automatically installed by the validation script if not found. For permanent installation:
# macOS
brew install hadolint
# Linux
wget -O ~/.local/bin/hadolint https://github.com/hadolint/hadolint/releases/latest/download/hadolint-Linux-x86_64
chmod +x ~/.local/bin/hadolint
# Docker (fallback option)
docker pull hadolint/hadolint
Workflow:
# Run hadolint on Dockerfile
hadolint Dockerfile
# Run with JSON output for parsing
hadolint --format json Dockerfile
# Run with specific rules ignored
hadolint --ignore DL3006 --ignore DL3008 Dockerfile
# Using Docker if not installed
docker run --rm -i hadolint/hadolint < Dockerfile
Common Issues Detected:
DL-prefixed rules (hadolint-specific):
DL3000 - Use absolute WORKDIR pathsDL3001 - For some bash commands make no sense in a Docker containerDL3002 - Last USER should not be rootDL3003 - Use WORKDIR to switch directoriesDL3004 - Do not use sudoDL3006 - Always tag image versions (avoid :latest)DL3007 - Using latest is not recommendedDL3008 - Pin versions in apt-get installDL3009 - Delete apt-cache after installingDL3013 - Pin versions in pip installDL3014 - Use -y switch for apt-getDL3015 - Avoid additional packages with apt-getDL3016 - Pin versions in npm installDL3018 - Pin versions in apk addDL3019 - Use --no-cache with apk addDL3020 - Use COPY instead of ADD for filesDL3021 - COPY from previous stages should reference by nameDL3022 - COPY --from should reference a previously defined FROM aliasDL3025 - Use JSON notation for CMD and ENTRYPOINTDL3059 - Multiple consecutive RUN instructions (combine for layer efficiency)SC-prefixed rules (ShellCheck for RUN commands):
SC1091 - Not following sourced filesSC2046 - Quote to prevent word splittingSC2086 - Double quote to prevent globbingSC2164 - Use cd ... || exit for error handlingRule Severity Levels:
Best Practices:
Purpose: Detect security misconfigurations and vulnerabilities before image deployment.
Tool: Checkov - Policy-as-code security scanner with 50+ built-in Dockerfile policies.
Installation:
The validation script automatically installs Checkov in an isolated Python venv if not found. For permanent installation:
# Install directly
pip3 install checkov
# macOS Homebrew
brew install checkov
# Verify installation
checkov --version
Workflow:
# Scan a Dockerfile
checkov -f Dockerfile --framework dockerfile
# Scan a directory (finds all Dockerfiles)
checkov -d . --framework dockerfile
# Scan with compact output (only failures)
checkov -f Dockerfile --framework dockerfile --compact
# Scan with JSON output
checkov -f Dockerfile --framework dockerfile -o json
# Skip specific checks
checkov -f Dockerfile --framework dockerfile --skip-check CKV_DOCKER_2
Common Security Checks:
General Security:
CKV_DOCKER_1 - Ensure port 22 (SSH) is not exposedCKV_DOCKER_2 - Ensure HEALTHCHECK instruction existsCKV_DOCKER_3 - Ensure user is created and used (not root)CKV_DOCKER_4 - Ensure ADD is not used (prefer COPY)CKV_DOCKER_5 - Ensure update without install is not used aloneCKV_DOCKER_6 - Ensure SHELL instruction uses -o pipefailCKV_DOCKER_7 - Ensure base image uses specific version tagCKV_DOCKER_8 - Ensure last USER is not rootCKV_DOCKER_9 - Ensure apt-get dist-upgrade is not usedCKV_DOCKER_10 - Ensure yum update is not used alonePackage Management:
Secrets Detection:
Output Formats:
cli - Human-readable console output (default)json - JSON format for programmatic parsingsarif - SARIF format for IDE integrationgitlab_sast - GitLab security dashboard formatjunitxml - JUnit XML for CI integrationUnderstanding Results:
Check: "Ensure that HEALTHCHECK instructions have been added to container images"
FAILED for resource: Dockerfile.
File: /Dockerfile:1-20
Guide: https://docs.bridgecrew.io/docs/ensure-that-healthcheck-instructions-have-been-added-to-container-images
1 | FROM node:18
20 | CMD ["node", "server.js"]
Suppressing False Positives:
Add inline comments to suppress specific checks:
# checkov:skip=CKV_DOCKER_2:Health check not applicable for this init container
FROM alpine:3.21
...
Exit Codes:
0 - All checks passed1 - One or more checks failedBest Practices:
Purpose: Ensure Dockerfiles follow official Docker best practices and current recommendations.
Custom Validation Checks:
1. Base Image Validation:
# Check for :latest tag usage
grep -E "^FROM.*:latest" Dockerfile
# Recommend specific tags or digest pinning
# Good: FROM alpine:3.21
# Better: FROM alpine:3.21@sha256:digest
2. Multi-Stage Build Detection:
# Count FROM statements
grep -c "^FROM" Dockerfile
# Single FROM suggests potential for multi-stage optimization
3. USER Directive Check:
# Ensure USER is set before CMD/ENTRYPOINT
# Check that last USER is not root
grep "^USER" Dockerfile
4. HEALTHCHECK Presence:
# Verify HEALTHCHECK is defined for services
grep "^HEALTHCHECK" Dockerfile
5. Layer Efficiency:
# Count RUN commands (>5 suggests combination opportunity)
grep -c "^RUN" Dockerfile
# Check for apt-get update separated from install
grep -A1 "^RUN.*apt-get update" Dockerfile
6. Package Manager Cache Cleanup:
# Verify cache cleanup in same RUN layer
grep "rm -rf /var/lib/apt/lists" Dockerfile
grep "--no-cache" Dockerfile # for apk
Best Practices Checklist:
Base Images:
Layer Optimization:
Security:
Multi-Stage Builds:
Runtime Configuration:
Build Performance:
Purpose: Identify opportunities to reduce image size, build time, and layer count.
Optimization Categories:
1. Image Size Reduction:
# Bad: Full distro
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y curl
# Good: Minimal distro
FROM alpine:3.21
RUN apk add --no-cache curl
# Better: Multi-stage with distroless
FROM golang:1.21 AS build
WORKDIR /app
COPY . .
RUN go build -o myapp
FROM gcr.io/distroless/base-debian11
COPY --from=build /app/myapp /
ENTRYPOINT ["/myapp"]
2. Layer Optimization:
# Bad: Separate RUN commands (creates many layers)
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y git
RUN apt-get install -y vim
# Good: Combined RUN (single layer)
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
git \
vim \
&& rm -rf /var/lib/apt/lists/*
3. Build Cache Efficiency:
# Bad: Copy all, then install dependencies
COPY . /app
RUN pip install -r requirements.txt
# Good: Copy dependency file first
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app
4. Multi-Stage Build Opportunities:
# Detects single-stage builds that could benefit from separation
# Look for:
# - Build tools installed but not needed at runtime
# - Source code copied but only binary needed
# - Development dependencies mixed with runtime
Optimization Metrics:
Reference Documentation:
Load detailed best practices:
references/docker_best_practices.md - Official Docker recommendations
references/optimization_guide.md - Layer and size optimization techniques
Purpose: Ensure build context is optimized by excluding unnecessary files.
Validation Checks:
# Check if .dockerignore exists
if [ ! -f .dockerignore ]; then
echo "WARNING: .dockerignore file not found"
fi
# Common patterns that should be included
.git
.gitignore
README.md
.env
*.log
node_modules
*.md
.dockerignore
Dockerfile*
docker-compose*.yml
Benefits of .dockerignore:
Best Practices:
The validation script automatically installs tools if not found. No manual installation required.
For permanent installations:
# Install hadolint
brew install hadolint # macOS
# Install Checkov
pip3 install checkov
Minimum Versions:
Testing Auto-Install and Cleanup:
To test the temporary installation and cleanup functionality even when tools are already installed:
# Force temporary installation for testing
FORCE_TEMP_INSTALL=true bash scripts/dockerfile-validate.sh Dockerfile
This will:
When validation tools are not installed:
Detect Missing Tool:
Complete Available Validations:
Prompt User for Installation:
For hadolint:
hadolint is not installed. The script will automatically install it temporarily.
For permanent installation:
- macOS: brew install hadolint
- Linux: wget -O ~/.local/bin/hadolint https://github.com/hadolint/hadolint/releases/latest/download/hadolint-Linux-x86_64 && chmod +x ~/.local/bin/hadolint
- Docker: docker pull hadolint/hadolint
hadolint provides comprehensive Dockerfile linting and best practice checking.
For Checkov:
Checkov is not installed. Would you like to install it?
Installation options:
- Recommended: pip3 install checkov
- macOS: brew install checkov
Checkov provides security scanning with 50+ Dockerfile policies.
Install and rerun validation? (y/N)
If User Chooses to Install:
hadolint --version or checkov --versionIf User Declines:
Required (always run):
Recommended (offer installation if missing):
Optional:
Error: FROM instruction must be first non-comment
Solution: Move ARG that defines base image tag before FROM
ARG VERSION=18
FROM node:${VERSION}
Error: Unknown instruction (typo)
Solution: Check instruction spelling (RUN, COPY, FROM, etc.)
Common typos: RUNS, COPIES, FRUM
Error: Chained RUN command fails
Solution: Add set -e or check individual command success
RUN apt-get update && apt-get install -y package || exit 1
Error: COPY failed: file not found
Solution: Check file path is relative to build context
Verify file exists and not excluded by .dockerignore
Security: Hardcoded secrets detected
Solution: Use build secrets (BuildKit)
# Instead of: ENV API_KEY=secret123
# Use: docker build --secret id=api_key,src=api_key.txt
Performance: Slow builds
Solution:
1. Optimize layer caching (COPY package files first)
2. Use .dockerignore to reduce context
3. Enable BuildKit: export DOCKER_BUILDKIT=1
4. Use multi-stage builds
dockerfile-validate.sh
bash scripts/dockerfile-validate.sh [Dockerfile]good-example.Dockerfile - Demonstrates best practices and optimal structure
bad-example.Dockerfile - Common mistakes and anti-patterns
security-issues.Dockerfile - Intentional security vulnerabilities for testing
python-optimized.Dockerfile - Python-specific optimizations and multi-stage build
golang-distroless.Dockerfile - Minimal Go application using distroless base image
.dockerignore.example - Example .dockerignore for build context optimization
docker_best_practices.md - Official Docker best practices and recommendations
optimization_guide.md - Layer optimization and image size reduction techniques
security_checklist.md - Container security best practices
IMPORTANT: When using this skill, you MUST follow these steps in order:
bash scripts/dockerfile-validate.sh <Dockerfile> to run all 4 validation stages.Summarize findings by severity - After validation completes, provide a clear summary organized by:
Propose specific fixes - For each issue found, provide concrete code examples showing how to fix it. You MUST use the Read tool to load the appropriate reference files before proposing fixes:
references/security_checklist.md - For security-related fixesreferences/optimization_guide.md - For performance/size improvementsreferences/docker_best_practices.md - For general best practicesNote: Always explicitly read reference files during the post-validation phase to ensure fix recommendations follow authoritative patterns, even if you have prior knowledge of the content.
Offer to apply fixes - Ask the user if they want you to apply the proposed fixes to their Dockerfile.
IMPORTANT: After running validation, you MUST use the Read tool to explicitly load the appropriate reference files before proposing fixes. This ensures fix recommendations are accurate and follow authoritative patterns.
Workflow:
| Issue Type | Reference File | Action |
|---|---|---|
| Security issues (secrets, USER, ports) | references/security_checklist.md | Read before proposing security fixes |
| Size/performance optimization | references/optimization_guide.md | Read before proposing optimization fixes |
| General best practices | references/docker_best_practices.md | Read before proposing best practice fixes |
Example:
# After validation finds security issues:
1. Use Read tool: Read references/security_checklist.md
2. Apply fix patterns from the file to the specific issues found
3. Propose fixes with code examples based on reference content
User: "Validate my Dockerfile"
Steps:
1. Read the Dockerfile using Read tool to understand structure
2. Run validation script: bash scripts/dockerfile-validate.sh Dockerfile
3. Review output from all 4 stages (hadolint, Checkov, best practices, optimization)
4. Summarize findings organized by severity (critical → low)
5. Use Read tool to load relevant reference files:
- Read references/security_checklist.md (if security issues found)
- Read references/optimization_guide.md (if optimization issues found)
- Read references/docker_best_practices.md (if best practice issues found)
6. Propose specific fixes with code examples based on reference content
7. Ask user: "Would you like me to apply these fixes?"
8. Apply fixes if user approves
User: "Check all Dockerfiles in my project"
Steps:
1. Find all Dockerfile* files
2. Validate each sequentially
3. Aggregate results
4. Identify common issues across files
5. Provide unified report
6. Suggest project-wide improvements
User: "Security audit my Dockerfile"
Steps:
1. Run Checkov security scan
2. Run hadolint for security rules (DL3* series)
3. Check for hardcoded secrets
4. Verify USER directive
5. Check base image vulnerabilities
6. Provide security-focused report
7. Prioritize critical findings
User: "How can I optimize my Dockerfile?"
Steps:
1. Analyze current layer structure
2. Identify multi-stage opportunities
3. Check build cache efficiency
4. Suggest base image alternatives
5. Calculate potential size savings
6. Provide before/after comparison
7. Implement optimizations if approved
This skill works well in combination with:
This skill is based on comprehensive research from authoritative sources:
Official Docker Documentation:
Security Guidelines:
Best Practices Resources:
examples/bad-example.Dockerfile (opens in a new tab)examples/golang-distroless.Dockerfile (opens in a new tab)examples/good-example.Dockerfile (opens in a new tab)examples/python-optimized.Dockerfile (opens in a new tab)examples/security-issues.Dockerfile (opens in a new tab)references/docker_best_practices.md (opens in a new tab)references/optimization_guide.md (opens in a new tab)references/security_checklist.md (opens in a new tab)scripts/dockerfile-validate.sh (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Generated from the plugin's indexed repository (gitjfmd/ansible-skills) — no native marketplace selected
npx claudepluginhub p/gitjfmd-devops-skills-devops-skills-plugin31 skills in this plugin
/ansible-generatorComprehensive toolkit for generating best practice Ansible playbooks, roles, tasks, and inventory files./ansible-validatorComprehensive toolkit for validating, linting, testing, and automating Ansible playbooks, roles, and collections. Use this skill when working with Ansible files (.yml, .yaml playbooks, roles, inventories), validating automation code, debugging playbook execution, performing dry-run testing with check mode, or working with custom modules and collections./azure-pipelines-generatorComprehensive toolkit for generating best practice Azure DevOps Pipelines following current standards and conventions. Use this skill when creating new Azure Pipelines, implementing CI/CD workflows, or building deployment pipelines.Other plugins' skills for the same job