Dockerfile Linter with Explanations and SARIF Export
Operator: Abdulla Al Fahad. How tools are built and checked
A fourteen-line, two-stage Dockerfile can carry thirteen findings: a build stage on node:latest, an ubuntu base with no tag, a password written into ENV, an apt-get update in a layer of its own, an install without --no-install-recommends, a script piped into sh, and a CMD that runs under a shell as root. This page reads the file the way Docker does, applies 38 rules to the parsed instructions, and attaches every finding to its line with the reason it matters and the change that fixes it.
Key features
- 38 rules in four groups: base images, package managers, runtime and security, and Dockerfile hygiene
- A parser that follows the escape directive, continuation lines with comments between them, heredocs and ONBUILD
- Stage tracking, so USER, HEALTHCHECK and CMD checks apply to the final image and not to a build stage
- Per-instruction silencing with a comment line, and a count of how many findings were silenced
- Findings as a list with the original line, the rule explanation and the fix, and as SARIF 2.1.0 for code scanning
What the 38 rules look at, and how the file is read
Lines are joined the way the Docker builder joins them: a trailing backslash (or a backtick when a # escape=` directive starts the file) continues the instruction, and comment and blank lines inside a continuation are skipped. A heredoc such as RUN <<EOF is read to its closing delimiter and its body is checked as shell; the body of a COPY heredoc is file content and is ignored. Instruction names are case-insensitive. The shell inside a RUN is split at &&, ||, ;, | and newlines outside quotes and command substitutions, so apt-get install -y curl && rm -rf /var/lib/apt/lists/* is read as two commands. Package-manager checks cover apt-get, yum, dnf, microdnf, apk and pip, including python -m pip. A finding carries the id, the line range, the instruction name and a message naming the specific package, port or tag, with credentials shown only as their first four characters.
When to run it
- A pull request touches a service's Dockerfile and the reviewer pastes it to check that the new RUN did not add an unpinned install or drop the USER line.
- An image scan reports 40 megabytes more than expected, and the lint shows the apt lists were never removed in the same RUN as the install, which is rule DF005.
Running a check
- Paste the Dockerfile or drop the file and press Lint Dockerfile.
- Filter to errors first, then warnings; open Why and how to fix on any finding that is unclear.
- Change the file and lint again, or add a # lint ignore=DF004 comment above an instruction where the finding is deliberate.
Example
Fourteen-line example, first stage and last lines
FROM node:latest AS build
...
FROM ubuntu
ENV DB_PASSWORD=hunter2
RUN apt-get update
RUN apt-get install -y curl git
ADD ./dist /srv/app
RUN curl -fsSL https://get.example.com/install.sh | sh
...
CMD node /srv/app/server.jsFindings by line
line 1 DF001 node:latest uses the latest tag
line 6 DF002 ubuntu has no tag; DF003 the last stage never sets USER; DF013 no HEALTHCHECK
line 7 DF015 ENV DB_PASSWORD stores a value in the image config (the only error)
line 8 DF037 apt-get update runs alone
line 9 DF004 no --no-install-recommends; DF005 apt lists never deleted; DF007 curl and git are not pinned
line 10 DF010 ADD of a plain directory; COPY does the same
line 11 DF022 pipe without pipefail; DF023 download piped into a shell
line 14 DF014 CMD runs under /bin/sh -c
13 findings: 1 error, 10 warnings, 2 notes.Tips for best results
- An ignore comment must sit on a line of its own and hold only rule ids separated by commas or spaces; punctuation other than commas turns it into an ordinary comment, and it covers the next instruction only.
- Rules marked 'like hadolint DL…' name the closest rule in that linter, which helps when a team already has an ignore list; the ids and messages here are not hadolint's.
- Lint each Dockerfile of a repository separately: the SARIF file names the artifact you give it, so export one file per Dockerfile when uploading to code scanning.
Limits
Only text is analysed. Nothing is pulled, so a base image's own user, healthcheck or package versions are unknown, and a final stage that inherits USER from its base is still reported as running as root.
A shell is only parsed approximately. Commands built from variables, eval or here-strings are not expanded, so a finding that depends on them can be missed.
Best practices
- Pin the final stage's base image by digest and let a dependency bot update it: DF001 and DF002 only look at tags, and a digest is the one form they accept as fully pinned.
- Fix errors and warnings, then decide on each note: the note rules describe trade-offs, such as pinning, where either answer can be right.
What each group of rules protects
The base-image rules (DF001, DF002) keep a build repeatable: a tag that moves means two builds of the same commit can differ. The package rules cover the difference between a layer that works and one that grows or breaks later: an install without -y fails because there is no terminal, apt lists left in the layer add size permanently, and a lone apt-get update is cached so that weeks later an edited install line fails with 404 errors against a stale index.
The runtime rules are about what happens when the container is attacked or stopped. Shell-form CMD and ENTRYPOINT make the shell PID 1, which does not forward SIGTERM, so docker stop waits ten seconds before killing the process. A final stage with no USER runs as root. Secrets in ENV are stored in the image configuration and visible with docker inspect, and ARG values are recorded in the image history. A USER that is a name instead of a number is a note here (DF034) because Kubernetes cannot verify it is not root; the Kubernetes YAML linter on this site checks the manifest side of the same rule. Credential-shaped values (an AWS key id, a GitHub token, a private key block, a Slack token, a Stripe live key, a password in a URL) are flagged wherever they appear except in FROM.
Frequently asked questions
Why does it say my image runs as root when the base image sets a user?
Only the Dockerfile text is read. If your base image already ends with USER, the finding is a false alarm for that stage; add # lint ignore=DF003 above the FROM line, or add an explicit USER so the file says what the image does.
How do I upload the results to GitHub code scanning?
Download the SARIF file and upload it with the github/codeql-action/upload-sarif action or the code scanning API. The file records the Dockerfile path you gave it, which should match the path in the repository.
Does it understand multi-stage builds?
Yes. COPY --from is checked against earlier stage names and numbers, duplicate stage names are an error, and the USER, HEALTHCHECK and CMD rules judge only the last stage, which is the image that ships.