DockerLens is a CLI-based Dockerfile linter (dockerfile-lint-plus) that helps developers identify security risks, inefficiencies, performance issues, and maintainability problems in Dockerfiles before they reach production.
Think of it like ESLint for Dockerfiles โ it checks your Docker configuration against best practices and best practices.
dockerfile-lint-plus analyzes your Dockerfiles and reports violations based on 7 core rules:
| Rule | Type | Purpose |
|---|---|---|
no-latest-tag |
โ Error | Base images must specify a version tag (not :latest) |
require-tagged-base-image |
โ Error | Base images require an explicit tag |
no-curl-bash |
โ Error | Prevent piping remote scripts directly into bash |
copy-deps-before-source |
Copy dependency files before copying all source code | |
require-non-root-user |
Container should not run as root | |
prefer-copy-over-add |
Use COPY instead of ADD when possible | |
require-explicit-cmd-or-entrypoint |
Every image should have CMD or ENTRYPOINT |
Dockerfile Input
โ
[1] Parser - Extract all instructions (FROM, RUN, COPY, etc.)
โ
[2] Rule Engine - Run each of the 7 rules against the instructions
โ
[3] Collector - Gather all violations with line numbers and messages
โ
[4] Formatter - Format output (human-readable or JSON)
โ
Output Results (with exit code for CI/CD)
src/
โโโ cli.ts # Main command-line interface
โโโ analyzer/
โ โโโ parseDockerfile.ts # Parse Dockerfile into instructions
โ โโโ runRules.ts # Execute all rules
โ โโโ resultFormatter.ts # Format output
โโโ rules/ # Individual rule implementations
โ โโโ index.ts # Rule registry (active rules)
โ โโโ noLatestTag.ts
โ โโโ requireTaggedBaseImage.ts
โ โโโ noCurlBash.ts
โ โโโ copyDepsBeforeSource.ts
โ โโโ requireNonRootUser.ts
โ โโโ preferCopyOverAdd.ts
โ โโโ requireExplicitCmdOrEntrypoint.ts
โ โโโ _helpers.ts # Shared helper functions
โโโ types/
โ โโโ DockerInstruction.ts # Parsed instruction data
โ โโโ Finding.ts # Violation/finding data
โ โโโ Rule.ts # Rule interface
โโโ config/
โ โโโ defaultRules.ts # Default rule configuration
โโโ utils/
โโโ config.ts # Config loading/merging
โโโ file.ts # File system utilities
โโโ logger.ts # Logging utilities
โโโ patterns.ts # Regex patterns
Example: no-latest-tag Rule
// 1. Find all FROM instructions
// 2. Check if any use :latest tag
// 3. If found, create a Finding with:
// - Line number where violation is
// - Clear message explaining the problem
// - Suggestion on how to fix it
// 4. Return findings to be displayed to userInstall the CLI tool dockerfile-lint-plus globally:
npm install -g dockerfile-lint-plusOr use directly without installing:
npx dockerfile-lint-plus analyze Dockerfiledockerfile-lint-plus analyze DockerfileOutput:
[ERROR] no-latest-tag
Line 1
Base image uses the latest tag, which is not reproducible.
FROM node:latest
Suggestion: Pin the image to an explicit version such as node:20-alpine.
[WARNING] require-non-root-user
Line 5
No USER instruction found. Container may run as root.
CMD ["npm", "start"]
Suggestion: Add a non-root USER instruction.
Total findings: 2
dockerfile-lint-plus rulesOutput:
no-latest-tag [error] - Avoid latest tags
require-tagged-base-image [error] - Require tagged base image
no-curl-bash [error] - Do not pipe curl or wget into shell
copy-deps-before-source [warning] - Copy dependency files first
require-non-root-user [warning] - Require non-root user
prefer-copy-over-add [warning] - Prefer COPY over ADD
require-explicit-cmd-or-entrypoint [warning] - Require CMD or ENTRYPOINT
dockerfile-lint-plus explain no-latest-tagOutput:
no-latest-tag
Avoid latest tags
Severity: error
Category: base-image
What it checks:
Flags FROM instructions that use the latest tag.
Why it matters:
latest changes over time and makes builds unpredictable.
Suggestion:
Use a pinned version such as node:20-alpine.
dockerfile-lint-plus initCreates dockerfile-lint-plus.config.json with default configuration that you can customize.
dockerfile-lint-plus analyze Dockerfile [options]
Options:
-f, --format <format> Output format: 'stylish' (default) or 'json'
-c, --config <path> Path to config file
-r, --recursive Scan all Dockerfiles in a directory
--strict Fail on warnings too (not just errors)Analyze with JSON output:
dockerfile-lint-plus analyze Dockerfile --format jsonAnalyze with custom config:
dockerfile-lint-plus analyze Dockerfile --config my-rules.jsonFail on both errors and warnings:
dockerfile-lint-plus analyze Dockerfile --strictUse in CI/CD:
dockerfile-lint-plus analyze Dockerfile
if [ $? -ne 0 ]; then
echo "Dockerfile failed linting"
exit 1
fiThe project includes comprehensive tests covering:
- Unit Tests โ Individual rule testing (each rule is tested)
- Integration Tests โ Full analyzer testing with multiple rules
- Edge Cases โ Empty files, comments, multi-stage builds, etc.
- Parser Tests โ Dockerfile parsing with line continuations
- JSON Output Validation โ Correct JSON format
npm test- 82 tests all passing โ
- 12 test files covering different aspects
- Edge cases like empty Dockerfiles, comments-only files
- Multi-stage builds tested for correctness
Test files:
tests/
โโโ analyzer/
โ โโโ parseDockerfile.test.ts # Parser tests
โ โโโ runRules.test.ts # Rule engine tests
โ โโโ jsonOutputValidation.test.ts # Output format tests
โโโ rules/
โ โโโ noLatestTag.test.ts
โ โโโ requireTaggedBaseImage.test.ts
โ โโโ noCurlBash.test.ts
โ โโโ copyDepsBeforeSource.test.ts
โ โโโ requireNonRootUser.test.ts
โ โโโ preferCopyOverAdd.test.ts
โ โโโ requireExplicitCmdOrEntrypoint.test.ts
โโโ edge-cases/
โ โโโ edgeCases.test.ts # Unusual input handling
โโโ integration/
โโโ fullEngine.test.ts # Full pipeline tests
npm test # Run all tests
npm run build # Compile TypeScript
npm run dev # Run with live reloading (development)
npm run lint:sample # Test against sample DockerfileThe following features are planned for future versions:
- Recursive scanning โ Scan entire directories for all Dockerfiles
- JSON output โ Machine-readable output for CI/CD integration
- Dockerfile quality score โ Rate Dockerfile quality 0-100
- GitHub Action โ Use as a GitHub Action in workflows
- Configuration profiles โ Preset configs for different use cases
- More rules โ Additional best-practice rules for security and performance
- Custom rules โ Allow users to write their own rules
- Markdown reports โ Generate detailed HTML/Markdown reports
- Quick fixes โ Automatic suggestion-based fixes
- Multi-format support โ Docker Compose files support
- Performance metrics โ Analyze build layer sizes and caching efficiency
- GitLab CI support โ Integration templates
- pre-commit hooks โ Use as git pre-commit hook
- Docker plug-in โ Embedded in Docker CLI
- IDE extensions โ VSCode extension for real-time linting
Create a dockerfile-lint-plus.config.json file to customize rules:
{
"failOn": ["error"],
"format": "stylish",
"rules": {
"no-latest-tag": "error",
"require-tagged-base-image": "error",
"no-curl-bash": "error",
"copy-deps-before-source": "warning",
"require-non-root-user": "warning",
"prefer-copy-over-add": "warning",
"require-explicit-cmd-or-entrypoint": "warning"
}
}- failOn โ Which severities cause exit code 1 (array of 'error', 'warning', 'info')
- format โ Output format ('stylish' for humans, 'json' for machines)
- rules โ Set severity per rule or 'off' to disable
dockerfile-lint-plus analyze DockerfileCheck your Dockerfile before committing.
# GitHub Actions example
- name: Lint Dockerfile
run: dockerfile-lint-plus analyze Dockerfile --strict#!/bin/bash
dockerfile-lint-plus analyze Dockerfile
[ $? -eq 0 ] && git add Dockerfiledockerfile-lint-plus analyze Dockerfile --format json > report.json
if grep -q '"severity": "error"' report.json; then
exit 1
figit clone <repo>
cd dockerfile-lint-plus
npm install
npm run build
npm test- Source โ TypeScript in
src/ - Tests โ Vitest in
tests/ - Build output โ Compiled JavaScript in
dist/ - Config โ TypeScript compiler in
tsconfig.json
- commander โ CLI argument parsing
- chalk โ Colored terminal output
- typescript โ Type-safe JavaScript
- Project Name โ DockerLens
- CLI Package Name โ dockerfile-lint-plus
- Version โ 1.0.0
- License โ MIT
- Author โ DockerLens Contributors
- Repository โ GitHub
- NPM Package โ Available as
dockerfile-lint-plus
-
Install DockerLens
npm install -g dockerfile-lint-plus
-
Check your Dockerfile with DockerLens
dockerfile-lint-plus analyze Dockerfile
-
View violations
[ERROR] no-latest-tag - Line 1 [WARNING] require-non-root-user - Line 5 Total findings: 2 -
Get help on a rule
dockerfile-lint-plus explain no-latest-tag
-
Fix violations based on suggestions
Done! Your Dockerfile is now more secure and reproducible. โ
For issues, feature requests, or contributions, please visit the DockerLens repository.
DockerLens โ Made By Vidun Shanuka _ Full Stack / ML Developer IN SRI LANKA