简体   繁体   中英

Why we use go mod download before build go application?

When people make docker file of go application with module, most of them make like this.

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build

But when I tried the commands below, it also works well.

COPY . .
RUN go build

So, what's the difference? Is there any advantages using GO MOD DOWNLOAD command?

Go 1.11 introduced the go mod download command, which takes go.mod and go.sum files and downloads the dependencies from them instead of using the source code. As these files don't change frequently (unless you are updating the dependencies), they can be simply cached by the COPY command from Dockerfile .

This is usually done to better use the cache. The dependencies do not change so often as the program itself. Doing the download in a separate step allows this layer to be cached. Repeated builds will then be much faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM