Sub topic 1
Parser directives: what they are and where they go
Prerequisites
This chapter assumes you can do the following:
- Write a basic Dockerfile with
FROM,RUN,COPY, andCMD. - Run
docker buildand read its output. - Use BuildKit, which is the default builder in current Docker releases.
You don't need to know heredocs or build checks yet. Later chapters cover them.
What a parser directive is
A parser directive is a special comment that changes how the builder reads the rest of the Dockerfile. It doesn't add a layer to the image and it doesn't run during the build. The builder reads it before it parses any instructions.
A directive has this form:
# directive=valueDocker supports three directives:
| Directive | What it controls |
|---|---|
| `syntax` | Which Dockerfile frontend parses and builds the file |
| `escape` | The character that escapes line endings and special characters |
| `check` | How build checks behave, such as which rules to skip |
Directive names are case-insensitive, but lowercase is the convention. The value is case-sensitive in the sense that it is passed on as written, so write it exactly as the documentation shows.
Placement rules
Directives are easy to misplace, and a misplaced one is silently treated as an ordinary comment. The rules are:
- Directives must be at the very top of the Dockerfile.
- Nothing may come before them except other directives. That includes blank lines, normal comments, and instructions such as
FROM. - Each directive can appear only once.
- Once the builder sees a blank line, a normal comment, or an instruction, it stops looking for directives. Any later
# name=valueline is just a comment.
Because rule 4 also covers line breaks, you can't put a blank line between two directives. Keep them on consecutive lines.
This Dockerfile places its directives correctly:
# syntax=docker/dockerfile:1
# escape=\
FROM alpine:3.20
RUN echo "directives were read"This one does not:
# Build the demo image
# syntax=docker/dockerfile:1
FROM alpine:3.20The first line is a normal comment, so the builder has already left the directive section. The syntax line is ignored, and Docker uses the frontend bundled with your builder instead. The build still succeeds, which is why this mistake is hard to spot.
Combining directives
You can use several directives in one file, each on its own line, with no blank lines or comments between them:
# syntax=docker/dockerfile:1
# escape=\
# check=error=true
FROM alpine:3.20The check directive accepts several options in one line, separated by semicolons. For example, # check=skip=JSONArgsRecommended;error=true sets two options. Later chapters explain what each option does. For now, the point is that one directive line can carry more than one setting.
How the builder reads the file
The order matters because the syntax directive can change which frontend does the parsing. The builder therefore has to read directives first, before it knows how to interpret anything else.
Common mistakes
- A comment above the directive. A license header or description on line 1 pushes the directive out of the directive section. Put the directives first and the comments after them.
- A blank line between directives. The blank line ends the directive section, so every directive after it is ignored.
- A repeated directive. Declaring
syntaxtwice is invalid. Keep one line per directive. - A directive after
FROM. Instructions end the directive section, so the line becomes a comment.
Exercise
A Dockerfile starts with the line # Base image for the web app, followed by # syntax=docker/dockerfile:1, then FROM alpine:3.20. Does the build use the syntax directive? Explain, and show the corrected first lines.
Show answer
No. The comment on line 1 ends the directive section, so the syntax line is treated as an ordinary comment and is ignored. No error is reported. The corrected file puts the directive first: # syntax=docker/dockerfile:1, then # Base image for the web app, then FROM alpine:3.20.
Exercise
A Dockerfile has # syntax=docker/dockerfile:1, then a blank line, then # check=error=true, then FROM alpine:3.20. Which directive takes effect, and what change makes both work?
Show answer
Only syntax takes effect. The blank line ends the directive section, so check is read as a normal comment. Remove the blank line so both directives sit on consecutive lines at the top of the file.