From 9177911152c3207d960832f91fb60bf14d5fac1f Mon Sep 17 00:00:00 2001 From: Daniel Aloni Date: Sun, 19 Mar 2023 11:10:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20Build=20docker=20image=20without?= =?UTF-8?q?=20--mount.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index f09e767b9..b3d4dee4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,26 +2,31 @@ # base installs required dependencies and runs go mod download to cache dependencies # ARG BUILDPLATFORM=${BUILDPLATFORM} -FROM --platform=${BUILDPLATFORM} docker.io/golang:1.20-alpine AS base +FROM --platform=$BUILDPLATFORM docker.io/golang:1.20-alpine AS base RUN apk --update --no-cache add bash build-base curl # # build creates all needed binaries # -FROM --platform=${BUILDPLATFORM} base AS build +FROM --platform=$BUILDPLATFORM base AS build WORKDIR /src ARG TARGETOS ARG TARGETARCH ARG FLAGS -RUN --mount=target=. \ - --mount=type=cache,target=/root/.cache/go-build \ - --mount=type=cache,target=/go/pkg/mod \ - USERARCH=`go env GOARCH` \ - GOARCH="$TARGETARCH" \ - GOOS="linux" \ - CGO_ENABLED=$([ "$TARGETARCH" = "$USERARCH" ] && echo "1" || echo "0") \ - go build -v -ldflags="${FLAGS}" -trimpath -o /out/ ./cmd/... +# Mount volumes using the -v flag instead of --mount to avoid requiring BuildKit which is not easily supported using cloudbuild. +COPY . . +RUN mkdir -p /root/.cache/go-build && \ + mkdir -p /go/pkg/mod +VOLUME /root/.cache/go-build +VOLUME /go/pkg/mod + +# Run the build command in multiple RUN commands +RUN USERARCH=`go env GOARCH` +ENV GOARCH="$TARGETARCH" +ENV GOOS="linux" +RUN CGO_ENABLED=$([ "$TARGETARCH" = "$USERARCH" ] && echo "1" || echo "0") +RUN go build -v -ldflags="${FLAGS}" -trimpath -o /out/ ./cmd/... # # Builds the Dendrite image containing all required binaries