Skip to content

Instantly share code, notes, and snippets.

@anton-petrov
Created September 22, 2023 08:21
Show Gist options
  • Save anton-petrov/50c6e15760034b4f7c299a1227bea8f2 to your computer and use it in GitHub Desktop.
Save anton-petrov/50c6e15760034b4f7c299a1227bea8f2 to your computer and use it in GitHub Desktop.
Go docker example
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