🐳 Build docker image without --mount.

This commit is contained in:
Daniel Aloni 2023-03-19 11:10:42 +02:00
parent e16a7363e7
commit 9177911152

View file

@ -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