Read more

Dockerfile and Heredocs

Michael Leimstädtner
January 08, 2024Software engineer at makandra GmbH

I recently did a quick research on how to better write down multiline statements like this:

# Dockerfile
RUN export DEBIAN_FRONTEND=noninteractive \
  && apt-get update \
  && apt-get dist-upgrade -y \
  && apt-get install --no-install-recommends --no-install-suggests -y \
    ca-certificates \
    curl \
    unzip
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

It turns out, there is! Buildkit, the tool behind docker build added support for heredoc strings in May 2021 Show archive.org snapshot .
Now we could refactor the code above like this:

# Dockerfile
RUN <<-EOF
  export DEBIAN_FRONTEND=noninteractive
  apt-get update
  apt-get dist-upgrade -y
  apt-get install --no-install-recommends --no-install-suggests -y \
    ca-certificates \
    curl \
    unzip
EOF

Nested multiline commands still require trailing backslashes to work properly.

Note

I needed to install the Buildkit plugin buildx Show archive.org snapshot to get the desired result. If you add heredocs to your Dockerfile, you might break compatiblity with older clients!

Current Verdict

Not recommended for internal use. It's not very clear if each line statement is chained with && or executed no matter if the previous statement fails or not.

Posted by Michael Leimstädtner to makandra dev (2024-01-08 17:28)