Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Created March 31, 2022 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developer-guy/84be081a6c476f4e6e5e81e5f2f62cc2 to your computer and use it in GitHub Desktop.
Save developer-guy/84be081a6c476f4e6e5e81e5f2f62cc2 to your computer and use it in GitHub Desktop.
Cross-compilation support enabled Dockerfile for Go applications
# 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