earthly
earthly copied to clipboard
Proposal: Heredoc and inline files
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.
This proposal needs to be updated to match the new Dockerfile heredoc feature (the COPY syntax is a bit different).
@vladaionescu Since Dockerfile added this feature, this issue should be tagged as dockerfile-parity as well
for reference: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-running-a-multi-line-script
This feature would still be really nice to have!
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!