Sub topic 9
Quoting, variable expansion, and the <<- form
This chapter assumes you can write a basic Dockerfile and that you know POSIX here-documents. It also assumes you have read the earlier chapters on RUN and COPY heredocs. Here you will control when $VARIABLE is replaced inside a heredoc, and how to indent heredoc bodies without changing their content.
Who expands the variable?
Two programs can expand $NAME in a heredoc: the Dockerfile frontend at build time, and the shell at run time. Which one acts depends on the instruction and on how you write the delimiter.
- In
COPYandADDheredocs, no shell runs. The frontend writes the file. An unquoted delimiter lets the frontend substituteARGandENVvalues. A quoted delimiter keeps the text literal. - In
RUNheredocs, the body is handed to the shell (or to the interpreter you named). The shell's own quoting rules then apply.
Expansion in COPY heredocs
This Dockerfile writes two files from nearly identical heredocs:
# syntax=docker/dockerfile:1
FROM alpine:3.20
ARG GREETING=hello
COPY <<EOF /expanded.txt
greeting=$GREETING
EOF
COPY <<"EOF" /literal.txt
greeting=$GREETING
EOF
CMD ["sh", "-c", "cat /expanded.txt /literal.txt"]After a build, expanded.txt contains greeting=hello and literal.txt contains greeting=$GREETING. Single quotes (<<'EOF') behave the same as double quotes.
A variable that is not an ARG or ENV in the Dockerfile expands to an empty string in the unquoted form. That is why the earlier script example quoted its delimiter: $HOME inside a shell script must survive until the script runs.
Expansion in RUN heredocs
With RUN, the shell reads the body, so the usual shell rules apply. Variables set with ENV are already in the shell's environment, and ARG values are available as environment variables during that RUN.
# syntax=docker/dockerfile:1
FROM alpine:3.20
ENV APP=demo
RUN <<EOF
set -e
echo "App is $APP"
echo "Shell PID is $$"
EOFHere $APP and $$ are both resolved by the shell while the build step runs. If you want a literal $ to reach a program such as awk, use normal shell quoting inside the body, for example awk '{print $1}'.
Removing leading tabs with <<-
Heredoc bodies keep their indentation, which makes them awkward inside indented scripts. The <<- form strips leading tab characters from every body line and from the closing delimiter line. The body below is indented with tabs:
# syntax=docker/dockerfile:1
FROM alpine:3.20
COPY <<-EOF /etc/motd
Welcome
Built with heredocs
EOFThe file /etc/motd contains two lines with no leading whitespace. You can combine the forms: <<-"EOF" strips tabs and also disables expansion.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A script in a `COPY` heredoc has empty values where `$VAR` should be | Unquoted delimiter let the frontend expand the variable | Quote the delimiter: `<<"EOF"` |
| A `$ARG` in a `COPY` heredoc stays literal when you wanted its value | The delimiter is quoted | Remove the quotes |
| Build reports a missing delimiter | Closing line has spaces, or `<<-` used with space indentation | Put the delimiter alone on its line, or indent it with tabs under `<<-` |
| Extra leading spaces in the written file | Body indented with spaces | Use tabs with `<<-`, or remove the indentation |
Exercise
You write COPY <<EOF /app/run.sh with a body containing echo "User: $USER", and no ENV USER or ARG USER exists in the Dockerfile. The script prints User: with nothing after it. What two changes could you make, and how does each change the result?
Show answer
Quote the delimiter (<<"EOF"), so the frontend leaves $USER untouched and the shell expands it when the script runs. Alternatively, define ARG USER=name or ENV USER=name in the Dockerfile and keep the delimiter unquoted, so the frontend writes the fixed value name into the file at build time.