Search code examples
dockerdockerfile

How to improve Docker build output when using `RUN` with a HEREDOC?


Given the Dockerfile:

FROM <some-image>
RUN <<`
  set -e
  # do multiple lines of commands
`

This is output during docker buildx build as:

=> [2/2] RUN <<` (set -e...)

It is not clear which RUN command this was, since any using this HEREDOC format will look the same. I'd like to be able to use this HEREDOC format, while also having the build output be more clear, e.g.:

RUN <<` echo Installing deps...
  set -e
  # do multiple lines of commands
`

Which gives the output:

=> [2/2] RUN <<` echo Installing deps...

So I can clearly see which RUN HEREDOC is being executed. But this does not work, and neither does:

RUN <<` # Installing deps...
  set -e
  # do multiple lines of commands
`

Because the multiline HEREDOC is strung together when it's executed, into a single line.


To solve this, I could just simply ECHO before my RUN directives which use HEREDOCs:

RUN echo Installing deps...
RUN <<`
  set -e
  # do multiple lines of commands
`

But is it the proper way, since it adds a useless RUN directive?


Solution

  • It looks like you already have a solution. If you want to see something other than set -e in your docker build output, then ensure the first line of each heredoc is something other than set -e. E.g, we can write:

    FROM docker.io/alpine:latest
    
    RUN <<EOF
      # update package database
      set -e
      apk update
    EOF
    
    RUN <<EOF
      # install packages
      set -e
      apk add bash curl
    EOF
    

    Running docker build with the above Dockerfile produces:

     => [internal] load build definition from Dockerfile
     => => transferring dockerfile: 256B
     => [internal] load metadata for docker.io/library/alpine:latest
     => [internal] load .dockerignore
     => => transferring context: 2B
     => CACHED [1/3] FROM docker.io/library/alpine:latest
     => [2/3] RUN <<EOF (# update package database...)
     => [3/3] RUN <<EOF (# install packages...)
     => exporting to image
     => => exporting layers
     => => writing image sha256:227569447477d6002b6c58af1c6f2336332fca59d6779101f926449d5086c6d6
    

    That seems to clearly indicate which heredoc is associated with each RUN statement.