Sub topic 13
Making checks part of CI
This chapter assumes you can run docker build --check . and read its warnings. You also need to know how the check directive sets skip and error. The goal here is to run those checks automatically on every change, so a bad Dockerfile can't merge.
Why run checks in CI
Local checks depend on each developer remembering to run them. A CI job runs the same checks on every pull request. It uses the same Docker version and the same frontend, so a Dockerfile is judged the same way on every machine.
Checks are fast because --check never builds the image. It parses the Dockerfile, evaluates the rules, and reports. You can run it as a cheap first job, before the slower build and push steps.
Make warnings fail the job
By default, check warnings are only reported and the command still exits with status 0. CI treats a zero exit as success, so warnings would pass unnoticed. Set error=true in the Dockerfile so that any violation produces a non-zero exit code:
# syntax=docker/dockerfile:1
# check=error=true
FROM alpine:3.20 AS build
CMD ["echo", "hello"]Keeping this setting in the Dockerfile means the rule applies on every developer's machine too, not only in CI. Local builds fail the same way CI does.
If you want CI to be stricter than local builds, you can pass the same option as a build argument instead:
docker build --check --build-arg BUILDKIT_DOCKERFILE_CHECK="error=true" .A minimal GitHub Actions job
The runner image already has Docker, so a single run step is enough:
name: dockerfile-checks
on:
pull_request:
paths:
- "Dockerfile"
- ".github/workflows/dockerfile-checks.yml"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check the Dockerfile
run: docker build --check .The paths filter runs the job only when the Dockerfile or the workflow itself changes. If you use docker/build-push-action, version 6.6 and later can run the same checks with call: check instead of a shell step. The action also shows the warnings in the job summary.
The same command works in any other CI system, because only the exit code matters. For a Dockerfile in another location, pass -f and the context:
docker build --check -f services/api/Dockerfile services/apiKeep the results stable
Checks come from the Dockerfile frontend, and the syntax directive chooses that frontend. With # syntax=docker/dockerfile:1, you get the newest 1.x release. A newer frontend can add rules. With error=true, a rule you have never seen can suddenly fail an unrelated pull request.
You have two ways to avoid surprise failures:
- Pin the frontend to a version tag or digest, and update it on purpose in its own pull request.
- Leave the frontend floating, and accept that CI may occasionally fail after a frontend release. Fix the warnings or add a
skipentry.
Pinning is the better choice for teams where a red build blocks many people.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| No `error=true` | Warnings print but CI passes | Add `# check=error=true` |
| Skipping a rule with `skip=all` | No checks run at all | Skip only the named rules you have decided to accept |
| Checking only on release | Problems found late | Run the job on every pull request |
| Floating frontend with `error=true` | New rules fail unrelated pull requests | Pin the frontend version |
Summary of the workflow
- Put
# syntax=and# check=error=trueat the top of each Dockerfile. - Add a CI job that runs
docker build --checkfor each Dockerfile. - Run that job before the image build.
- Pin the frontend if surprise failures cost you more than occasional upgrades.
Exercise
Your CI job runs docker build --check ., but it passes even though the output lists two warnings. What is the likely cause, and what is the smallest change that fixes it?
Show answer
By default, check warnings don't change the exit code, so CI sees success. Add # check=error=true as a parser directive at the top of the Dockerfile, after the syntax line and before the first instruction. The job then exits with a non-zero status when any rule is violated.