Skip to content

Commit

Permalink
Optimize Dockerfile for multi-arch (#1052)
Browse files Browse the repository at this point in the history
Signed-off-by: FillZpp <[email protected]>
  • Loading branch information
FillZpp authored Aug 9, 2022
1 parent 4263a74 commit 2319654
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
10 changes: 4 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions Dockerfile_multiarch
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2319654

Please sign in to comment.