Sub topic 3
Heredocs in a Dockerfile: the basic idea
This chapter assumes you can write a basic Dockerfile with FROM, RUN, COPY, and CMD. It also assumes you know POSIX shell here-documents and that your builds run on BuildKit, the default builder in current Docker releases.
The problem heredocs solve
Multi-step RUN instructions are usually chained with && and line-continuation backslashes:
FROM alpine:3.20
RUN set -e && \
echo "step one" && \
echo "step two"This works, but every line needs a trailing \ and &&. A missing one breaks the build, and the commands are hard to read and edit.
A heredoc (here-document) lets you write the commands as a plain block of lines instead.
What a heredoc is
A heredoc is a way to give an instruction a multi-line block of text. You write << followed by a delimiter word on the instruction line. The following lines are the body. The body ends at the first line that contains only the delimiter.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN <<EOF
set -e
echo "step one"
echo "step two"
EOFHere EOF is the delimiter. The two echo lines and set -e are the body. The whole block is one RUN instruction, so it produces one image layer, the same as the chained version.
The delimiter can be any word. EOF is the common choice. Use a different word if the body itself contains a line that says EOF.
How the parser reads it
The Dockerfile parser normally treats each line as part of an instruction. When it sees <<WORD in an instruction that supports heredocs, it keeps reading lines as the body until it finds WORD alone on a line. Then it hands the body to the instruction.
Where heredocs work
Heredocs are supported in these instructions:
RUN, where the body is a script or the standard input of a command.COPYandADD, where the body becomes the content of a file in the image.
Other instructions, such as ENV or CMD, do not accept them. The next chapters cover RUN and COPY in detail. This chapter only sets up the shared idea.
Requirements
Heredoc support lives in the Dockerfile frontend, the component that parses the Dockerfile and turns it into build steps. It arrived in frontend version 1.4. Recent Docker releases bundle a frontend that supports it. Start the file with the syntax directive anyway, so the build uses a known stable frontend whatever the local Docker version:
# syntax=docker/dockerfile:1The directive must be the first line. A later chapter covers directive placement and pinning.
Common errors
The delimiter line is matched exactly. These mistakes cause failures:
- Extra characters on the closing line. A trailing space, or
EOFindented with spaces, means the parser does not see the end of the body. It keeps reading and then fails with an error about an unterminated heredoc, or swallows the following instructions. - Mismatched delimiter. Opening with
<<EOFand closing withENDhas the same effect. - Using a heredoc in an unsupported instruction. The body is parsed as ordinary Dockerfile lines, which usually produces an "unknown instruction" error.
Checking your result
Build the first example and confirm both lines appear in the output:
docker build --progress=plain --no-cache .You should see step one and step two printed under a single RUN step.
Exercise
A Dockerfile contains RUN <<EOF, a few shell commands, and then a closing line EOF (EOF followed by a trailing space). The build fails or later instructions are swallowed. Why, and how do you fix it?
Show answer
The parser matches the closing delimiter exactly, so EOF with a trailing space is treated as another body line and the heredoc never ends. Remove the trailing space so the line contains only EOF.