Sub topic 2
Choosing a Dockerfile Frontend with the syntax Directive
This chapter assumes you can write a basic Dockerfile and run docker build. It also assumes your Docker release uses BuildKit by default. You should know that a parser directive is a special comment at the top of a Dockerfile, which the previous chapter covered.
What a frontend is
BuildKit does not read a Dockerfile directly. A frontend is a component that parses the Dockerfile and turns it into the low-level build graph BuildKit executes. The Dockerfile frontend ships as a container image, docker/dockerfile.
Without any directive, BuildKit uses the frontend bundled with your Docker or BuildKit version. Its features therefore depend on the version of the tool you happen to have installed.
The syntax directive overrides that choice. It tells BuildKit to pull a frontend image and use it to parse the file.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN echo "built with a selected frontend"The directive must be on the first line, before any instruction, blank line, or other comment. If anything precedes it, BuildKit treats it as an ordinary comment and ignores it, without an error. This is a common cause of "my heredoc syntax is rejected" reports.
How the directive is resolved
When you build, BuildKit reads the first line, pulls the named image if it is not cached, and runs it as the frontend. The frontend then reads your Dockerfile and returns the build graph.
Pulling the image means the first build needs registry access. Later builds reuse the cached image unless the tag has moved and BuildKit checks for a newer one.
Why select a frontend
Selecting a frontend gives you three things:
- New syntax on older Docker. Features such as heredocs and build checks arrive in the frontend. With the directive, you can use them without upgrading the Docker engine.
- Consistency across machines. A laptop and a CI runner with different Docker versions parse the file the same way.
- Bug fixes. Frontend fixes reach you without an engine upgrade.
Heredocs and build checks, covered in later chapters, depend on a recent frontend. Starting every Dockerfile with the directive is a cheap way to make sure they work.
Choosing a tag
The image tag controls how much the frontend can change under you.
| Directive | Meaning | Trade-off |
|---|---|---|
| `docker/dockerfile:1` | Latest stable 1.x release | Gets new features and fixes, stays backward compatible |
| `docker/dockerfile:1.7` | Latest patch of 1.7 | Only bug fixes within that minor version |
| `docker/dockerfile:1.7.1` | Exact release | Fully fixed, no automatic fixes |
| `docker/dockerfile:labs` | Experimental channel | May change or break |
For most projects, docker/dockerfile:1 is the right default. Use a more specific tag when you need identical parsing over time. The next chapters on reproducibility cover pinning by digest.
Common errors
- Directive not on line 1. A leading blank line or comment disables it silently. Check the top of the file first.
- Space or case mistakes. The form is
# syntax=docker/dockerfile:1. Directive names are case-insensitive, but keep the lowercase form so the file is easy to scan. - Offline builds. The frontend image must be pullable or already cached. In an air-gapped network, mirror the image or omit the directive.
- Repeated directive. Each directive may appear only once, and directives must be grouped together at the top.
Try it
This Dockerfile uses a heredoc, which only works if the frontend supports it. With the directive in place it builds on any BuildKit-enabled Docker.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN <<EOF
set -e
apk add --no-cache curl
curl --version
EOFBuild it with docker build .. In the build output, look for a step that loads docker/dockerfile:1 metadata. That line confirms BuildKit resolved your frontend.
Exercise
A Dockerfile begins with a blank line, then # syntax=docker/dockerfile:1, then FROM alpine:3.20. Is the directive applied? What happens, and how do you fix it?
Show answer
No. A directive must be on the first line, before any blank line, comment, or instruction. BuildKit treats the misplaced line as a normal comment and uses the bundled frontend, with no error. Remove the blank line so the directive is line 1.
Exercise
You want identical parsing for every build over the next year, but you do not want to maintain the tag by hand more than necessary. Which is a better fit, docker/dockerfile:1 or docker/dockerfile:1.7.1, and what do you give up?
Show answer
docker/dockerfile:1.7.1 fixes the exact release, so parsing never changes. You give up automatic bug fixes and new features, and you must update the tag yourself. docker/dockerfile:1 receives updates automatically but can change behavior within the 1.x line.