Skip to content

Commit

Permalink
refactor: extract dockerfile content to standalone files
Browse files Browse the repository at this point in the history
  • Loading branch information
yin1999 committed Aug 21, 2024
1 parent 92d9790 commit 6709f50
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ header:
- '**/docs/**'
- '**/.husky/pre-commit'
- '**/.nvmrc'
- 'app/dubboctl/internal/dubbo/dockerfiles/**'
- '**/**.txtar'
- '**/**gen.go'
- '**/api/**'
Expand Down
59 changes: 15 additions & 44 deletions app/dubboctl/internal/dubbo/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
27 changes: 27 additions & 0 deletions app/dubboctl/internal/dubbo/dockerfiles/go.dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 7 additions & 0 deletions app/dubboctl/internal/dubbo/dockerfiles/java.dockerfile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/dubboctl/internal/dubbo/dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6709f50

Please sign in to comment.