From 23196544e12df845cd5cd6f690cfa8181caa4318 Mon Sep 17 00:00:00 2001 From: Siyu Wang Date: Tue, 9 Aug 2022 10:14:57 +0800 Subject: [PATCH] Optimize Dockerfile for multi-arch (#1052) Signed-off-by: FillZpp --- Dockerfile | 10 ++++------ Dockerfile_multiarch | 31 +++++++++++++++++++++++++++++++ Makefile | 4 ++-- 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 Dockerfile_multiarch diff --git a/Dockerfile b/Dockerfile index c01f992d78..9fcd11c772 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,10 @@ -# Build the manager binary +# Build the manager and daemon binaries FROM golang:1.17 as builder WORKDIR /workspace # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -#RUN go mod download # Copy the go source COPY main.go main.go @@ -20,12 +17,13 @@ COPY vendor/ vendor/ RUN CGO_ENABLED=0 GO111MODULE=on go build -mod=vendor -a -o manager main.go \ && CGO_ENABLED=0 GO111MODULE=on go build -mod=vendor -a -o daemon ./cmd/daemon/main.go -# Use Ubuntu 20.04 LTS as base image to package the manager binary +# Use Ubuntu 20.04 LTS as base image to package the binaries FROM ubuntu:focal -# This is required by daemon connnecting with cri +# This is required by daemon connnecting with CRI RUN ln -s /usr/bin/* /usr/sbin/ && apt-get update -y \ && apt-get install --no-install-recommends -y ca-certificates \ && apt-get clean && rm -rf /var/log/*log /var/lib/apt/lists/* /var/log/apt/* /var/lib/dpkg/*-old /var/cache/debconf/*-old + WORKDIR / COPY --from=builder /workspace/manager . COPY --from=builder /workspace/daemon ./kruise-daemon diff --git a/Dockerfile_multiarch b/Dockerfile_multiarch new file mode 100644 index 0000000000..353e66620c --- /dev/null +++ b/Dockerfile_multiarch @@ -0,0 +1,31 @@ +# Build the manager and daemon binaries +FROM --platform=$BUILDPLATFORM golang:1.17 as builder + +WORKDIR /workspace +# Copy the Go Modules manifests +COPY go.mod go.mod +COPY go.sum go.sum + +# Copy the go source +COPY main.go main.go +COPY apis/ apis/ +COPY cmd/ cmd/ +COPY pkg/ pkg/ +COPY vendor/ vendor/ + +# Build +ARG TARGETOS TARGETARCH +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 GO111MODULE=on go build -mod=vendor -a -o manager main.go \ + && GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 GO111MODULE=on go build -mod=vendor -a -o daemon ./cmd/daemon/main.go + +# Use Ubuntu 20.04 LTS as base image to package the binaries +FROM --platform=$TARGETPLATFORM ubuntu:focal +# This is required by daemon connnecting with CRI +RUN ln -s /usr/bin/* /usr/sbin/ && apt-get update -y \ + && apt-get install --no-install-recommends -y ca-certificates \ + && apt-get clean && rm -rf /var/log/*log /var/lib/apt/lists/* /var/log/apt/* /var/lib/dpkg/*-old /var/cache/debconf/*-old + +WORKDIR / +COPY --from=builder /workspace/manager . +COPY --from=builder /workspace/daemon ./kruise-daemon +ENTRYPOINT ["/manager"] diff --git a/Makefile b/Makefile index c8fe53d037..fd3af92d03 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Image URL to use all building/pushing image targets IMG ?= openkruise/kruise-manager:test # Platforms to build the image for -PLATFORMS ?= linux/amd64,linux/arm64,linux/arm +PLATFORMS ?= linux/amd64,linux/arm64,linux/arm,linux/ppc64le CRD_OPTIONS ?= "crd:crdVersions=v1" # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) @@ -62,7 +62,7 @@ docker-push: ## Push docker image with the manager. # Build and push the multiarchitecture docker images and manifest. docker-multiarch: - docker buildx build --pull --no-cache --platform=$(PLATFORMS) --push . -t $(IMG) + docker buildx build -f ./Dockerfile_multiarch --pull --no-cache --platform=$(PLATFORMS) --push . -t $(IMG) ##@ Deployment