Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Last active November 25, 2020 10:22
Show Gist options
  • Save developer-guy/7c012a8975ac505faa2e00b528856398 to your computer and use it in GitHub Desktop.
Save developer-guy/7c012a8975ac505faa2e00b528856398 to your computer and use it in GitHub Desktop.
cross-compile-go-app-with-buildx
#!/usr/bin/env bash
set -e
WORKDIR=$(mktemp -d)
cd $WORKDIR
cat << EOF > $WORKDIR/main.go
package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}
EOF
cat << EOF > $WORKDIR/Dockerfile
FROM --platform=${BUILDPLATFORM} golang:1.14.3-alpine AS build
WORKDIR /src
ENV CGO_ENABLED=0
COPY . .
ARG TARGETOS
ARG TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
FROM scratch AS bin
COPY --from=build /out/example /
EOF
BUILDER=cross-compile-go-app
docker buildx create --use --name=$BUILDER
docker buildx inspect --bootstrap $BUILDER
DOCKERID=devopps
docker buildx build \
--push -t $DOCKERID/hello-cross-compiled:latest \
--platform=linux/amd64,linux/arm64 .
# cleanup
docker buildx rm $BUILDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment