Sub topic 1
Enabling BuildKit features with the syntax directive
BuildKit is the build engine behind docker build. It runs build steps in parallel, skips unused stages, and supports extra RUN options such as --mount. This chapter shows how to make sure those features are available to your Dockerfile. Later chapters use them for cache mounts and secret mounts.
Prerequisites
This chapter assumes you can:
- Write a basic Dockerfile with
FROM,COPYandRUN. - Run
docker buildfrom the command line. - Describe image layers and layer caching at a basic level.
You also need a recent Docker Engine (version 23.0 or later) or Docker Desktop. Check your version with docker version.
BuildKit and the Docker CLI
Docker has two build engines. The legacy builder is the old one. BuildKit is the newer one. Since Docker Engine 23.0 and Docker Desktop 4.19, BuildKit is the default builder on Linux, macOS and Windows.
On older installations you can turn BuildKit on for a single build with an environment variable:
DOCKER_BUILDKIT=1 docker build -t myapp .You can also run docker buildx build, which always uses BuildKit. Check which builder you have:
docker buildx version
docker buildx lsUsing BuildKit is only half of the setup. The Dockerfile must also be parsed by a frontend that understands the newer instructions.
The frontend and the syntax directive
BuildKit does not read the Dockerfile directly. A component called the frontend parses the Dockerfile and turns it into build steps. By default, the frontend is the one bundled with your Docker installation. Its version depends on the version of Docker you installed.
The syntax directive is a special comment on the first line of the Dockerfile. It tells BuildKit to download a specific frontend image and use that instead of the bundled one:
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN echo "built with a pinned frontend"The value docker/dockerfile:1 is an image on Docker Hub. The tag 1 means the latest release of version 1 of the Dockerfile syntax. It receives new features and bug fixes but not breaking changes.
This has two effects:
- You get new Dockerfile features without upgrading Docker itself.
- Every machine that builds the file uses the same frontend, so builds behave the same on a laptop and in CI.
The first time you build, BuildKit pulls the frontend image. It caches the image afterwards. This means the first build needs network access to Docker Hub or your configured registry.
How the pieces fit together
Choosing a version tag
You can pin the frontend more or less tightly.
| Directive | Meaning | Trade-off |
|---|---|---|
| `# syntax=docker/dockerfile:1` | Latest 1.x release | Gets new features. Can change between builds. |
| `# syntax=docker/dockerfile:1.7` | Latest 1.7.x release | Gets only bug fixes. |
| `# syntax=docker/dockerfile:1.7.1` | Exactly 1.7.1 | Fully reproducible. No automatic fixes. |
Most projects use docker/dockerfile:1. Use a more specific tag when you need identical builds over a long time.
Checking that the mount syntax works
The RUN --mount option needs BuildKit and a frontend that supports it. The following Dockerfile tests both. The --mount=type=cache option is explained in later chapters. Here it only shows that the syntax is accepted.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN --mount=type=cache,target=/tmp/demo \
echo "mount syntax accepted"Build it and show plain output so you can see each step:
docker build --progress=plain -t mount-check .If the build succeeds, your setup supports mounts.
Common errors
the --mount option requires BuildKit: you are using the legacy builder. SetDOCKER_BUILDKIT=1, usedocker buildx build, or upgrade Docker.Unknown flag: mount: the frontend is too old, or the directive is not on the first line. Add or move# syntax=docker/dockerfile:1.- Failed to pull the frontend image: the build machine cannot reach the registry. Allow network access, or mirror the frontend image in a registry you can reach.
Exercise
A Dockerfile starts with a blank line, then # syntax=docker/dockerfile:1, then FROM alpine. It fails with "Unknown flag: mount" on a RUN --mount step. What is the likely cause and the fix?
Show answer
The syntax directive is not on the first line, so BuildKit ignores it and uses the older bundled frontend. Remove the blank line so the directive is the first line of the file.
Exercise
Why might a team choose docker/dockerfile:1.7.1 instead of docker/dockerfile:1?
Show answer
An exact tag pins the frontend, so every build uses the same parser. This gives fully reproducible builds. The cost is that bug fixes and new features arrive only when someone updates the tag.