Skip to content

Commit 0e089ff

Browse files
committed
feat: add deps stage to go dockerfile to improve caching
1 parent 0ffda7b commit 0e089ff

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

runtime/golang.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,28 @@ func (d *Golang) GenerateDockerfile(path string) ([]byte, error) {
9696

9797
var golangTemplate = strings.TrimSpace(`
9898
ARG VERSION={{.Version}}
99-
ARG BUILDPLATFORM=linux
99+
ARG BUILDPLATFORM=linux/amd64
100100
FROM --platform=${BUILDPLATFORM} golang:${VERSION} AS base
101+
102+
FROM base AS deps
101103
WORKDIR /go/src/app
102-
ARG TARGETOS=linux
103-
ARG TARGETARCH=arm64
104-
ARG CGO_ENABLED=0
104+
COPY go.mod* go.sum* ./
105+
# GOPROXY is used to specify the module proxy to use.
105106
ARG GOPROXY=direct
106-
107-
COPY . .
108107
ENV GOPROXY=${GOPROXY}
109-
RUN if [ -f go.mod ]; then go mod download; fi
108+
RUN if [ -f go.mod ]; then go mod download && go mod tidy; fi
110109
111-
FROM base AS build
110+
FROM deps AS build
112111
WORKDIR /go/src/app
112+
113+
COPY . .
114+
115+
ARG PACKAGE={{.Package}}
113116
ARG TARGETOS=linux
114-
ARG TARGETARCH=arm64
117+
ARG TARGETARCH=amd64
115118
ARG CGO_ENABLED=0
116-
ARG PACKAGE={{.Package}}
117-
ARG GOPROXY=direct
118119
# -trimpath removes the absolute path to the source code in the binary
119120
# -ldflags="-s -w" removes the symbol table and debug information from the binary
120-
# CGO_ENABLED=0 disables the use of cgo
121-
ENV GOPROXY=${GOPROXY}
122121
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath -ldflags="-s -w" -o /go/bin/app "${PACKAGE}"
123122
124123
FROM debian:stable-slim
@@ -130,6 +129,7 @@ RUN chown -R nonroot:nonroot /app
130129
COPY --chown=nonroot:nonroot --from=build /go/bin/app .
131130
132131
ENV PORT=8080
132+
EXPOSE ${PORT}
133133
USER nonroot:nonroot
134134
CMD ["/app/app"]
135135
`)

0 commit comments

Comments
 (0)