Sub topic 12
The escape directive for Windows-style paths
This chapter assumes you know the other parser directives (syntax, check) and how a Dockerfile is laid out. It covers the escape directive, which matters mostly when you build Windows container images.
What the escape character does
Every Dockerfile has an escape character. It has two jobs:
- It joins a long instruction across several lines when it ends a line.
- It escapes a character, so the parser treats that character literally.
The default is the backslash (\). That works on Linux, where paths use forward slashes. Windows paths use backslashes, so the default causes trouble there.
The problem with backslashes
Take this instruction for a Windows image:
COPY app.txt C:\app\
RUN echo doneThe first line ends in a backslash, so the parser reads it as a line continuation. It joins the next line onto it, and the COPY destination turns into C:\app\RUN echo done. The build fails or does something you did not intend.
Switching to a backtick
The escape directive changes the escape character. It accepts only two values: the backslash and the backtick (` ``). Choosing the backtick frees the backslash to be an ordinary path separator.
# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2022
WORKDIR C:\app
COPY app.txt C:\app\
RUN mkdir C:\data `
&& echo built > C:\data\build.txtIn this file:
COPY app.txt C:\app\now ends with a plain path separator.- The
RUNline ends with a backtick, so the next line continues the same instruction.
Building this image needs a Windows host that runs Windows containers.
Placement rules
escape is a parser directive, so it follows the same rules as syntax and check:
- It is written as a special comment,
# escape=followed by the value. - It must appear before any ordinary comment, blank line, or instruction. After the first of those, the parser stops looking for directives and treats later directive-style lines as plain comments.
- Each directive can appear only once.
- Directive names are case-insensitive, but lowercase is the convention.
You can combine directives, each on its own line at the top:
# syntax=docker/dockerfile:1
# escape=`
FROM mcr.microsoft.com/windows/servercore:ltsc2022Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Paths ending in `\` swallow the next line | The default backslash is still the escape character | Add `# escape=` followed by a backtick at the top |
| The directive seems ignored | A blank line or comment sits above it | Move it to the first lines of the file |
| Backslash continuations stop working | You switched to the backtick | End continued lines with a backtick instead |
| Error about an invalid escape value | The value is neither backslash nor backtick | Use one of those two characters |
Escaping inside a backtick Dockerfile
With the backtick as escape character, a backtick before a character makes that character literal for the Dockerfile parser. It does not change how the shell inside a RUN command reads its input. In PowerShell, the backtick is also PowerShell's own escape character, so a RUN line that uses the PowerShell shell can contain backticks that mean different things to the two parsers. Keep such lines short, or move complex logic into a script file.
Should you use it?
Use escape only for Windows images, where backslash paths are common. For Linux images, keep the default. A directive that changes the continuation character makes your file look unusual to readers who expect backslashes.
Exercise
A Windows Dockerfile has the line # escape= followed by a backtick on line 3, after FROM on line 1 and a blank line on line 2. The build still treats a trailing backslash as a continuation. Why, and what is the fix?
Show answer
Parser directives must come before any comment, blank line, or instruction. Here FROM and a blank line come first, so the parser treats line 3 as an ordinary comment and the escape character stays the backslash. Move the escape directive to the first line of the file, before FROM.
Exercise
Which two characters can the escape directive select, and which character ends a continued line after you pick the non-default one?
Show answer
The backslash and the backtick. If you select the backtick, a continued line ends with a backtick instead of a backslash.