Skip to content

Commit

Permalink
feat: add deps stage to go dockerfile to improve caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed May 22, 2024
1 parent 0ffda7b commit 0e089ff
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions runtime/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,28 @@ func (d *Golang) GenerateDockerfile(path string) ([]byte, error) {

var golangTemplate = strings.TrimSpace(`
ARG VERSION={{.Version}}
ARG BUILDPLATFORM=linux
ARG BUILDPLATFORM=linux/amd64
FROM --platform=${BUILDPLATFORM} golang:${VERSION} AS base
FROM base AS deps
WORKDIR /go/src/app
ARG TARGETOS=linux
ARG TARGETARCH=arm64
ARG CGO_ENABLED=0
COPY go.mod* go.sum* ./
# GOPROXY is used to specify the module proxy to use.
ARG GOPROXY=direct
COPY . .
ENV GOPROXY=${GOPROXY}
RUN if [ -f go.mod ]; then go mod download; fi
RUN if [ -f go.mod ]; then go mod download && go mod tidy; fi
FROM base AS build
FROM deps AS build
WORKDIR /go/src/app
COPY . .
ARG PACKAGE={{.Package}}
ARG TARGETOS=linux
ARG TARGETARCH=arm64
ARG TARGETARCH=amd64
ARG CGO_ENABLED=0
ARG PACKAGE={{.Package}}
ARG GOPROXY=direct
# -trimpath removes the absolute path to the source code in the binary
# -ldflags="-s -w" removes the symbol table and debug information from the binary
# CGO_ENABLED=0 disables the use of cgo
ENV GOPROXY=${GOPROXY}
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath -ldflags="-s -w" -o /go/bin/app "${PACKAGE}"
FROM debian:stable-slim
Expand All @@ -130,6 +129,7 @@ RUN chown -R nonroot:nonroot /app
COPY --chown=nonroot:nonroot --from=build /go/bin/app .
ENV PORT=8080
EXPOSE ${PORT}
USER nonroot:nonroot
CMD ["/app/app"]
`)
Expand Down

0 comments on commit 0e089ff

Please sign in to comment.