Sub topic 4
Replacing chained && commands with RUN heredocs
This chapter assumes you can write a basic Dockerfile with FROM, RUN, COPY, and CMD. It also assumes you can run docker build and know what a POSIX shell here-document is. Builds use BuildKit, which is the default builder in current Docker releases.
The problem with chained commands
A single RUN instruction creates one image layer. To keep layers few and small, people chain commands with && and break lines with backslashes:
RUN set -e && \
apk add --no-cache curl && \
curl --versionThis works, but it is easy to get wrong. A missing backslash ends the instruction early. A missing && lets a failing command go unnoticed. Long chains are also hard to read and hard to edit.
Enabling heredocs
Heredoc support in RUN is a feature of the BuildKit Dockerfile frontend. A frontend is the component that parses your Dockerfile and turns it into build steps. The syntax directive on the first line selects the stable frontend, so the heredoc syntax is available.
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN <<EOF
set -e
apk add --no-cache curl
curl --version
EOFThe parts are:
<<EOFafterRUNopens the heredoc.EOFis the delimiter, and you can choose another word.- The lines that follow are the body. They are the script the shell runs.
- A line containing only the delimiter closes the heredoc.
The whole body runs as one RUN instruction, so it produces one layer. You do not need && or backslashes. Each command goes on its own line.
Error handling
The body runs in the image's default shell, which is /bin/sh -c unless you changed it with SHELL. Without options, the shell keeps going after a failed command, and the step's exit status is that of the last command. A failure in the middle can therefore go unnoticed.
The first line of the body in the example is set -e. It makes the shell stop at the first failing command, which restores the behavior that && chains gave you.
Why this helps
Compare how the two styles behave when you edit them:
| Concern | Chained `&&` | RUN heredoc |
|---|---|---|
| Adding a command | Add `&&` and a backslash | Add a line |
| Forgotten backslash | Breaks the instruction | Not applicable |
| Layers created | One | One |
| Readability | Lower for long scripts | Close to a plain shell script |
The layer count is the same. The gain is maintainability.
How the build processes a heredoc
The diagram shows what happens when the builder reads a RUN heredoc.
Common errors
- Missing
syntaxline. Recent Docker releases usually accept heredocs without it, but older frontends may not. Adding the directive makes the requirement explicit. - Delimiter not alone on its line. The closing delimiter must be the only text on its line. Trailing spaces can stop it from matching.
- No shebang needed for
RUN. The body runs through the shell directly. A shebang line such as#!/bin/shis only needed when you write the body to a file, which a later chapter covers. - Expecting a different shell. If your body uses Bash features on an Alpine image, it fails, because Alpine has only
shby default.
Try it
Save the first example as Dockerfile and run the build:
docker build --progress=plain -t heredoc-demo .With --progress=plain, the output shows the body running as a single step, ending with the output of curl --version.
Exercise
A RUN heredoc contains two commands. The first one fails, and the second one succeeds. The body does not start with set -e. Does the build step fail, and why?
Show answer
No, the step succeeds. The shell continues after the failed command, and the step's exit status comes from the last command, which succeeded. Adding set -e as the first line makes the shell stop at the first failure.
Exercise
Rewrite RUN apt-get update && apt-get install -y curl && curl --version as a RUN heredoc that stops on the first error.
Show answer
RUN <<EOF set -e apt-get update apt-get install -y curl curl --version EOF