Last active
November 25, 2020 10:22
-
-
Save developer-guy/7c012a8975ac505faa2e00b528856398 to your computer and use it in GitHub Desktop.
cross-compile-go-app-with-buildx
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
#!/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
Helpful links down below: