From f21c3fb763e6330f305b16bd09340975950e6819 Mon Sep 17 00:00:00 2001 From: berg Date: Tue, 13 Sep 2022 10:35:45 +0800 Subject: [PATCH] dockerfile support multiarch(amd64,arm64) (#83) Signed-off-by: liheng.zms Signed-off-by: liheng.zms --- Dockerfile_multiarch | 26 ++++++++++++++++++++++++++ Makefile | 5 +++++ docs/tutorials/basic_usage.md | 6 ++---- 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 Dockerfile_multiarch diff --git a/Dockerfile_multiarch b/Dockerfile_multiarch new file mode 100644 index 0000000..7033106 --- /dev/null +++ b/Dockerfile_multiarch @@ -0,0 +1,26 @@ +# Build the manager binary +FROM --platform=$BUILDPLATFORM golang:1.16 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 api/ api/ +COPY pkg/ pkg/ + +# Build +ARG TARGETOS TARGETARCH +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go + +# Use distroless as minimal base image to package the manager binary +# Refer to https://github.com/GoogleContainerTools/distroless for more details +FROM gcr.io/distroless/static:nonroot +WORKDIR / +COPY --from=builder /workspace/manager . +USER 65532:65532 + +ENTRYPOINT ["/manager"] diff --git a/Makefile b/Makefile index 38999fd..6931229 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ # Image URL to use all building/pushing image targets IMG ?= controller:latest +# Platforms to build the image for +PLATFORMS ?= linux/amd64,linux/arm64 # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -68,6 +70,9 @@ docker-build: ## Build docker image with the manager. docker-push: ## Push docker image with the manager. docker push ${IMG} +# Build and push the multiarchitecture docker images and manifest. +docker-multiarch: + docker buildx build -f ./Dockerfile_multiarch --pull --no-cache --platform=$(PLATFORMS) --push . -t $(IMG) ##@ Deployment install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. diff --git a/docs/tutorials/basic_usage.md b/docs/tutorials/basic_usage.md index 6b82dc0..21258d5 100644 --- a/docs/tutorials/basic_usage.md +++ b/docs/tutorials/basic_usage.md @@ -31,7 +31,7 @@ spec: containers: - name: echoserver # mac m1 should choics image can support arm64,such as image e2eteam/echoserver:2.2-linux-arm64 - image: cilium/echoserver:1.10.2 + image: cilium/echoserver:1.10.2 imagePullPolicy: IfNotPresent ports: - containerPort: 8080 @@ -107,9 +107,7 @@ spec: trafficRoutings: # echoserver service name - service: echoserver - # nginx ingress - type: nginx - # echoserver ingress name + # echoserver ingress name, current only nginx ingress ingress: name: echoserver ```