A Dockerfile linter can catch fragile builds, unsafe instructions, and avoidable image bloat before they reach a registry. This guide explains how to lint a Dockerfile, interpret common warnings, and turn checks into a repeatable local and CI workflow.
Overview
Linting is a static review of a Dockerfile and its related build configuration. It does not replace a successful image build, a vulnerability scan, or a runtime test. Instead, it identifies problems that are visible before the application runs: questionable instruction order, missing version pins, inefficient layer construction, secret exposure, shell mistakes, and instructions that make a build harder to reproduce.
The most useful workflow combines three types of checks:
- Dockerfile linting: checks syntax, instruction usage, shell behavior, portability, and common best-practice violations.
- Build validation: confirms that the Dockerfile and build context can produce an image using the intended builder and target platform.
- Image and configuration scanning: examines the resulting image, dependencies, or infrastructure configuration for risks that are not visible from Dockerfile text alone.
Tools differ in scope. Hadolint is commonly used for Dockerfile-focused shell and instruction checks. Docker's own build validation features can identify issues during supported build workflows. Image scanners and policy tools can add checks for packages, credentials, base-image choices, and configuration. Treat the output as review evidence rather than an automatic verdict: a warning may be intentional, but it should be understood and documented.
| Tool or check type | Best use | CI integration pattern | Important limitation |
|---|---|---|---|
| Hadolint | Dockerfile instructions and shell commands | Run against every changed Dockerfile; fail on agreed severity levels | Does not build the image or prove runtime behavior |
| Docker build validation | Builder-aware checks and build-time feedback | Run as part of a pull-request build or a dedicated validation job | Available checks and output depend on the Docker and builder workflow in use |
| Image vulnerability scanner | Packages and dependencies in the built image | Scan the image artifact before promotion | Cannot replace Dockerfile review; results vary with image contents and databases |
| Policy or configuration scanner | Secrets, permissions, root users, and infrastructure rules | Enforce organization-specific rules in CI | Rules require maintenance and may need justified exceptions |
Use a Dockerfile linter early, before spending time on a full build. Then build the image, scan the artifact, and test the container with the same inputs used by deployment.
Checklist by scenario
Before the first build
- Confirm the Dockerfile is in the intended directory and that the build command uses the correct context.
- Run a Dockerfile linter locally and save the output with the pull request or issue when a warning needs discussion.
- Check that every
COPYandADDsource is inside the build context and is not excluded unexpectedly by.dockerignore. - Use a deliberate base image family and version reference. A floating tag can make later builds change without a Dockerfile edit; a digest can improve reproducibility when your update process supports digest maintenance.
- Confirm that build arguments do not contain credentials or other secrets. Arguments can be exposed through build metadata or image history depending on how they are used.
When fixing build order and caching
- Copy dependency manifests before application source when dependency installation can be cached independently.
- Keep frequently changing files out of early layers. A source-code change should not necessarily invalidate an expensive dependency step.
- Combine related package-manager operations where appropriate, and remove package-manager indexes or temporary files in the same layer when the package manager creates them.
- Use a multi-stage build when compilation, testing, or asset generation requires tools that are not needed at runtime.
- Review whether cache mounts or remote build caches are appropriate for the builder and CI environment. Caching must not weaken reproducibility or leak sensitive data between builds.
Before merging a security-sensitive change
- Search the Dockerfile, build scripts, and context for passwords, tokens, private keys, and copied local configuration.
- Check the final stage's user. If the application does not require root privileges, create or select a non-root user and verify file ownership.
- Review exposed ports, entrypoints, and default commands against the deployment contract.
EXPOSEdocuments an intended port; it does not replace runtime network policy. - Scan the built image and investigate both direct and transitive package findings. Decide whether to update, remove, or explicitly accept each relevant finding.
- Make sure test files, source maps, development caches, and build credentials are not included in the final image unless required.
Before deployment
- Build from a known commit with a controlled context and record the image reference used for deployment.
- Run the container as the same user and with comparable environment assumptions to production.
- Test the health endpoint, startup behavior, graceful shutdown, logging, and required filesystem permissions.
- Confirm that the image architecture and runtime base are compatible with the deployment target.
- Require a clean lint result or an approved exception for every remaining warning.
What to double-check
Instruction order
A common inefficient pattern copies the entire repository before installing dependencies:
COPY . .
RUN npm ci
If one source file changes, the dependency layer may be invalidated. A more cache-friendly arrangement separates stable manifests from changing source files:
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
The exact manifest names depend on the project. The principle is to place inputs with different change rates in separate layers, while ensuring the lockfile and package-manager behavior remain consistent.
Build context and .dockerignore
The build context is the set of files made available to the builder. A broad context can slow transfers and accidentally make local secrets, dependency directories, or build output available to COPY. A restrictive .dockerignore is helpful, but it can also exclude a file that the Dockerfile needs.
Review both files together. For example, if the Dockerfile contains COPY config/production.json /app/config/, verify that the file is intentionally present in the context and not excluded. Prefer generating deployment configuration through the runtime environment or a supported secret mechanism rather than copying sensitive configuration into an image.
Annotated warning examples
- Unpinned base image:
FROM node:latestis easy to write but makes the input less predictable. Choose a maintained, explicit version policy and update it deliberately. - Secrets in
ENVorARG:ENV API_TOKEN=...places a credential in the image configuration. Use a build or runtime secret mechanism suited to your environment instead. - Shell form ambiguity:
RUN curl URL | shcombines network retrieval and execution. Pin the downloaded artifact or package where practical, verify it, and make the command's failure behavior clear. - Root runtime: Installing files as root is not automatically wrong, but the final process should use the least privilege that the application permits.
- Multiple package-manager layers: Separate update and install commands can leave stale indexes or increase image size. Keep related operations together and clean temporary data in the same layer.
A practical CI pattern
Keep the CI job simple enough that developers can reproduce it locally. A generic pipeline can lint changed Dockerfiles, build the intended target, scan the resulting image, and run a small container test:
lint-dockerfiles
build-image
scan-image
run-smoke-test
For a repository with multiple images, discover Dockerfiles explicitly rather than assuming every file is named Dockerfile. Configure severity thresholds deliberately: fail immediately on syntax errors and confirmed secret exposure, while requiring an owner and expiration date for accepted style or portability exceptions.
Common mistakes
- Using a linter as a security guarantee. Static checks cannot see every dependency vulnerability, runtime permission issue, or registry policy. Pair linting with image scanning and container tests.
- Suppressing warnings globally. A global ignore hides future regressions. Prefer a narrow rule suppression with a comment explaining why it is safe.
- Optimizing layers before defining the runtime boundary. First separate build-time tools from runtime files with a suitable multi-stage design; then tune caching and cleanup.
- Copying the whole repository by default. This can include credentials, local dependencies, test artifacts, and large files. Review the context and
.dockerignoreas part of every image change. - Assuming a smaller image is always a better image. Size matters, but compatibility, debuggability, updateability, and a supported runtime base matter too.
- Testing only the Dockerfile. A clean lint result does not prove that the application starts, listens on the expected interface, handles signals, or has the permissions it needs.
When to revisit
Return to this checklist whenever a Dockerfile, base image, package lockfile, build argument, entrypoint, or CI runner changes. Revisit it before a deployment pipeline update, after a vulnerability review, and whenever a build becomes unexpectedly slow or large.
At a minimum, review lint rules and CI examples quarterly or after a major Docker, builder, base-image, or scanning-tool release. Tool behavior and recommended syntax can change, and a rule that was unavailable or noisy may become useful later. Also review accepted exceptions: each should still have an owner, a reason, and a clear condition for removal.
For the next image change, use this short sequence:
- Inspect the diff and the build context.
- Lint the Dockerfile locally.
- Fix or document warnings related to pins, secrets, users, order, and size.
- Build the exact target used by CI or deployment.
- Scan the image and run a smoke test as the intended runtime user.
- Record any exception before merging, then schedule its review.
That process keeps Dockerfile linting connected to the outcome that matters: a build that is reproducible enough to review, small enough to operate, and constrained enough to deploy responsibly.