Created
September 22, 2023 08:21
-
-
Save anton-petrov/50c6e15760034b4f7c299a1227bea8f2 to your computer and use it in GitHub Desktop.
Go docker example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.21 AS builder | |
ARG VERSION | |
ARG BUILD_TIME | |
RUN git config --global url."git@github.com:".insteadOf "https://github.com/" | |
ENV GOPRIVATE=github.com/company/private-repo | |
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts | |
ADD go.mod go.sum deps/deps.go /deps/ | |
WORKDIR /deps | |
RUN --mount=type=ssh \ | |
--mount=type=cache,target=/go/pkg/mod \ | |
--mount=type=cache,target=/root/.cache/go-build \ | |
go mod download && \ | |
go build -v -o deps . | |
ADD / /src | |
WORKDIR /src | |
RUN mkdir /target | |
ENV CGO_ENABLED=0 | |
RUN --mount=type=cache,target=/go/pkg/mod \ | |
--mount=type=cache,target=/root/.cache/go-build \ | |
go build -ldflags="-X main.revision=${VERSION} -X main.buildTime=${BUILD_TIME} -w -s" -v -o /target/app main.go | |
FROM alpine:latest AS runtime | |
COPY --from=builder /target/app / | |
WORKDIR / | |
CMD ["./app"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment