Linting Dockerfiles and build checks

Intermediate · 11 min

Catch Dockerfile mistakes before they ship: BuildKit's built-in build checks with docker build --check, the check directive, and hadolint in CI.

Before thisCMD, ENTRYPOINT and PID 1Build arguments and environment variables

Nick Janetakis · 11 min

Linting Dockerfiles and build checks

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Nick JanetakisReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Build checks are a linter built into BuildKit's Dockerfile frontend since Dockerfile 1.8. Every build runs them and prints violations as warnings.
  • docker build --check . runs only the checks, without executing any build steps, which makes it a fast CI gate.
  • Violations exit with status 0 unless the Dockerfile has # check=error=true, or the build passes BUILDKIT_DOCKERFILE_CHECK=error=true.
  • # check=skip=RuleName turns off a rule you disagree with, and pinning the # syntax line stops new rules from failing an error=true build after an upgrade.
  • The checks catch real bugs as well as style: shell-form CMD that swallows signals, secrets in ARG or ENV, undefined variables and duplicate CMD lines.
  • hadolint is a separate linter that also runs ShellCheck on RUN commands and enforces practices such as pinning apt package versions.

Mistakes that build fine

Most Dockerfile bugs do not fail the build. A shell-form CMD builds and then makes docker stop hang for ten seconds. A typo in $PATH builds and then the app cannot find its own binaries. A token passed as ARG builds and then sits in the image history for anyone who pulls it. Code review catches some of this. A linter catches it every time, in seconds.

There are two common tools. BuildKit has checks built in. hadolint is a separate linter with a different focus. Most teams can use both.

Build checks in BuildKit

Build checks arrived with Dockerfile 1.8 and Buildx 0.15. They live in the Dockerfile frontend, the part of BuildKit that turns a Dockerfile into a build graph, so every docker build runs them and prints violations at the end. To lint without building:

docker build --check .

Nothing executes. The frontend parses the file, resolves base image configs and reports.

To see the checks without Docker, I called the frontend’s lint entry point directly. DockerfileLint is the Go function the --check request reaches, in BuildKit v0.35.1, the current release. I fed it this deliberately bad Dockerfile, with base image configs resolved from Docker Hub:

FROM python:3.14-slim as build
ARG API_TOKEN
ENV DB_PASSWORD=changeme
workdir app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
MAINTAINER ops@example.com
FROM python:3.14-slim
COPY --from=build /app /app
ENV PATH=/app/bin:$PTH
EXPOSE 8080/TCP
CMD python -m app
CMD ["python", "-m", "app"]

It reported ten problems:

line 1   FromAsCasing: 'as' and 'FROM' keywords' casing do not match
line 7   MaintainerDeprecated: Maintainer instruction is deprecated in favor of using label
line 4   ConsistentInstructionCasing: Command 'workdir' should match the case of the command majority (uppercase)
line 2   SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ARG "API_TOKEN")
line 3   SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "DB_PASSWORD")
line 4   WorkdirRelativePath: Relative workdir "app" can have unexpected results if the base image changes
line 10  UndefinedVar: Usage of undefined variable '$PTH' (did you mean $PATH?)
line 11  ExposeProtoCasing: Defined protocol '8080/TCP' in EXPOSE instruction should be lowercase
line 12  JSONArgsRecommended: JSON arguments recommended for CMD to prevent unintended behavior related to OS signals
line 12  MultipleInstructionsDisallowed: Multiple CMD instructions should not be used in the same stage because only the last one will be used

Four of those are style. The other six are bugs waiting to happen: two leaked secrets, a broken PATH, a relative WORKDIR that depends on whatever the base image sets, a signal-swallowing CMD, and a CMD that is silently ignored. The fixed version returned no warnings.

That version of the frontend defines 22 rules. One of them, InvalidDefinitionDescription, is experimental and off by default. Another, CopyIgnoredFile, warns when COPY names a file your .dockerignore excludes.

Making checks fail the build

By default a violation is a warning and the exit code is 0, so a CI step that runs --check passes no matter what. The check parser directive changes that:

# syntax=docker/dockerfile:1.29
# check=error=true
FROM python:3.14-slim

Now any violation fails docker build and docker build --check. You can do the same from outside the file with --build-arg BUILDKIT_DOCKERFILE_CHECK=error=true, which is handy for turning it on in CI first.

Two refinements from the build checks page:

  • # check=skip=JSONArgsRecommended,StageNameCasing turns off rules you have decided against. skip=all turns them all off.
  • Pin the # syntax= line when you use error=true. Newer frontends add checks, and without a pinned version an upgrade can fail a build that passed yesterday. The reference recommends exactly this.

The directives combine with a semicolon: # check=skip=JSONArgsRecommended;error=true. Parser directives must be the very first lines of the file. After any other comment or instruction they are ignored as plain comments.

hadolint

hadolint, currently v2.15.1, parses the Dockerfile itself and also runs ShellCheck over the shell code in RUN lines. Its rules lean towards reproducibility and hygiene. A few from its README:

Rule Severity Says
DL3007 Warning Using latest is prone to errors. Pin a tag.
DL3008 Warning Pin versions in apt-get install.
DL3015 Info Use --no-install-recommends.
DL3002 Warning The last USER should not be root.
DL4006 Warning Set SHELL ["/bin/bash", "-o", "pipefail", "-c"] before a RUN with a pipe.

It runs as a container, so CI needs nothing installed:

docker run --rm -i hadolint/hadolint < Dockerfile
docker run --rm -i hadolint/hadolint hadolint --ignore DL3008 --failure-threshold warning - < Dockerfile

A .hadolint.yaml in the repository holds ignored rules, trusted registries and the failure threshold, so local runs and CI agree. An inline # hadolint ignore=DL3008 above one line silences a single case, with the reason in the comment.

The two tools overlap on a few rules, such as JSON-form CMD. Build checks understand BuildKit semantics, such as undefined variables across stages and secrets in ARG. hadolint understands shell. Running both costs seconds.

About the video

The embed is Nick Janetakis’s “Lint your Dockerfile with Hadolint”, November 2021, 10.5 minutes. Its chapters cover adding hadolint to his example projects, running it as a Docker container, ignoring specific rules such as apt version pinning, the issues it found, customizing the config file and setting which severities produce a non-zero exit code. It predates BuildKit’s build checks by more than two years, so the first half of this lesson is from Docker’s documentation and the BuildKit source. English was judged from the channel and the English caption track. Manuel Castellin’s video in deeper is a shorter hadolint tour.

What was run: the ten warnings are real output from a Go program calling dockerfile2llb.DockerfileLint in BuildKit v0.35.1, the function behind docker build --check, with base image configs fetched from Docker Hub. The rule count is from that version’s linter source. The hadolint rules and flags are from its README, and the directive behaviour from the build checks page, checked 2026-11-28. Neither docker build nor hadolint itself was run.

Where this fits

That closes the Dockerfile group. Next, the engine underneath every build in it: BuildKit and docker buildx.

Check yourself

  1. 01Your pipeline runs docker build --check on a Dockerfile with a shell-form CMD. The step passes. Why, and how do you make it fail?

    Check violations are warnings with exit code 0 by default. Add # check=error=true to the Dockerfile, or pass --build-arg BUILDKIT_DOCKERFILE_CHECK=error=true, so violations exit non-zero.

  2. 02The linter reports UndefinedVar for ENV PATH=/app/bin:$PTH. What bug did it find?

    A typo. $PTH is never defined, so it expands to nothing and PATH loses everything after /app/bin:. Without the check the image builds fine and fails at run time.

  3. 03Why should a Dockerfile with # check=error=true also pin its # syntax= line?

    New Dockerfile frontend versions add new checks. Without a pinned syntax version, an upgrade can introduce a rule your Dockerfile violates and suddenly fail builds that passed yesterday.

  4. 04What does hadolint check that BuildKit's build checks do not?

    The shell code inside RUN, through ShellCheck, and practices such as pinned apt package versions (DL3008), --no-install-recommends (DL3015) and not ending as root (DL3002).

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark