Created
March 31, 2022 12:46
-
-
Save developer-guy/84be081a6c476f4e6e5e81e5f2f62cc2 to your computer and use it in GitHub Desktop.
Cross-compilation support enabled Dockerfile for Go applications
This file contains hidden or 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
# syntax = docker/dockerfile:1.4.0 | |
FROM --platform=${BUILDPLATFORM} golang:1.17.8-alpine AS base | |
WORKDIR /src | |
ENV CGO_ENABLED=0 | |
COPY go.* . | |
# https://go.dev/ref/mod#module-cache | |
RUN --mount=type=cache,target=/go/pkg/mod go mod download | |
COPY . . | |
FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx | |
FROM base AS build | |
COPY --from=xx / / | |
ARG TARGETPLATFORM | |
# https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching | |
RUN --mount=type=cache,target=/root/.cache/go-build \ | |
--mount=type=cache,target=/go/pkg/mod \ | |
xx-go build -o /out/example . | |
FROM scratch AS bin-unix | |
COPY --from=build /out/example / | |
ENTRYPOINT [ "/example"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment