This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hertzbeat-collector-go.git
The following commit(s) were added to refs/heads/main by this push:
new 45df712 feat: update Dockerfile and docker-compose and related mk
tools (#10)
45df712 is described below
commit 45df712f0161bb306aca6ae77cdb0fe65fe772ce
Author: shown <[email protected]>
AuthorDate: Mon Sep 8 21:26:42 2025 +0800
feat: update Dockerfile and docker-compose and related mk tools (#10)
Signed-off-by: yuluo-yx <[email protected]>
---
Dockerfile | 63 ----------------------
Makefile | 11 ++--
.../docker/docker-compose/docker-compose.yml | 4 +-
Makefile => tools/docker/hcg/Dockerfile | 25 +++++----
tools/make/golang.mk | 22 +++++---
tools/make/image.mk | 58 ++++++++++++++++++++
6 files changed, 97 insertions(+), 86 deletions(-)
diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index 74c38a7..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,63 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
-FROM golang:1.23-alpine AS golang-builder
-
-ARG GOPROXY
-# ENV GOPROXY ${GOPROXY:-direct}
-# ENV GOPROXY=https://proxy.golang.com.cn,direct
-
-ENV GOPATH /go
-ENV GOROOT /usr/local/go
-ENV PACKAGE hertzbeat.apache.org/hertzbeat-collector-go
-ENV BUILD_DIR /app
-
-COPY . ${BUILD_DIR}
-WORKDIR ${BUILD_DIR}
-RUN apk --no-cache add build-base git bash
-
-RUN make init && \
- make fmt && \
- make go-lint &&\
- make build
-
-RUN chmod +x bin/collector
-
-FROM alpine
-
-ARG TIMEZONE
-ENV TIMEZONE=${TIMEZONE:-"Asia/Shanghai"}
-
-RUN apk update \
- && apk --no-cache add \
- bash \
- ca-certificates \
- curl \
- dumb-init \
- gettext \
- openssh \
- sqlite \
- gnupg \
- tzdata \
- && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
- && echo "${TIMEZONE}" > /etc/timezone
-
-COPY --from=golang-builder /app/bin/collector /usr/local/bin/collector
-COPY --from=golang-builder /app/etc/hertzbeat-collector.yml
/etc/hertzbeat-collector.yml
-
-EXPOSE 8090
-ENTRYPOINT ["collector", "server", "--config", "/etc/hertzbeat-collector.yml"]
diff --git a/Makefile b/Makefile
index cd02530..6b33134 100644
--- a/Makefile
+++ b/Makefile
@@ -17,11 +17,12 @@
_run:
@$(MAKE) --warn-undefined-variables \
- -f tools/make/common.mk \
- -f tools/make/golang.mk \
- -f tools/make/linter.mk \
- -f tools/make/tools.mk \
- $(MAKECMDGOALS)
+ -f tools/make/common.mk \
+ -f tools/make/golang.mk \
+ -f tools/make/linter.mk \
+ -f tools/make/image.mk \
+ -f tools/make/tools.mk \
+ $(MAKECMDGOALS)
.PHONY: _run
diff --git a/docker-compose.yml b/tools/docker/docker-compose/docker-compose.yml
similarity index 93%
rename from docker-compose.yml
rename to tools/docker/docker-compose/docker-compose.yml
index 7c158b5..427b692 100644
--- a/docker-compose.yml
+++ b/tools/docker/docker-compose/docker-compose.yml
@@ -19,11 +19,11 @@ services:
# collector go service
hertzbeat-collector:
- image: hertzbeat/hertzbeat-collector-go:latest
+ image: hertzbeat-collector-go:latest
container_name: hertzbeat-collector-go
restart: on-failure
ports:
- - "8090:8090"
+ - "8080:8080"
networks:
hcg-network:
diff --git a/Makefile b/tools/docker/hcg/Dockerfile
similarity index 51%
copy from Makefile
copy to tools/docker/hcg/Dockerfile
index cd02530..2bb553d 100644
--- a/Makefile
+++ b/tools/docker/hcg/Dockerfile
@@ -15,14 +15,21 @@
# specific language governing permissions and limitations
# under the License.
-_run:
- @$(MAKE) --warn-undefined-variables \
- -f tools/make/common.mk \
- -f tools/make/golang.mk \
- -f tools/make/linter.mk \
- -f tools/make/tools.mk \
- $(MAKECMDGOALS)
+FROM
docker.io/library/busybox@sha256:ab33eacc8251e3807b85bb6dba570e4698c3998eca6f0fc2ccb60575a563ea74
AS builder
-.PHONY: _run
+# prepare hertzbeat data dir
+RUN mkdir -p /var/hertzbeat
-$(if $(MAKECMDGOALS),$(MAKECMDGOALS): %: _run)
+# 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/base-nossl:nonroot@sha256:8981b63f968e829d21351ea9d28cc21127e5f034707f1d8483d2993d9577be0b
+
+COPY --from=builder /var/hertzbeat/ /var/hertzbeat/
+
+# copy binary to image, run make build to generate binary.
+COPY bin /usr/local/bin/
+COPY etc /var/hertzbeat/config/
+
+USER 65532:65532
+
+ENTRYPOINT ["/usr/local/bin/collector", "server", "--config",
"/var/hertzbeat/config/hertzbeat-collector.yaml"]
diff --git a/tools/make/golang.mk b/tools/make/golang.mk
index 0fe356f..a67ec75 100644
--- a/tools/make/golang.mk
+++ b/tools/make/golang.mk
@@ -18,37 +18,46 @@
VERSION_PACKAGE :=
hertzbeat.apache.org/hertzbeat-collector-go/internal/cmd/version
GO_LDFLAGS += -X $(VERSION_PACKAGE).hcgVersion=$(shell cat VERSION) \
- -X $(VERSION_PACKAGE).gitCommitID=$(GIT_COMMIT)
+-X $(VERSION_PACKAGE).gitCommitID=$(GIT_COMMIT)
GIT_COMMIT:=$(shell git rev-parse HEAD)
+GOOS ?= $(shell go env GOOS)
+GOARCH ?= $(shell go env GOARCH)
+
##@ Golang
.PHONY: fmt
fmt: ## Golang fmt
+ @$(LOG_TARGET)
go fmt ./...
.PHONY: vet
vet: ## Golang vet
+ @$(LOG_TARGET)
go vet ./...
.PHONY: dev
dev: ## Golang dev, run main by run.
+ @$(LOG_TARGET)
go run cmd/main.go server --config etc/hertzbeat-collector.yaml
.PHONY: prod
prod: ## Golang prod, run bin by run.
+ @$(LOG_TARGET)
bin/collector server --config etc/hertzbeat-collector.yaml
.PHONY: build
-# build
+build: GOOS = linux
build: ## Golang build
+ @$(LOG_TARGET)
@version=$$(cat VERSION); \
# todo; 添加交叉编译支持
- CGO_ENABLED=0 go build -o bin/collector -ldflags "$(GO_LDFLAGS)"
cmd/main.go
+ CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/collector
-ldflags "$(GO_LDFLAGS)" cmd/main.go
.PHONY: init
init: ## install base. For proto compile.
+ @$(LOG_TARGET)
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2@latest
@@ -58,6 +67,7 @@ init: ## install base. For proto compile.
# generate api proto
api: API_PROTO_FILES := $(wildcard api/*.proto)
api: ## compile api proto files
+ @$(LOG_TARGET)
protoc --proto_path=./api \
--go_out=paths=source_relative:./api \
--go-http_out=paths=source_relative:./api \
@@ -66,12 +76,10 @@ api: ## compile api proto files
.PHONY: go-lint
go-lint: ## run golang lint
+ @$(LOG_TARGET)
golangci-lint run --config tools/linter/golang-ci/.golangci.yml
.PHONY: test
test: ## run golang test
+ @$(LOG_TARGET)
go test -v ./...
-
-.PHONY: golang-all
-golang-all: ## run fmt lint vet build api test
-golang-all: fmt go-lint vet build api test
diff --git a/tools/make/image.mk b/tools/make/image.mk
new file mode 100644
index 0000000..fc37001
--- /dev/null
+++ b/tools/make/image.mk
@@ -0,0 +1,58 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# This is a wrapper to build and push docker image
+#
+
+# All make targets related to docker image are defined in this file.
+
+REGISTRY ?= docker.io
+
+TAG ?= $(shell git rev-parse HEAD)
+
+DOCKER := docker
+DOCKER_SUPPORTED_API_VERSION ?= 1.32
+
+IMAGES_DIR ?= $(wildcard tools/docker/hcg)
+
+IMAGES ?= hertzbeat-collector-go
+IMAGE_PLATFORMS ?= amd64 arm64
+
+BUILDX_CONTEXT = hcg-build-tools-builder
+
+##@ Image
+
+# todo: multi-platform build
+
+.PHONY: image-build
+image-build: ## Build docker image
+image-build: IMAGE_PLATFORMS = ${shell uname -m}
+image-build:
+ @$(LOG_TARGET)
+ make build
+ $(DOCKER) buildx create --name $(BUILDX_CONTEXT) --use; \
+ $(DOCKER) buildx use $(BUILDX_CONTEXT); \
+ $(DOCKER) buildx build --load \
+ -t $(REGISTRY)/${IMAGES}:$(TAG) \
+ --platform linux/${IMAGE_PLATFORMS} \
+ --file $(IMAGES_DIR)/Dockerfile . ; \
+ $(DOCKER) buildx rm $(BUILDX_CONTEXT)
+
+.PHONY: image-push
+image-push: ## Push docker image
+image-push:
+ @$(LOG_TARGET)
+ $(DOCKER) push $(REGISTRY)/$${image}:$(TAG)-$${platform}; \
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]