template.go 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 && apk add --no-cache ca-certificates 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. }