Sub topic 8
Skipping rules and failing on warnings with the check directive
This chapter assumes you can run docker build --check and read its warnings, as covered earlier. You also need to know that parser directives are special comments at the top of a Dockerfile. Here you will use the check directive to change how build checks behave: which rules run, and whether a warning stops the build.
What the check directive does
By default, build checks only warn. The build still succeeds, and the warnings appear in the output. The check directive is a parser directive that changes this behavior from inside the Dockerfile. It supports two options:
| Option | Value | Effect |
|---|---|---|
| `skip` | Comma-separated rule names, or `all` | Those rules are not evaluated |
| `error` | `true` | Any remaining warning fails the build |
You can combine options on one line by separating them with a semicolon. Use commas only between rule names inside skip.
Placement rules
The check directive follows the same rules as syntax and escape:
- It must appear before any instruction, blank line, or ordinary comment.
- Each directive goes on its own line, and the directive lines must be consecutive.
- It is written as
# check=followed by the options.
If you put it after FROM, Docker treats it as a normal comment and silently ignores it. Your checks then keep their default behavior, which is a common source of confusion.
Making warnings fail the build
This Dockerfile breaks two rules on purpose. StageNameCasing flags the uppercase stage name Build. JSONArgsRecommended flags the shell-form CMD.
# syntax=docker/dockerfile:1
# check=error=true
FROM alpine:3.20 AS Build
CMD echo helloBecause of error=true, both warnings now fail the build, and docker build . exits with a non-zero status. You can also run the checks without building:
docker build --check .With error=true in the file, this command fails too, so it works as a quick lint step.
Skipping specific rules
Sometimes a rule does not fit your project. Perhaps you deliberately use shell-form CMD because you need shell expansion. List the rule in skip so it no longer counts:
# syntax=docker/dockerfile:1
# check=skip=JSONArgsRecommended,StageNameCasing;error=true
FROM alpine:3.20 AS Build
CMD echo helloBoth named rules are skipped here. The error=true option still applies to every other rule. This file produces no warnings, so the build succeeds.
Fix first, skip second
Skipping hides a problem and does not remove it. Before you add a rule to skip, try the fix:
StageNameCasing: renameAS BuildtoAS build.JSONArgsRecommended: changeCMD echo hellotoCMD ["echo", "hello"].
Skip a rule only when the flagged pattern is intentional. Add a comment elsewhere in the repository explaining why, because the directive line itself must stay comment-free above it.
Common mistakes
- Placing the directive below
FROMor below a blank line. It is then ignored. - Using a semicolon between rule names. Use commas for rules and semicolons between options.
- Misspelling a rule name. Run
docker build --check .and copy the name exactly as it appears in the warning. - Adding
error=trueto an existing project and being surprised by many failures. Run--checkfirst, fix or skip what it reports, and then turn onerror=true.
Exercise
A Dockerfile has # check=error=true on line 5, after FROM alpine:3.20, and CMD echo hi on line 6. You run docker build .. Does the build fail, and why? What single change makes the intended behavior work?
Show answer
The build does not fail because of checks. The directive is below an instruction, so Docker treats it as an ordinary comment and the checks keep their default warn-only behavior. Move # check=error=true to the top of the file, before FROM, with no blank lines before it. Then the JSONArgsRecommended warning on the shell-form CMD fails the build.
Exercise
Write a check directive line that skips only StageNameCasing and also makes any other warning fail the build.
Show answer
# check=skip=StageNameCasing;error=true