Sub topic 6
Linting your Dockerfile with docker build --check
This chapter assumes you can write a basic Dockerfile with FROM, RUN, COPY, and CMD, and that you can run docker build and read its output. It also assumes you know that current Docker releases build with BuildKit by default.
What build checks are
Build checks are lint rules that BuildKit applies to a Dockerfile. A linter is a tool that reads your source and warns about risky or inconsistent style without running it. Each rule has a name such as StageNameCasing or JSONArgsRecommended, a short description, and a link to documentation that explains the fix.
Checks are part of the Dockerfile frontend. The frontend is the component that parses your Dockerfile and turns it into build steps. For this reason, the # syntax=docker/dockerfile:1 line you saw earlier matters. It selects a stable frontend release, and newer frontends can ship newer rules.
Checks run on every normal docker build. If a rule fires, the build still succeeds, and you see the warnings in the output. This is useful for catching problems early, but it is easy to overlook warnings in a long log.
Running checks without building
The --check flag runs only the checks and reports the warnings. It does not execute the build, so it is fast and has no side effects on your image cache.
docker build --check .The command exits with status 0 if no rule fires. If any warning appears, it still reports them, but the exit status stays 0 unless you tell BuildKit to treat warnings as errors. The next chapter covers that setting.
A Dockerfile that breaks two rules
Here is a file that deliberately breaks two rules.
# syntax=docker/dockerfile:1
# check=error=true
FROM alpine:3.20 AS Build
CMD echo helloThe second line is a check directive. It uses error=true to make warnings fail the build. You will learn how it works in a later chapter. For now, notice that two things are wrong with the file:
StageNameCasingfires because the stage nameBuildis not lowercase. Docker expects stage names in lowercase so references to them stay consistent.JSONArgsRecommendedfires becauseCMD echo hellouses shell form. Shell form runs your command through/bin/sh -c, so the shell becomes the main process and may not receive signals such asSIGTERM. JSON (exec) form runs the program directly.
Reading a warning
Each warning has the same parts. Look for these when you read the output:
- The rule name, for example
StageNameCasing. - A one-line description of the problem.
- The file name and line number.
- A link to the rule's documentation page.
The line number points at the instruction that triggered the rule. Open the linked page when the fix is not obvious, because it shows a bad example and a good one.
Fixing the warnings
Both fixes are small. Make the stage name lowercase and switch CMD to JSON form.
# syntax=docker/dockerfile:1
FROM alpine:3.20 AS build
CMD ["echo", "hello"]Run docker build --check . again. A clean file prints a short confirmation that the check completed with no warnings.
A caution on JSON form: it requires double quotes and a valid JSON array. Single quotes make Docker fall back to shell form, and the rule can fire again.
How checks fit into your workflow
The following diagram shows where --check sits compared with a full build.
Common errors
- Warnings appear but the build passes. This is the default. Checks only fail a build when you set
error=true. - A rule you expected does not fire. An old frontend may not include it. Pin a current one with the
syntaxdirective. - Warnings you cannot fix right now. You can skip individual rules, which a later chapter explains.
Exercise
You run docker build --check . on a Dockerfile containing CMD echo hello. The command prints a JSONArgsRecommended warning. What does the command do besides print the warning, and how do you fix the instruction?
Show answer
It does not build the image, because --check only runs the checks. Fix the instruction by switching to JSON form: CMD ["echo", "hello"].