Field Note

Writing multiline `RUN` commands in Dockerfile using heredoc syntax

python docker run multi-line
Posted on Thursday, April the 11th 2024
1 min read

In essence, this well known pattern (joining command invocations together over multiple lines with && \)

FROM ubuntu:22.04

RUN apt-get update && \
  apt-get upgrade -y && \
  apt-get install -y curl

may be replaced with this

FROM ubuntu:22.04

RUN <<EOF
set -e
apt-get update
apt-get upgrade -y
apt-get install -y curl
EOF

The heredoc syntax is enabled by default when building with BuildKit (standard for DockerDesktop as well as DockerEngine since v23.0). It doesn’t apply to older versions or when not building with BuildKit.

This feature is not supported in Kaniko.

friedrichkurz.me

© 2025 Friedrich Kurz

Privacy Policy