Sub topic 11
Running Python and Other Interpreters from Heredocs
This chapter assumes you can write a basic Dockerfile, know POSIX here-documents, and have read the earlier chapters on the syntax directive and RUN heredocs. Here you will pass a heredoc to an interpreter other than the shell, and combine several heredocs in one instruction.
How the interpreter is chosen
A RUN heredoc is not always run by /bin/sh. The text after RUN is the command, and the heredoc body becomes that command's standard input. If the command is python3, Python reads and runs your script.
# syntax=docker/dockerfile:1
FROM python:3.12-alpine
RUN python3 <<EOF
import sys
print(sys.version)
EOFThe syntax directive on the first line selects the stable Dockerfile frontend, which provides heredoc support. The build prints the Python version, and no script file is added to the image.
You can also choose the interpreter with a shebang line as the first line of the body. This works when the heredoc is the whole RUN command:
# syntax=docker/dockerfile:1
FROM python:3.12-alpine
RUN <<EOF
#!/usr/bin/env python3
print("run by the shebang interpreter")
EOFPrefer the explicit RUN python3 <<EOF form. It is easier to read and does not depend on shebang handling.
What happens at build time
Whatever the script changes on disk is saved in a single layer, the same as with a chained shell command.
Multiple heredocs in one instruction
One RUN can take more than one heredoc. Each has its own delimiter, and the command decides how to use them. A common pattern is to run a shell command that reads two scripts:
# syntax=docker/dockerfile:1
FROM python:3.12-alpine
RUN python3 <<PY1 && python3 <<PY2
print("first script")
PY1
print("second script")
PY2This runs the two scripts in order, in one layer. Use distinct delimiter names so the parser can tell where each body ends.
Gotchas
- Variable expansion applies to the body. With an unquoted delimiter, the Dockerfile expands
$VARand${VAR}before the interpreter sees the text. Python code that contains$can be changed unexpectedly. Quote the delimiter (<<"EOF") when the script is not meant to use Dockerfile variables. - The interpreter must exist in the image.
RUN python3 <<EOFon a plainalpinebase fails withpython3: not found. Install the interpreter first or choose a base image that includes it. - Errors need checking. A shell body does not stop on the first failed command unless you add
set -e. A Python script stops on an uncaught exception and exits non-zero, which fails the build. - Indentation matters. Python is sensitive to leading whitespace. Do not indent the body to match the Dockerfile. The
<<-form strips leading tabs only, not spaces, so it does not help with space-indented code.
When to use this
Use an interpreter heredoc for short build-time tasks such as generating a config file, checking a version, or transforming a downloaded file. For longer logic, a script copied in with COPY is easier to test, lint, and reuse outside the build.
| Approach | Good for | Drawback |
|---|---|---|
| `RUN python3 <<EOF` | Short one-off build steps | Hard to test outside the build |
| `COPY` a script, then `RUN` it | Longer or reused logic | Extra file to maintain |
| Chained `&&` shell commands | Simple command sequences | Hard to read when long |
Exercise
A Dockerfile uses FROM alpine:3.20 followed by RUN python3 <<EOF and a short script. The build fails with python3: not found. Give two ways to fix it.
Show answer
Alpine does not include Python. Either use a base image that does, such as python:3.12-alpine, or add an earlier step such as RUN apk add --no-cache python3 before the heredoc RUN.
Exercise
A heredoc script passed to python3 contains the text price = "$5", and the build output shows a different value. Why, and how do you stop it?
Show answer
The delimiter is unquoted, so the Dockerfile expanded $5 before Python ran. Quote the delimiter, as in RUN python3 <<"EOF", to pass the body unchanged.