From 75307d60205193aee8fe3e485c750b81aa3ff8a0 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Mon, 20 Jan 2025 15:51:16 +0900 Subject: [PATCH] fix: dockerfile build Signed-off-by: Ji Hwan --- .dockerignore | 1 + Dockerfile | 41 ++++++++++++++++++----------------------- Makefile | 4 ++++ 3 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..c0363794 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +tmp/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f5454383..2f2f79bc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,31 +1,26 @@ -FROM golang:1.22 AS builder +# Build stage +FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder + +# Set the workspace for the build WORKDIR /workspace + +# Copy only necessary Go module files for caching dependencies COPY go.mod go.sum ./ RUN go mod download -COPY abi/ abi/ -COPY bindings/ bindings/ -COPY cmd/ cmd/ -COPY dashboard/ dashboard/ -COPY gethkeystore/ gethkeystore/ -COPY hdwallet/ hdwallet/ -COPY metrics/ metrics/ -COPY p2p/ p2p/ -COPY proto/ proto/ -COPY rpctypes/ rpctypes/ -COPY util/ util/ -COPY main.go ./ -RUN CGO_ENABLED=0 go build -o polycli main.go +# Copy the necessary source code +COPY . ./ + +# Build the Go binary +RUN go build -o /workspace/polycli main.go + +# Final stage: minimal base image +FROM --platform=${BUILDPLATFORM} debian:bookworm-slim -# 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 only the necessary files from the builder image COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt COPY --from=builder /workspace/polycli /usr/bin/polycli -USER 65532:65532 -ENTRYPOINT ["polycli"] -CMD ["--help"] -# How to test this image? -# https://github.com/0xPolygon/polygon-cli/pull/189#discussion_r1464486344 +# Default cmd for the container +CMD ["/bin/sh", "-c", "polycli"] +ENTRYPOINT ["polycli"] \ No newline at end of file diff --git a/Makefile b/Makefile index 2f25115e..2f0af027 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,10 @@ $(BUILD_DIR): ## Create the build folder. build: $(BUILD_DIR) ## Build go binary. go build -ldflags "$(VERSION_FLAGS)" -o $(BUILD_DIR)/$(BIN_NAME) main.go +.PHONY: build-docker +build-docker: ## Builds a docker image with the polycli binary + docker build -t polycli -f ./Dockerfile . + .PHONY: install install: build ## Install the go binary. $(RM) $(INSTALL_DIR)/$(BIN_NAME)