Searchโ€ฆ
Part 1b: Detailed explanation
Going back to the example Earthfile definition, here is what each command does:
Go
JavaScript
Java
Python
The first commands in the file are part of the base target and are implicitly inherited by all other targets (as if they started with FROM +base).
The build starts from a docker image: golang:1.15-alpine3.13.
1
FROM golang:1.15-alpine3.13
Copied!
We change the current working directory to /go-example.
1
WORKDIR /go-example
Copied!
Declare a target, build. (This target may be invoked from the command line as earthly +build.)
1
build:
Copied!
... and define the recipe of the target build as follows.
Copy main.go from the build context (the directory where the Earthfile resides) to the build environment (the containerized environment where Earthly commands are ran in).
1
COPY main.go .
Copied!
Run a go build command. This uses the previously copied main.go file.
1
RUN go build -o build/go-example main.go
Copied!
Save the output of the build command as an artifact. Call this artifact /go-example (it can be later referenced as +build/go-example). In addition, store the artifact as a local file (on the host) named build/go-example. This local file is only written if the entire build succeeds.
1
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
Copied!
Declare a target, docker.
1
docker:
Copied!
... and define the recipe of the target docker as follows.
Copy the artifact /go-example produced by another target, +build, to the current directory within the build environment.
1
COPY +build/go-example .
Copied!
Set the entrypoint for the resulting docker image.
1
ENTRYPOINT ["/go-example/go-example"]
Copied!
Save the current state as a docker image, which will have the docker tag go-example:latest. This image is only made available to the host's docker if the entire build succeeds.
1
SAVE IMAGE go-example:latest
Copied!
The first commands in the file are part of the base target and are implicitly inherited by all other targets (as if they started with FROM +base).
The build starts from a docker image: node:13.10.1-alpine3.11.
1
FROM node:13.10.1-alpine3.11
Copied!
We change the current working directory to /js-example.
1
WORKDIR /js-example
Copied!
Declare a target, build. (This target may be invoked from the command line as earthly +build.)
1
build:
Copied!
... and define the recipe of the target build as follows.
Copy index.js from the build context (the directory where the Earthfile resides) to the build environment (the containerized environment where Earthly commands are ran in).
1
COPY src/index.js .
Copied!
Save the index.js in an artifact directory called dist (it can be later referenced as +build/dist). In addition, store the artifact as a local file (on the host) named dist/index.js. This local file is only written if the entire build succeeds.
1
SAVE ARTIFACT index.js /dist/index.js AS LOCAL ./dist/index.js
Copied!
Declare a target, docker.
1
docker:
Copied!
... and define the recipe of the target docker as follows.
Copy the artifact /dist produced by another target, +build, to the current directory within the build container.
1
COPY +build/dist dist
Copied!
Set the entrypoint for the resulting docker image.
1
ENTRYPOINT ["node", "./dist/index.js"]
Copied!
Save the current state as a docker image, which will have the docker tag js-example:latest. This image is only made available to the host's docker if the entire build succeeds.
1
SAVE IMAGE js-example:latest
Copied!
The first commands in the file are part of the base target and are implicitly inherited by all other targets (as if they started with FROM +base).
The build starts from a docker image: openjdk:8-jdk-alpine.
1
FROM openjdk:8-jdk-alpine
Copied!
We install gradle using alpine's apk command.
1
RUN apk add --update --no-cache gradle
Copied!
We change the current working directory to /java-example.
1
WORKDIR /java-example
Copied!
Declare a target, build. (This target may be invoked from the command line as earthly +build.)
1
build:
Copied!
... and define the recipe of the target build as follows.
Copy build.gradle and src from the build context (the directory where the Earthfile resides) to the build environment (the containerized environment where Earthly commands are ran in).
1
COPY build.gradle ./
2
COPY src src
Copied!
Run the gradle build and gradle install commands. These use the previously copied src dir.
1
RUN gradle build
2
RUN gradle install
Copied!
Save the output of the build command as artifacts. Call these artifacts bin and lib (they can be later referenced as +build/bin and +build/lib respectively). In addition, store the artifacts as local directories (on the host) named build/bin and build/lib. These local directories are only written if the entire build succeeds.
1
SAVE ARTIFACT build/install/java-example/bin /bin AS LOCAL build/bin
2
SAVE ARTIFACT build/install/java-example/lib /lib AS LOCAL build/lib
Copied!
Declare a target, docker.
1
docker:
Copied!
... and define the recipe of the target docker as follows.
Copy the artifacts /bin and /lib produced by another target, +build, to the current directory within the build container.
1
COPY +build/bin bin
2
COPY +build/lib lib
Copied!
Set the entrypoint for the resulting docker image.
1
ENTRYPOINT ["/java-example/bin/java-example"]
Copied!
Save the current state as a docker image, which will have the docker tag java-example:latest. This image is only made available to the host's docker if the entire build succeeds.
1
SAVE IMAGE java-example:latest
Copied!
The first commands in the file are part of the base target and are implicitly inherited by all other targets (as if they started with FROM +base).
The build starts from the python:3 docker image.
1
FROM python:3
Copied!
We change the current working directory to /code.
1
WORKDIR /code
Copied!
Declare a target, build. (This target may be invoked from the command line as earthly +build.)
1
build:
Copied!
... and define the recipe of the target build as follows.
Copy the src directory from the build context (the directory where the Earthfile resides) to the build environment (the containerized environment where Earthly commands are ran in).
1
COPY src src
Copied!
Save the python source in an artifact directory called src (it can be later referenced as +build/src).
1
SAVE ARTIFACT src /src
Copied!
Declare a target, docker.
1
docker:
Copied!
... and define the recipe of the target docker as follows.
Copy the artifact /src from the target +build into the current directory within the build container.
1
COPY +build/src src
Copied!
Set the entrypoint for the resulting docker image.
1
ENTRYPOINT ["python3", "./src/hello.py"]
Copied!
Save the current state as a docker image, which will have the docker tag python-example:latest. This image is only made available to the host's docker if the entire build succeeds.
1
SAVE IMAGE python-example:latest
Copied!

Note

Targets have a particular referencing convention which helps Earthly to identify which recipe to execute. In the simplest form, targets are referenced by +<target-name>. For example, +build. For more details see the target referencing page.
Target and artifact reference syntax
Last modified 18d ago
Copy link