template.go 956 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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{{end}}
  14. WORKDIR /build/zero
  15. ADD go.mod .
  16. ADD go.sum .
  17. RUN go mod download
  18. COPY . .
  19. COPY {{.GoRelPath}}/etc /app/etc
  20. RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
  21. FROM alpine
  22. RUN apk update --no-cache
  23. RUN apk add --no-cache ca-certificates
  24. RUN apk add --no-cache tzdata
  25. ENV TZ Asia/Shanghai
  26. WORKDIR /app
  27. COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}
  28. COPY --from=builder /app/etc /app/etc
  29. CMD ["./{{.ExeFile}}"{{.Argument}}]
  30. `
  31. )
  32. func GenTemplates(_ *cli.Context) error {
  33. return util.InitTemplates(category, map[string]string{
  34. dockerTemplateFile: dockerTemplate,
  35. })
  36. }