Fix docker build

This commit is contained in:
Yusuke Kuoka 2022-03-03 01:40:45 +00:00
parent d20ad71071
commit 25570a0c6d
1 changed files with 24 additions and 17 deletions

View File

@ -1,32 +1,39 @@
# Build the manager binary # Build the manager binary
FROM golang:1.17 as builder FROM --platform=$BUILDPLATFORM golang:1.17 as builder
ARG TARGETPLATFORM
WORKDIR /workspace WORKDIR /workspace
ENV GO111MODULE=on \ # Copy the Go Modules manifests
CGO_ENABLED=0 COPY go.mod go.sum ./
# # Copy the Go Modules manifests # cache deps before building and copying source so that we don't need to re-download as much
# COPY go.mod go.sum ./ # and so that source changes don't invalidate our downloaded layer.
#
# # cache deps before building and copying source so that we don't need to re-download as much # Also, we need to do this before setting TARGETPLATFORM/TARGETOS/TARGETARCH/TARGETVARIANT
# # and so that source changes don't invalidate our downloaded layer # so that go mod cache is shared across platforms.
# RUN --mount=type=cache,target=/go/pkg/mod go mod download RUN go mod download
# Copy the go source # Copy the go source
# COPY . . # COPY . .
ARG TARGETOS # Usage:
ARG TARGETARCH # docker buildx build --tag repo/img:tag -f ./Dockerfile . --platform linux/amd64,linux/arm64,linux/arm/v7
#
# With the above commmand,
# TARGETOS can be "linux", TARGETARCH can be "amd64", "arm64", and "arm", TARGETVARIANT can be "v7".
ARG TARGETPLATFORM TARGETOS TARGETARCH TARGETVARIANT
# We intentionally avoid `--mount=type=cache,mode=0777,target=/go/pkg/mod` in the `go mod download` and the `go build` runs
# to avoid https://github.com/moby/buildkit/issues/2334
# We can use docker layer cache so the build is fast enogh anyway
# We also use per-platform GOCACHE for the same reason.
env GOCACHE /build/${TARGETPLATFORM}/root/.cache/go-build
# Build # Build
RUN --mount=target=. \ RUN --mount=target=. \
--mount=type=cache,mode=0777,target=/root/.cache/go-build \ --mount=type=cache,mode=0777,target=${GOCACHE} \
--mount=type=cache,mode=0777,target=/go/pkg/mod\ GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=${TARGETVARIANT#v} \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
GOARM=$(echo ${TARGETPLATFORM} | cut -d / -f3 | cut -c2-) \
go build -o /out/manager main.go && go build -o /out/github-webhook-server ./cmd/githubwebhookserver go build -o /out/manager main.go && go build -o /out/github-webhook-server ./cmd/githubwebhookserver
# Use distroless as minimal base image to package the manager binary # Use distroless as minimal base image to package the manager binary