template.go 921 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package docker
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/util"
  4. "github.com/urfave/cli"
  5. )
  6. const (
  7. category = "docker"
  8. dockerTemplateFile = "docker.tpl"
  9. dockerTemplate = `FROM golang:alpine AS builder
  10. LABEL stage=gobuilder
  11. ENV CGO_ENABLED 0
  12. ENV GOOS linux
  13. ENV GOPROXY https://goproxy.cn,direct
  14. WORKDIR /build/zero
  15. COPY . .
  16. RUN sh -c "[ -f go.mod ]" || exit
  17. COPY {{.goRelPath}}/etc /app/etc
  18. RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}}
  19. FROM alpine
  20. RUN apk update --no-cache
  21. RUN apk add --no-cache ca-certificates
  22. RUN apk add --no-cache tzdata
  23. ENV TZ Asia/Shanghai
  24. WORKDIR /app
  25. COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}}
  26. COPY --from=builder /app/etc /app/etc
  27. CMD ["./{{.exeFile}}"{{.argument}}]
  28. `
  29. )
  30. func GenTemplates(_ *cli.Context) error {
  31. return util.InitTemplates(category, map[string]string{
  32. dockerTemplateFile: dockerTemplate,
  33. })
  34. }