earthly icon indicating copy to clipboard operation
earthly copied to clipboard

Proposal: Heredoc and inline files

Open vladaionescu opened this issue 4 years ago • 7 comments

Allow long commands to be specified as heredoc.

Similarly, allow files to be specified inline via heredoc.

Example heredoc command

some-target:
  RUN <<EOF
    echo "Downloading something"
    curl -o ./file -L https://...
    chmod +x ./file
  EOF

Example heredoc file

some-target:
  COPY - ./docker-compose.yml <<EOF
  version: "3"
  
  services:
    postgres:
      image: aa8y/postgres-dataset:iso3166
      ports:
        - 127.0.0.1:5432:5432
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
  EOF

Notes

  • The text needs to be indented if it's part of a non-base target. Otherwise the text is interpreted as raw - eg cannot use comments, variables are not expanded.

vladaionescu avatar Dec 05 '20 00:12 vladaionescu

This proposal needs to be updated to match the new Dockerfile heredoc feature (the COPY syntax is a bit different).

vladaionescu avatar Oct 08 '21 21:10 vladaionescu

@vladaionescu Since Dockerfile added this feature, this issue should be tagged as dockerfile-parity as well

mxey avatar Oct 11 '21 07:10 mxey

for reference: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-running-a-multi-line-script

alexcb avatar Nov 02 '22 22:11 alexcb

This feature would still be really nice to have!

Qqwy avatar Dec 19 '22 11:12 Qqwy

As another posted pointed out, the origional proposal is different to the current dockerfile 1.4 support for this.

Here's a couple of examples that are now possible in dockerfiles:

RUN <<EOF
#!/usr/bin/env python3
with open("/hello", "w") as f:
    print("Hello", file=f)
    print("World", file=f)
EOF
COPY <<EOF /usr/share/nginx/html/index.html
(your index page goes here)
EOF

As a bonus feature here, if the heredoc support allows variables to be used (I'm unsure if the dockerfile implementation allows this) then you can have a simple but effective templating mechanism so you can do something like this which means you can throw away templating tools like envsubst

COPY <<EOF /usr/share/nginx/html/index.html
<title>My name is $NAME</title>
EOF

Another more practical example for yum:

COPY <<EOF /etc/yum.repos.d/Repo.repoxyz
[repo]
name            = YUM Repository
baseurl         = $BASE_URL
enabled         = 1
gpgcheck        = 0
EOF

This feature becomes very powerful in UDCs, because now it's possible to embed templates for config files, or embed shell scripts within the UDC which can be dynamically populated with args without having to make them available in the callers file context. It's a pretty killer addon!

hongkongkiwi avatar Sep 28 '23 08:09 hongkongkiwi