Hadolint compatibility
shell-lsp checks Dockerfiles with the rules Hadolint is known for, under Hadolint's DL codes, and reads Hadolint's inline pragmas and .hadolint.yaml. A project that runs Hadolint can drop it and keep its configuration: the editor shows the same rules while typing, and shell-lsp check runs them in CI. Every rule is listed with its severity in checks.md.
The rules were written from what Hadolint does, never from how it does it: Hadolint 2.14.0 was run as a black box on more than 500 Dockerfiles written for the purpose, recording only codes and positions, as CLEAN_ROOM.md requires. The cases live in internal/hostnav/testdata/hadolint, and go test ./internal/hostnav checks each of them against both shell-lsp and the live Hadolint. Wherever the two disagree, the case says why, in a line such as >>+ DL3008 2 -- /usr/bin/apt-get is apt-get, called by its path. just corpus-hosts compares them on the Dockerfiles of the corpus as well.
Rules
The DL3xxx and DL4xxx rules check how images are built: package versions and caches for apt, apk, yum, dnf, microdnf, zypper, pip, npm, gem, yarn and go, image tags and registries, USER, WORKDIR, COPY and ADD, EXPOSE, ENV, pipes without pipefail, consecutive RUNs, and labels.
Some problems are also checks BuildKit itself makes, which shell-lsp already runs under SL7 codes (rules.md). Those are reported once, under the SL code, and the DL code stands for it wherever a code is named: in # hadolint ignore=, in ignored and in override.
| Hadolint | Reported as |
|---|---|
| DL1000 | SL7001, SL7002, SL7003 |
| DL3000 | SL7015 |
| DL3012, DL4003, DL4004 | SL7016 |
| DL3022 | SL7005 |
| DL3023 | SL7024 |
| DL3024 | SL7007 |
| DL3025 | SL7004 |
| DL3029 | SL7022 |
| DL3043, DL3061 | SL7001 |
| DL4000 | SL7014 |
Hadolint runs ShellCheck on the shell of each RUN. shell-lsp lints that shell with its own linter, under the same SC codes, as the shell the stage's SHELL names, and a pragma above a RUN covers its shell findings too.
Two rules are off until asked for, as in Hadolint: DL1001, which reports every # hadolint ignore= pragma, and DL3057, which reports an image without a HEALTHCHECK. Naming them under override turns them on, as does enabling the optional checks no-ignore-pragmas and require-healthcheck with lint.optional, --enable or enable= in .shellcheckrc.
Pragmas
A comment line of its own, outside any instruction:
# hadolint ignore=DL3008,SC2086ignores those codes in the instruction that starts on the next line. A blank line or another comment in between ends its reach.# hadolint global ignore=DL3059ignores them in the whole file, wherever it stands.# hadolint shell=bashlints the shell of every laterRUNin the stage as that shell, as a laterSHELLinstruction would; whichever comes last wins. A shell shell-lsp does not read, such aspwsh, leaves thoseRUNs alone.
A trailing # comment after the codes is allowed. A pragma with an unknown code, an empty item or a second word is not a pragma and ignores nothing.
Configuration
shell-lsp uses the .hadolint.yaml or .hadolint.yml nearest to the Dockerfile, looking in its directory and then in each directory above it, the way .shellcheckrc is found. Hadolint looks only in the directory it is run from, which an editor does not have; projects keep the file at their root, so both find the same one. When no project has one, the first of these is used: $XDG_CONFIG_HOME/hadolint.yaml (or ~/.config/hadolint.yaml), ~/.hadolint/hadolint.yaml and ~/.hadolint.yaml, each also as .yml. Files are not merged. The editor rereads a file when it changes.
| Setting | What shell-lsp does with it |
|---|---|
ignored | A list of codes reported nowhere in the Dockerfile, DL, SC or SL. |
override | A mapping of error, warning, info and style to lists of codes reported at that severity. A code under several takes the most severe. |
trustedRegistries | A list of registries FROM may pull from (DL3026); * matches any run of characters, so *.gcr.io trusts eu.gcr.io. Without the setting, DL3026 is off. |
label-schema | A mapping of label names to text, url, email, rfc3339, spdx, hash or semver. The final image must carry each label (DL3049), and each must have a value of its kind (DL3051 to DL3058). |
strict-labels | true reports labels the schema does not name (DL3050). |
disable-ignore-pragma | true makes ignore= and global ignore= pragmas ignore nothing. |
failure-threshold, no-fail, format, no-color, verbose | Checked, then left to Hadolint: they shape Hadolint's own output and exit status. shell-lsp check has its own options for both (cli.md). |
A configuration with a mistake is not applied. The server logs a message to the editor that names the file, line and column, as it does for a broken .shellcheckrc, and shell-lsp check stops with that error. Unlike Hadolint, shell-lsp also treats an unknown setting, a level that override does not know, and a code that is no check code as mistakes, so a misspelt setting cannot go unnoticed; an empty file, on the other hand, configures nothing and is no mistake. The file is read as YAML without anchors, aliases, tags or block scalars, and with each value on one line.
Hadolint's command-line flags and its HADOLINT_ environment variables are not read.
Where shell-lsp differs
Hadolint matches the words of a command where shell-lsp reads the command the way the tool does. The cases record every difference; these are the kinds.
- Commands. A command called by its path, such as
/usr/bin/apt-get, or throughsudo,env,nice,xargs,execorcommand, is the command it runs. The first word after the options is the subcommand, soapt-get remove vim install gitremoves, and the words after--, the values of options such as-oor--target-releaseand the build arguments aftergem install x --are no packages. - What the tools accept. Each tool's own rules decide: apt-get's quiet level 2 or more,
-o APT::Get::Assume-Yes=trueandapt-get dist-clean; apk's~versions and--no-network; pip's requirement and constraint files, VCS references and every boolean value ofPIP_NO_CACHE_DIR; npm'snpm iand#refgit specs and itslatesttag; local paths and archives, which are fixed files; go's local packages andgo runarguments. - Cleaning up. A cache counts as removed when the removed path or the cache mount covers it, whatever
rmoptions spell it, and not when an unrelatedrm -rfruns. Caches, labels, the lastUSERandHEALTHCHECKmatter only in the final stage and the stages others are builtFROM; a build stage that onlyCOPY --fromreads never becomes an image. - Values.
$NAMEtakes the valueARGorENVgives it, souseradd -u "$UID"withARG UID=1000is fine. A value nothing sets, such as anARGwithout a default, is unknown and reported by no rule. - The Dockerfile grammar is BuildKit's. Stage names are case-insensitive,
localhost:5000/appnames the registrylocalhost:5000,EXPOSE 53/sctpandADDheredocs are valid, and a syntax error does not stop the other checks. The exec form ofRUNruns no shell, so&&in it is a word, and neither a PowerShellRUNnor the stringsh -cruns is read as shell. - Named contexts. A
FROMimage orCOPY --fromname that a bake file (docker-bake.hcl,docker-bake.jsonor any*.hclwithtargetblocks) or a Composeadditional_contextsmaps for this Dockerfile is that context, not a registry image: DL3006 is reported only when the context is adocker-image://reference without a tag. DL3007 and DL3026 are not applied to context values, since a:latestcontext, such as an image another target of the same bake run builds, is common and deliberate.- Where bake files are found. In the Dockerfile's directory and every directory above it, and, inside a git repository, in every directory of the repository that neither
.gitignore, the repository's.git/info/excludenor the filecore.excludesFilenames excludes, read as the workspace scan reads them (.git,node_modules,vendor,.venvand similar directories are skipped). The search stops after 20,000 directories; the server then warns once per repository andshell-lsp checkprints one line to standard error, without changing its exit status, since contexts from bake files past that point are not known. Outside a repository only the directory and its ancestors are searched. Ignored bake files in a searched directory still count, since a localdocker-bake.override.hclis often ignored on purpose. - Paths.
contextanddockerfileare relative to the bake file's directory, which is where bake runs from when it finds its default files; acwd://path is read the same way. A remotecontextnames no local Dockerfile. Acwd://context value is a local directory, not an image. - Variables.
${NAME}takes the stringdefaultofvariable "NAME", and a default may itself interpolate other variables, as indefault = "${REGISTRY}/base". The environment is not read, so the server andshell-lsp checkagree whatever environment they run in; a variable without a default, any function call, and defaults that refer to each other in a cycle leave the value unknown. A target whosecontextordockerfileis unknown is skipped; a context whose value is unknown still counts as named, but its tag is not checked. - Several files. The default files of a directory (Compose files,
docker-bake.json,docker-bake.hcland their overrides) form one project and other*.hclfiles one each, as bake reads them.inheritsand variables a project does not define are taken from the other bake files of the same directory, the project's own definitions winning.
- Where bake files are found. In the Dockerfile's directory and every directory above it, and, inside a git repository, in every directory of the repository that neither
- Formats. RFC 3339, SemVer and email values are held to their specifications;
spdxaccepts SPDX license expressions over License List 3.27, which the OCIlicenseslabel is defined to hold; andhashaccepts abbreviated and SHA-256 commit hashes. - Places. A finding sits on the word it is about, such as the unpinned package, and each such word gets one, where Hadolint reports each rule once per instruction at its first column.