Sub topic 5
Creating Files Inline with COPY Heredocs
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 use BuildKit, which is the default builder in current Docker releases.
Why write files inside the Dockerfile
Small config files and helper scripts are often kept as separate files next to the Dockerfile, just to be copied in. That adds clutter and splits one build step across two places. A COPY heredoc lets you put the file's content directly in the Dockerfile and write it into the image.
A heredoc is a block of text that starts after a delimiter word (here EOF) and ends at a line holding only that word. The text between is the file content.
The basic form
The COPY instruction takes the heredoc as its source and a destination path as its target.
# syntax=docker/dockerfile:1
FROM alpine:3.20
COPY --chmod=755 <<"EOF" /usr/local/bin/hello.sh
#!/bin/sh
echo "Home is $HOME"
EOF
CMD ["/usr/local/bin/hello.sh"]Here is how the pieces work:
- The
syntaxdirective on the first line selects the stable Dockerfile frontend. Heredoc support depends on a recent frontend. <<"EOF"opens the heredoc. Everything up to the closingEOFline becomes the file./usr/local/bin/hello.shis the destination inside the image.--chmod=755makes the file executable. Without it, the file is created with default permissions andCMDfails with a permission error.
The ADD instruction accepts heredocs in the same way.
Quoting controls variable expansion
The quotes around the delimiter decide who expands variables such as $HOME.
| Form | Expansion at build time | Result in the file |
|---|---|---|
| `<<"EOF"` | None | `$HOME` is written literally and expanded when the script runs |
| `<<EOF` | Dockerfile expands build arguments and environment variables | `$HOME` is replaced if the Dockerfile knows a value for it |
In the example above, the quoted delimiter keeps $HOME intact. The script prints the home directory of whoever runs it, which is what you want. If you leave the quotes off, the Dockerfile may substitute a value, or an empty string, when it builds the image. The script then contains a fixed value instead of the variable.
How the build processes a COPY heredoc
Writing configuration files
Heredocs work for any text file, not only scripts. This example creates a small configuration file and then uses it.
# syntax=docker/dockerfile:1
FROM nginx:1.27-alpine
COPY <<"EOF" /etc/nginx/conf.d/default.conf
server {
listen 8080;
location / {
return 200 "ok\n";
}
}
EOFThe quoted delimiter matters here too. Nginx variables also start with $, and you do not want the Dockerfile to touch them.
Common errors
- Missing executable bit. Add
--chmod=755for scripts. - Unexpected empty values. An unquoted delimiter let the Dockerfile expand a variable that was not defined. Quote the delimiter.
- Delimiter not found. The closing word must be alone on its line, with no trailing spaces. Otherwise the parser keeps reading and the build fails.
- Old frontend. If the heredoc is rejected, check that the first line has
# syntax=docker/dockerfile:1. - Missing shebang. A script without a
#!/bin/shfirst line may not run correctly when called directly.
Exercise
A Dockerfile copies a script with COPY <<EOF /app/run.sh and the script contains echo "User: $USER". After the build, the script prints a fixed or empty value instead of the current user. What change fixes this, and what else must you add so the script can run directly?
Show answer
Quote the delimiter as <<"EOF" so the Dockerfile does not expand $USER at build time, which leaves it for the shell to evaluate at run time. Also add --chmod=755 to the COPY instruction so the file is executable.