Part 4: Caching dependencies
The reason the build is inefficient is because we have not made proper use of caching. When a file changes, the corresponding COPY command is re-executed without cache, causing all commands after it to also re-execute without cache.
If, however, we could first download the dependencies and only afterwards copy and build the code, then the cache would be reused every time we changed the code.
Go
JavaScript
Java
Python
./Earthfile
1
FROM golang:1.15-alpine3.13
2
WORKDIR /go-example
3
​
4
build:
5
# Download deps before copying code.
6
COPY go.mod go.sum .
7
RUN go mod download
8
# Copy and build code.
9
COPY main.go .
10
RUN go build -o build/go-example main.go
11
SAVE ARTIFACT build/go-example /go-example AS LOCAL build/go-example
12
​
13
docker:
14
COPY +build/go-example .
15
ENTRYPOINT ["/go-example/go-example"]
16
SAVE IMAGE go-example:latest
Copied!

Note

To copy the files for this example ( Part 4 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part4/part4 ./part4
Copied!
./Earthfile
1
FROM node:13.10.1-alpine3.11
2
WORKDIR /js-example
3
​
4
build:
5
# Download deps before copying code.
6
COPY package.json package-lock.json ./
7
RUN npm install
8
# Copy and build code.
9
COPY src src
10
RUN mkdir -p ./dist && cp ./src/index.html ./dist/
11
RUN npx webpack
12
SAVE ARTIFACT dist /dist AS LOCAL ./dist
13
​
14
docker:
15
COPY package.json package-lock.json ./
16
RUN npm install
17
COPY +build/dist dist
18
EXPOSE 8080
19
ENTRYPOINT ["/js-example/node_modules/http-server/bin/http-server", "./dist"]
20
SAVE IMAGE js-example:latest
Copied!

Note

To copy the files for this example ( Part 4 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/js:main+part4/part4 ./part4
Copied!
./Earthfile
1
FROM openjdk:8-jdk-alpine
2
RUN apk add --update --no-cache gradle
3
WORKDIR /java-example
4
​
5
build:
6
# Download deps before copying code.
7
COPY build.gradle ./
8
RUN gradle build
9
# Copy and build code.
10
COPY src src
11
RUN gradle build
12
RUN gradle install
13
SAVE ARTIFACT build/install/java-example/bin /bin AS LOCAL build/bin
14
SAVE ARTIFACT build/install/java-example/lib /lib AS LOCAL build/lib
15
​
16
docker:
17
COPY +build/bin bin
18
COPY +build/lib lib
19
ENTRYPOINT ["/java-example/bin/java-example"]
20
SAVE IMAGE java-example:latest
Copied!

Note

To copy the files for this example ( Part 4 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/java:main+part4/part4 ./part4
Copied!
./Earthfile
1
FROM python:3
2
WORKDIR /code
3
​
4
build:
5
# Download deps before copying code.
6
RUN pip install wheel
7
COPY requirements.txt ./
8
RUN pip wheel -r requirements.txt --wheel-dir=wheels
9
SAVE ARTIFACT wheels /wheels
10
# Copy and build code.
11
COPY src src
12
SAVE ARTIFACT src /src
13
​
14
docker:
15
COPY +build/wheels wheels
16
COPY +build/src src
17
COPY requirements.txt ./
18
RUN pip install --no-index --find-links=wheels -r requirements.txt
19
ENTRYPOINT ["python3", "./src/hello.py"]
20
SAVE IMAGE python-example:latest
Copied!

Note

To copy the files for this example ( Part 4 ) run
1
earthly --artifact github.com/earthly/earthly/examples/tutorial/python:main+part4/part4 ./part4
Copied!
For a primer into Dockerfile layer caching see this article. The same principles apply to Earthfiles.
Last modified 24d ago
Copy link