From 6709f50785f2623207b21f525b9db7c8b57f03c9 Mon Sep 17 00:00:00 2001 From: Allo Date: Wed, 21 Aug 2024 10:43:14 +0800 Subject: [PATCH] refactor: extract dockerfile content to standalone files --- .licenserc.yaml | 1 + app/dubboctl/internal/dubbo/dockerfile.go | 59 +++++-------------- .../internal/dubbo/dockerfiles/go.dockerfile | 27 +++++++++ .../dubbo/dockerfiles/java.dockerfile | 7 +++ app/dubboctl/internal/dubbo/dubbo.go | 2 +- 5 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 app/dubboctl/internal/dubbo/dockerfiles/go.dockerfile create mode 100644 app/dubboctl/internal/dubbo/dockerfiles/java.dockerfile diff --git a/.licenserc.yaml b/.licenserc.yaml index 3de0b608f..19ff1db92 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -83,6 +83,7 @@ header: - '**/docs/**' - '**/.husky/pre-commit' - '**/.nvmrc' + - 'app/dubboctl/internal/dubbo/dockerfiles/**' - '**/**.txtar' - '**/**gen.go' - '**/api/**' diff --git a/app/dubboctl/internal/dubbo/dockerfile.go b/app/dubboctl/internal/dubbo/dockerfile.go index 63b7d1e2f..2d878f06a 100644 --- a/app/dubboctl/internal/dubbo/dockerfile.go +++ b/app/dubboctl/internal/dubbo/dockerfile.go @@ -17,49 +17,20 @@ package dubbo -var ( - DockerfileGolang = ` -# Please modify your template according to your business needs!!! - -FROM golang:alpine AS builder - -LABEL stage=gobuilder - -ENV CGO_ENABLED 0 -ENV GOPROXY https://goproxy.cn,direct -RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories - -WORKDIR /build - -ADD go.mod . -ADD go.sum . -RUN go mod download -COPY . . -COPY ./conf /app/conf -RUN go build -ldflags="-s -w" -o /app/dubbogo ./cmd - -FROM scratch - -WORKDIR /app -COPY --from=builder /app/dubbogo /app/dubbogo -COPY --from=builder /app/conf /app/conf -ENV DUBBO_GO_CONFIG_PATH=/app/conf/dubbogo.yaml - -CMD ["./dubbogo"] -` - - DockerfileJava = ` -# Please modify your template according to your business needs!!! - -FROM openjdk:8-jdk-alpine - -ADD target/demo-0.0.1-SNAPSHOT.jar app.jar -ENV JAVA_OPTS="" -ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar -` +import ( + _ "embed" +) - DockerfileByRuntime = map[string]string{ - "go": DockerfileGolang, - "java": DockerfileJava, - } +// embeded dockerfile content +var ( + //go:embed dockerfiles/go.dockerfile + dockerfileGolang []byte + //go:embed dockerfiles/java.dockerfile + dockerfileJava []byte ) + +// DockerfileByRuntime is a map of runtime to dockerfile content. +var DockerfileByRuntime = map[string][]byte{ + "go": dockerfileGolang, + "java": dockerfileJava, +} diff --git a/app/dubboctl/internal/dubbo/dockerfiles/go.dockerfile b/app/dubboctl/internal/dubbo/dockerfiles/go.dockerfile new file mode 100644 index 000000000..ea791b36d --- /dev/null +++ b/app/dubboctl/internal/dubbo/dockerfiles/go.dockerfile @@ -0,0 +1,27 @@ +# Please modify your template according to your business needs!!! + +FROM golang:alpine AS builder + +LABEL stage=gobuilder + +ENV CGO_ENABLED 0 +ENV GOPROXY https://goproxy.cn,direct +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories + +WORKDIR /build + +ADD go.mod . +ADD go.sum . +RUN go mod download +COPY . . +COPY ./conf /app/conf +RUN go build -ldflags="-s -w" -trimpath -o /app/dubbogo ./cmd + +FROM scratch + +WORKDIR /app +COPY --from=builder /app/dubbogo /app/dubbogo +COPY --from=builder /app/conf /app/conf +ENV DUBBO_GO_CONFIG_PATH=/app/conf/dubbogo.yaml + +CMD ["./dubbogo"] diff --git a/app/dubboctl/internal/dubbo/dockerfiles/java.dockerfile b/app/dubboctl/internal/dubbo/dockerfiles/java.dockerfile new file mode 100644 index 000000000..711773a31 --- /dev/null +++ b/app/dubboctl/internal/dubbo/dockerfiles/java.dockerfile @@ -0,0 +1,7 @@ +# Please modify your template according to your business needs!!! + +FROM openjdk:8-jdk-alpine + +ADD target/demo-0.0.1-SNAPSHOT.jar app.jar +ENV JAVA_OPTS="" +ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar diff --git a/app/dubboctl/internal/dubbo/dubbo.go b/app/dubboctl/internal/dubbo/dubbo.go index 0ea87b667..ca48d88d8 100644 --- a/app/dubboctl/internal/dubbo/dubbo.go +++ b/app/dubboctl/internal/dubbo/dubbo.go @@ -474,7 +474,7 @@ func (f *Dubbo) EnsureDockerfile(cmd *cobra.Command) (err error) { fmt.Fprintln(cmd.OutOrStdout(), "The runtime of your current project is not one of Java or go. We cannot help you generate a Dockerfile template.") return } - if err = os.WriteFile(dockerfilepath, []byte(dockerfilebytes), 0o644); err != nil { + if err = os.WriteFile(dockerfilepath, dockerfilebytes, 0o644); err != nil { return } return