From a653181ea8bb90a1584f7e279c71d95801a78f1b Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Mon, 22 May 2023 23:54:02 -0300 Subject: [PATCH] Improve Docker layer caching for Go dependencies (#560) Running `go mod download` before copying the entire project to the Docker image avoids dependencies from being re-downloaded every time any file is modified, and `docker build` runs again. This follows the steps detailed in the official Docker guide for Go images: https://docs.docker.com/language/golang/build-images/#create-a-dockerfile-for-the-application Also, `GO111MODULE` doesn't make any difference for the supported Go versions, so it can be removed from the Dockerfile. --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1ac34cd..3f4d9cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,11 +5,13 @@ FROM golang:${GO_VERSION}-alpine as build # Necessary to run 'go get' and to compile the linked binary RUN apk add git musl-dev mailcap -ADD . /go/src/github.com/dutchcoders/transfer.sh - WORKDIR /go/src/github.com/dutchcoders/transfer.sh -ENV GO111MODULE=on +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . # build & install server RUN CGO_ENABLED=0 go build -tags netgo -ldflags "-X github.com/dutchcoders/transfer.sh/cmd.Version=$(git describe --tags) -a -s -w -extldflags '-static'" -o /go/bin/transfersh