template.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
  14. {{end}}
  15. WORKDIR /build/zero
  16. ADD go.mod .
  17. ADD go.sum .
  18. RUN go mod download
  19. COPY . .
  20. {{if .Argument}}COPY {{.GoRelPath}}/etc /app/etc
  21. {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
  22. FROM alpine
  23. RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
  24. ENV TZ Asia/Shanghai
  25. WORKDIR /app
  26. COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
  27. COPY --from=builder /app/etc /app/etc{{end}}
  28. {{if .HasPort}}
  29. EXPOSE {{.Port}}
  30. {{end}}
  31. CMD ["./{{.ExeFile}}"{{.Argument}}]
  32. `
  33. )
  34. func Clean() error {
  35. return util.Clean(category)
  36. }
  37. func GenTemplates(_ *cli.Context) error {
  38. return initTemplate()
  39. }
  40. func Category() string {
  41. return category
  42. }
  43. func RevertTemplate(name string) error {
  44. return util.CreateTemplate(category, name, dockerTemplate)
  45. }
  46. func Update() error {
  47. err := Clean()
  48. if err != nil {
  49. return err
  50. }
  51. return initTemplate()
  52. }
  53. func initTemplate() error {
  54. return util.InitTemplates(category, map[string]string{
  55. dockerTemplateFile: dockerTemplate,
  56. })
  57. }