Sub topic 7
Reading common check warnings and fixing them
This chapter assumes you can run docker build --check and write a basic Dockerfile with FROM, RUN, COPY, and CMD. You will learn to read what a check warning says and fix the line it points to.
Anatomy of a warning
Each build check has a rule name in CamelCase, a short message, and the line number of the instruction that triggered it. Rule names are the same ones the check directive uses to skip rules, so it helps to remember them.
Here is a Dockerfile that breaks two rules on purpose:
# syntax=docker/dockerfile:1
FROM alpine:3.20 AS Build
CMD echo helloRun the checks without building:
docker build --check .The output is similar to this. The exact wording and link format can vary between Docker versions.
WARNING: StageNameCasing - https://docs.docker.com/go/dockerfile/rule/stage-name-casing/
Stage name 'Build' should be lowercase
Dockerfile:2
--------------------
2 | >>> FROM alpine:3.20 AS Build
3 | CMD echo hello
--------------------
WARNING: JSONArgsRecommended - https://docs.docker.com/go/dockerfile/rule/json-args-recommended/
JSON arguments recommended for CMD to prevent unintended behavior related to OS signals
Dockerfile:3Read each warning in three steps:
- Note the rule name. It tells you which category of problem you have.
- Read the message. It usually names the offending value, such as the stage name
Build. - Open the rule's documentation link for the reason behind the rule and a corrected example.
Fixing the two warnings
StageNameCasing asks for lowercase stage names. The fix is to rename the stage everywhere it appears. If a later stage uses COPY --from=Build, change that reference too.
JSONArgsRecommended flags shell form for CMD and ENTRYPOINT. Shell form wraps your command in /bin/sh -c. The shell becomes the main process, and it does not forward signals such as SIGTERM to your program. As a result, docker stop waits for the timeout and then kills the container. The exec (JSON) form runs your program directly.
# syntax=docker/dockerfile:1
FROM alpine:3.20 AS build
CMD ["echo", "hello"]Run docker build --check . again. It should report no warnings.
Other rules you will meet
| Rule | What it flags | Typical fix |
|---|---|---|
| `FromAsCasing` | `FROM` and `AS` written in different cases | Use `FROM ... AS ...` in matching case |
| `ConsistentInstructionCasing` | Mixed upper- and lowercase instructions | Pick one case, usually uppercase |
| `LegacyKeyValueFormat` | `ENV KEY value` without `=` | Write `ENV KEY=value` |
| `MaintainerDeprecated` | The `MAINTAINER` instruction | Use `LABEL org.opencontainers.image.authors="[email protected]"` |
| `UndefinedVar` | A `$VAR` that no `ARG` or `ENV` defines | Declare it, or fix the typo in the name |
| `SecretsUsedInArgOrEnv` | Names like `PASSWORD` or `TOKEN` in `ARG` or `ENV` | Use a build secret mount instead |
| `WorkdirRelativePath` | `WORKDIR app` without a leading `/` | Use an absolute path |
The last two matter most. Values in ARG and ENV stay visible in image metadata, so a secret placed there leaks.
Not every warning is a bug
Some warnings point to real defects, such as UndefinedVar catching a misspelled variable. Others are style preferences, such as the casing rules. Fix the defects first. For a style rule you disagree with, you can skip it with the check directive, which the next chapter covers.
Exercise
A teammate's Dockerfile has the line ENV APP_HOME /srv/app and the line CMD node server.js. Which two rules does docker build --check report, and what are the corrected lines?
Show answer
LegacyKeyValueFormat flags the ENV line because it has no =, so write ENV APP_HOME=/srv/app. JSONArgsRecommended flags the CMD line because it uses shell form, so write CMD ["node", "server.js"].