Skip to content

Commit

Permalink
*: Init recipe for image build
Browse files Browse the repository at this point in the history
And the binary will be built with static tag.

Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Dec 8, 2023
1 parent 82e36b2 commit 1d65f9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:1.20 AS build-stage

WORKDIR /gomod
COPY go.mod go.sum ./
RUN go mod download

RUN mkdir -p /output

WORKDIR /kperf-build
RUN --mount=source=./,target=/kperf-build,rw make build && PREFIX=/output make install

FROM gcr.io/distroless/static-debian12:nonroot AS release-stage

WORKDIR /

COPY --from=build-stage /output/bin/kperf /kperf

USER nonroot:nonroot

ENTRYPOINT ["/kperf"]
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
COMMANDS=kperf

# PREFIX is base path to install.
PREFIX ?= /usr/local

GO_BUILDTAGS = -tags "osusergo netgo static_build"

# IMAGE_REPO is default repo for image-build recipe.
IMAGE_REPO ?= localhost:5000
IMAGE_TAG ?= latest
IMAGE_NAME = $(IMAGE_REPO)/kperf:$(IMAGE_TAG)

BINARIES=$(addprefix bin/,$(COMMANDS))

# default recipe is build
Expand All @@ -9,11 +19,23 @@ BINARIES=$(addprefix bin/,$(COMMANDS))
ALWAYS:

bin/%: cmd/% ALWAYS
@go build -o $@ ./$<
@GO_ENABLED=0 go build -o $@ ${GO_BUILDTAGS} ./$<

build: $(BINARIES) ## build binaries
@echo "$@"

install: ## install binaries
@install -d $(PREFIX)/bin
@install $(BINARIES) $(PREFIX)/bin

image-build: ## build image
@echo building ${IMAGE_NAME}
@docker build . -t ${IMAGE_NAME}

image-push: image-build ## push image
@echo pushing ${IMAGE_NAME}
@docker push ${IMAGE_NAME}

test: ## run test
@go test -v ./...

Expand Down

0 comments on commit 1d65f9b

Please sign in to comment.