How can I cache packages build artefacts for docker build?

Viewed 147

A typical go docker pattern is this:

# cache modules
COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .
RUN make

This will create a (cached) layer for downloaded packages before compiling the actual sources. It would be great to not only download but also compile the packages before adding the application to further speed up repeated builds.

How would one force-compile all downloaded packages irrespective of the parent application?

1 Answers

The goal of using go mod download in docker build, is to avoid waiting for the dependencies' sources to be compiled. Since you want to avoid that beahviour, you can replace that line with

RUN go get -d -v
Related