This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch refactor-with-go
in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git


The following commit(s) were added to refs/heads/refactor-with-go by this push:
     new 9addd977 add build and push docker images for cross-platform support 
(#1089)
9addd977 is described below

commit 9addd977cb630dfc6933de1fa752d608b36d8a4c
Author: Jun <[email protected]>
AuthorDate: Thu Apr 20 16:54:31 2023 +0800

    add build and push docker images for cross-platform support (#1089)
---
 Dockerfile |  9 ++++++++-
 Makefile   | 28 +++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index cb4ee77a..a05f5d39 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -18,6 +18,8 @@ FROM golang:1.20.1-alpine3.17 as builder
 
 
 # Build argments
+ARG TARGETOS
+ARG TARGETARCH
 ARG LDFLAGS
 ARG PKGNAME
 ARG BUILD
@@ -42,10 +44,15 @@ COPY cmd cmd/
 
 # Build
 RUN env
-RUN go build -ldflags="${LDFLAGS}" -a -o ${PKGNAME} 
/go/src/github.com/apache/dubbo-admin/cmd/${PKGNAME}/main.go
+RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build 
-ldflags="${LDFLAGS}" -a -o ${PKGNAME} 
/go/src/github.com/apache/dubbo-admin/cmd/${PKGNAME}/main.go
 
 
 FROM alpine:3.17
+# Add tzdata package
+RUN apk add --no-cache tzdata
+# Set Timezone
+
+# Build
 WORKDIR /
 ARG PKGNAME
 COPY --from=builder /go/src/github.com/apache/dubbo-admin/${PKGNAME} .
diff --git a/Makefile b/Makefile
index 47009e16..5110b55b 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@
 
 
 GOOS ?= $(shell go env GOOS)
+GOARCH ?= $(shell go env GOARCH)
 
 # Git information
 GIT_VERSION ?= $(shell git describe --tags --always)
@@ -62,7 +63,8 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
 KUSTOMIZE_VERSION ?= v3.8.7
 CONTROLLER_TOOLS_VERSION ?= v0.10.0
 
-
+## docker buildx support platform
+PLATFORMS ?= linux/arm64,linux/amd64
 
 ##@ General
 
@@ -138,10 +140,34 @@ images: image-dubbo-admin image-dubbo-authority  
image-dubbo-admin-ui
 image-dubbo-admin: ## Build docker image with the dubbo admin.
        docker build --build-arg LDFLAGS=$(LDFLAGS) --build-arg PKGNAME=admin 
-t ${DUBBO_ADMIN_IMG} .
 
+
+
+.PHONY: image-dubbo-admin-buildx
+image-dubbo-admin-buildx:  ## Build and push docker image for the dubbo admin 
for cross-platform support
+       # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into 
Dockerfile.cross, and preserve the original Dockerfile
+       sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 
1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > 
Dockerfile_admin.cross
+       - docker buildx create --name project-dubbo-admin-builder
+       docker buildx use project-dubbo-admin-builder
+       - docker buildx build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=admin  --push --platform=$(PLATFORMS) --tag ${DUBBO_ADMIN_IMG} -f 
Dockerfile_admin.cross .
+       #- docker buildx build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=admin  --output type=local,dest=./bin/buildx/dubbo-admin 
--platform=$(PLATFORMS) --tag ${DUBBO_ADMIN_IMG} -f Dockerfile_admin.cross .
+       - docker buildx rm project-dubbo-admin-builder
+       rm Dockerfile_admin.cross
+
 .PHONY: image-dubbo-authority
 image-dubbo-authority: ## Build docker image with the dubbo authority.
        docker build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=authority -t ${DUBBO_AUTHORITY_IMG} .
 
+.PHONY: image-dubbo-authority-buildx
+image-dubbo-authority-buildx:  ## Build and push docker image for the dubbo 
authority for cross-platform support
+       # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into 
Dockerfile.cross, and preserve the original Dockerfile
+       sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 
1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > 
Dockerfile_authority.cross
+       - docker buildx create --name project-dubbo-authority-builder
+       docker buildx use project-dubbo-authority-builder
+       - docker buildx build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=authority  --push --platform=$(PLATFORMS) --tag ${DUBBO_AUTHORITY_IMG} 
-f Dockerfile_authority.cross .
+       #- docker buildx build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=authority  --output type=local,dest=./bin/buildx/dubbo-authority 
--platform=$(PLATFORMS) --tag ${DUBBO_AUTHORITY_IMG} -f 
Dockerfile_authority.cross .
+       - docker buildx rm project-dubbo-authority-builder
+       rm Dockerfile_authority.cross
+
 .PHONY: image-dubbo-admin-ui
 image-dubbo-admin-ui: ## Build docker image with the dubbo-admin-ui.
        docker build --build-arg LDFLAGS=$(LDFLAGS) --build-arg 
PKGNAME=dubbo-admin-ui -t ${DUBBO_ADMIN_UI_IMG} ./dubbo-admin-ui

Reply via email to