From 76b0d133213bc7eb008e2714af6d22672406847f Mon Sep 17 00:00:00 2001 From: panigs7 <92028646+panigs7@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:43:23 +0530 Subject: [PATCH] Multistage docker build for powerflex (#161) --- .gitignore | 2 ++ .golangci.yaml | 2 +- Dockerfile | 20 ++++++++++++++++++-- Makefile | 2 +- README.md | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index cf80b28..9ef108a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor/ cmd/*/bin/ **/.vscode/ **/.idea/ + +csm-common.mk \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml index 56f5332..d20d7e0 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,6 +1,6 @@ run: timeout: 20m - issue-exit-code: 0 # we will change this later + issues-exit-code: 0 # we will change this later tests: true skip-dirs-use-default: true modules-download-mode: readonly diff --git a/Dockerfile b/Dockerfile index 6574a8c..f171282 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,22 @@ ARG BASEIMAGE +# Build the sdk binary +FROM golang:1.21 as builder + +# Set envirment variable +ENV APP_NAME karavi-metrics-powerflex +ENV CMD_PATH cmd/metrics-powerflex/main.go + +# Copy application data into image +COPY . /go/src/$APP_NAME +WORKDIR /go/src/$APP_NAME + +# Build the binary +RUN go install github.com/golang/mock/mockgen@v1.6.0 +RUN go generate ./... +RUN CGO_ENABLED=0 GOOS=linux go build -o /go/src/service /go/src/$APP_NAME/$CMD_PATH + +# Build the sdk image FROM $BASEIMAGE as final LABEL vendor="Dell Inc." \ name="csm-metrics-powerflex" \ @@ -7,6 +24,5 @@ LABEL vendor="Dell Inc." \ description="Provides insight into storage usage and performance as it relates to the CSI (Container Storage Interface) Driver for Dell PowerFlex" \ version="2.0.0" \ license="Apache-2.0" -ARG SERVICE -COPY $SERVICE/bin/service /service +COPY --from=builder /go/src/service / ENTRYPOINT ["/service"] diff --git a/Makefile b/Makefile index 2e9dd12..3641c51 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ download-csm-common: .PHONY: docker docker: download-csm-common $(eval include csm-common.mk) - SERVICE=cmd/metrics-powerflex docker build -t csm-metrics-powerflex -f Dockerfile --build-arg BASEIMAGE=$(DEFAULT_BASEIMAGE) cmd/metrics-powerflex/ + docker build -t csm-metrics-powerflex -f Dockerfile --build-arg BASEIMAGE=$(DEFAULT_BASEIMAGE) . .PHONY: push push: diff --git a/README.md b/README.md index deeac4c..59de489 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Once all prerequisites are on the Linux host, follow the steps below to clone an 1. Clone the repository using the following command: `git clone https://github.com/dell/karavi-metrics-powerflex.git` 1. Set the DOCKER_REPO environment variable to point to the local Docker repository, for example: `export DOCKER_REPO=:` -1. In the karavi-metrics-powerflex directory, run the following command to build the Docker image called karavi-metrics-powerflex: `make clean build docker` +1. In the karavi-metrics-powerflex directory, run the following command to build the Docker image called csm-metrics-powerflex: `make docker` 1. Tag (with the "latest" tag) and push the image to the local Docker repository by running the following command: `make tag push` __Note:__ Linux support only. If you are using a local insecure docker registry, ensure you configure the insecure registries on each of the Kubernetes worker nodes to allow access to the local docker repository.