Skip to content
Latchkey

Docker Heredoc "unexpected end of statement while looking for matching" in CI

BuildKit parsed a heredoc RUN <<EOF block and reached the end of the file without finding its closing EOF. The block is unterminated, or heredocs are not enabled for this Dockerfile.

What this error means

The build fails at parse time with unexpected end of statement while looking for matching heredoc "EOF". No steps run because the Dockerfile never finished parsing the heredoc body.

docker build output
ERROR: failed to solve: dockerfile parse error: unexpected end of statement
while looking for matching heredoc "EOF"
# the RUN <<EOF block was never closed by a line containing only EOF

Common causes

Missing or misindented terminator

The closing EOF must be on its own line with no leading whitespace (unless using <<-EOF). A trailing space, indentation, or a typo means the parser never sees the terminator.

Heredoc syntax without the syntax directive

Heredocs need a recent Dockerfile frontend. Without # syntax=docker/dockerfile:1 at the top (on older BuildKit), the <<EOF is not understood as a heredoc.

Mismatched delimiter name

Opening with <<EOF but closing with a different word (END, eof) leaves the heredoc open until end of file.

How to fix it

Close the heredoc on its own line

Enable the syntax frontend and terminate the block with an unindented delimiter.

Dockerfile
# syntax=docker/dockerfile:1
RUN <<EOF
set -e
apt-get update
apt-get install -y curl
EOF

Use <<-EOF if you must indent the terminator

The dash form strips leading tabs so an indented EOF still closes the block.

Dockerfile
RUN <<-EOF
	set -e
	echo hi
	EOF

How to prevent it

  • Add # syntax=docker/dockerfile:1 when using heredocs.
  • Keep the closing delimiter on its own line with no leading spaces.
  • Match the opening and closing delimiter words exactly.

Frequently asked questions

What causes ""unexpected end of statement""?
The closing EOF must be on its own line with no leading whitespace (unless using <<-EOF). A trailing space, indentation, or a typo means the parser never sees the terminator.
How do I fix "unexpected end of statement"?
Enable the syntax frontend and terminate the block with an unindented delimiter.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card