From 6d572b328891988e693fab6846f4b4607347222f Mon Sep 17 00:00:00 2001 From: codebien <2103732+codebien@users.noreply.github.com> Date: Wed, 6 Sep 2023 15:29:38 +0200 Subject: [PATCH] docker: Multiplatform cross-compilation --- Dockerfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index b736f890972d..85ac354a8c77 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,10 @@ -FROM golang:1.20-alpine3.18 as builder +FROM --platform=$BUILDPLATFORM golang:1.20-alpine3.18 as builder WORKDIR $GOPATH/src/go.k6.io/k6 -COPY . . +ARG TARGETOS TARGETARCH RUN apk --no-cache add git=~2 -RUN CGO_ENABLED=0 go install -a -trimpath -ldflags "-s -w -X go.k6.io/k6/lib/consts.VersionDetails=$(date -u +"%FT%T%z")/$(git describe --tags --always --long --dirty)" +RUN --mount=target=.,readonly \ + --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -a -trimpath -o /usr/bin/k6 # Runtime stage FROM alpine:3.18 as release @@ -10,7 +12,7 @@ FROM alpine:3.18 as release # hadolint ignore=DL3018 RUN apk add --no-cache ca-certificates && \ adduser -D -u 12345 -g 12345 k6 -COPY --from=builder /go/bin/k6 /usr/bin/k6 +COPY --from=builder /usr/bin/k6 /usr/bin/k6 USER k6 WORKDIR /home/k6