Part 1: A simple Earthfile
Earthfiles are always named Earthfile, regardless of their location in the codebase.
Here is a sample Earthfile of a Go app
./Earthfile
1
VERSION 0.6
2
FROM golang:1.15-alpine3.13
3
WORKDIR /go-example
4
​
5
build:
6
COPY main.go .
7
RUN go build -o build/go-example main.go
8
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
9
​
10
docker:
11
COPY +build/go-example .
12
ENTRYPOINT ["/go-example/go-example"]
13
SAVE IMAGE go-example:latest
Copied!
The code of the app might look like this
./main.go
1
package main
2
​
3
import "fmt"
4
​
5
func main() {
6
fmt.Println("hello world")
7
}
Copied!
Note
To copy the files for this example ( Part 1 ) run
1
mkdir tutorial
2
cd tutorial
3
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part1/part1 ./part1
Copied!
Here is a sample Earthfile of a JS app
./Earthfile
1
VERSION 0.6
2
FROM node:13.10.1-alpine3.11
3
WORKDIR /js-example
4
​
5
build:
6
# In JS, there's nothing to build in this simple form.
7
# The source is also the artifact used in production.
8
COPY src/index.js .
9
SAVE ARTIFACT index.js /dist/index.js AS LOCAL ./dist/index.js
10
​
11
docker:
12
COPY +build/dist dist
13
ENTRYPOINT ["node", "./dist/index.js"]
14
SAVE IMAGE js-example:latest
Copied!
The code of the app might look like this
./src/index.js
1
console.log("hello world");
Copied!
Note
To copy the files for this example ( Part 1 ) run
1
mkdir tutorial
2
cd tutorial
3
earthly --artifact github.com/earthly/earthly/examples/tutorial/js:main+part1/part1 ./part1
Copied!
Here is a sample Earthfile of a Java app
./Earthfile
1
VERSION 0.6
2
FROM openjdk:8-jdk-alpine
3
RUN apk add --update --no-cache gradle
4
WORKDIR /java-example
5
​
6
build:
7
COPY build.gradle ./
8
COPY src src
9
RUN gradle build
10
RUN gradle install
11
SAVE ARTIFACT build/install/java-example/bin /bin AS LOCAL build/bin
12
SAVE ARTIFACT build/install/java-example/lib /lib AS LOCAL build/lib
13
​
14
docker:
15
COPY +build/bin bin
16
COPY +build/lib lib
17
ENTRYPOINT ["/java-example/bin/java-example"]
18
SAVE IMAGE java-example:latest
Copied!
The code of the app might look like this
./src/main/java/hello/HelloWorld.java
1
package hello;
2
​
3
public class HelloWorld {
4
public static void main(String[] args) {
5
System.out.println("hello world");
6
}
7
}
Copied!
./build.gradle
1
apply plugin: 'java'
2
apply plugin: 'application'
3
​
4
mainClassName = 'hello.HelloWorld'
5
​
6
jar {
7
baseName = 'hello-world'
8
version = '0.0.1'
9
}
10
​
11
sourceCompatibility = 1.8
12
targetCompatibility = 1.8
Copied!
Note
To copy the files for this example ( Part 1 ) run
1
mkdir tutorial
2
cd tutorial
3
earthly --artifact github.com/earthly/earthly/examples/tutorial/java:main+part1/part1 ./part1
Copied!
Here is a sample Earthfile of a Python app
./Earthfile
1
VERSION 0.6
2
FROM python:3
3
WORKDIR /code
4
​
5
build:
6
# In Python, there's nothing to build.
7
COPY src src
8
SAVE ARTIFACT src /src
9
​
10
docker:
11
COPY +build/src src
12
ENTRYPOINT ["python3", "./src/hello.py"]
13
SAVE IMAGE python-example:latest
Copied!
The code of the app might look like this
./src/hello.py
1
print("hello world")
Copied!
Note
To copy the files for this example ( Part 1 ) run
1
mkdir tutorial
2
cd tutorial
3
earthly --artifact github.com/earthly/earthly/examples/tutorial/python:main+part1/part1 ./part1
Copied!
From the example above, you may notice that an Earthfile is very similar to a Dockerfile. This is an intentional design decision. Existing Dockerfiles can easily be ported to Earthly by copying them to an Earthfile and tweaking them slightly.
You may 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.
Last modified 2d ago
Copy link