Earthfiles are always named Earthfile
, regardless of their location in the codebase.
Here is a sample earthfile of a Go app
# EarthfileFROM golang:1.15-alpine3.13WORKDIR /go-examplebuild:COPY main.go .RUN go build -o build/go-example main.goSAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-exampledocker:COPY +build/go-example .ENTRYPOINT ["/go-example/go-example"]SAVE IMAGE go-example:latest
The code of the app might look like this
// main.gopackage mainimport "fmt"func main() {fmt.Println("hello world")}
Here is a sample earthfile of a JS app
# EarthfileFROM node:13.10.1-alpine3.11WORKDIR /js-examplebuild:# In JS, there's nothing to build in this simple form.# The source is also the artifact used in production.COPY index.js .SAVE ARTIFACT index.js /dist/index.js AS LOCAL ./dist/index.jsdocker:COPY +build/dist distENTRYPOINT ["node", "./dist/index.js"]SAVE IMAGE js-example:latest
The code of the app might look like this
// index.jsconsole.log("hello world");
Here is a sample earthfile of a Java app
# EarthfileFROM openjdk:8-jdk-alpineRUN apk add --update --no-cache gradleWORKDIR /java-examplebuild:COPY build.gradle ./COPY src srcRUN gradle buildRUN gradle installSAVE ARTIFACT build/install/java-example/bin /bin AS LOCAL build/binSAVE ARTIFACT build/install/java-example/lib /lib AS LOCAL build/libdocker:COPY +build/bin binCOPY +build/lib libENTRYPOINT ["/java-example/bin/java-example"]SAVE IMAGE java-example:latest
The code of the app might look like this
// src/main/java/hello/HelloWorld.javapackage hello;public class HelloWorld {public static void main(String[] args) {System.out.println("hello world");}}
Here is a sample earthfile of a Python app
# EarthfileFROM python:3WORKDIR /codebuild:# In Python, there's nothing to build.COPY src srcSAVE ARTIFACT src /srcdocker:COPY +build/src srcENTRYPOINT ["python3", "./src/hello.py"]SAVE IMAGE python-example:latest
The code of the app might look like this
// src/hello.pyprint("hello world")
You will notice that the recipes look very much like Dockerfiles. This is an intentional design decision. Existing Dockerfiles can be ported to earthfiles by copy-pasting them over and then tweaking them slightly. Compared to Dockerfile syntax, some commands are new (like SAVE ARTIFACT
), others have additional semantics (like COPY +target/some-artifact
) and other semantics have been removed (like FROM ... AS ...
and COPY --from
).
You might notice the command COPY +build/... ...
, which has an unfamiliar form. This is a special type of COPY
, which can be used to pass artifacts from one target to another. In this case, the target build
(referenced as +build
) produces an artifact, which has been declared with SAVE ARTIFACT
, and the target docker
copies that artifact in its build environment.
With Earthly you have the ability to pass such artifacts or images between targets within the same Earthfile, but also across different Earthfiles across directories or even across repositories. To read more about this, see the target, artifact and image referencing guide.