2020-11-09 18:02:16 +08:00
|
|
|
package docker
|
2020-07-29 17:11:41 +08:00
|
|
|
|
2020-11-09 18:02:16 +08:00
|
|
|
import (
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
category = "docker"
|
|
|
|
dockerTemplateFile = "docker.tpl"
|
|
|
|
dockerTemplate = `FROM golang:alpine AS builder
|
2020-07-29 17:11:41 +08:00
|
|
|
|
|
|
|
LABEL stage=gobuilder
|
|
|
|
|
|
|
|
ENV CGO_ENABLED 0
|
|
|
|
ENV GOOS linux
|
2020-12-05 21:48:09 +08:00
|
|
|
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct{{end}}
|
2020-07-29 17:11:41 +08:00
|
|
|
|
2020-11-08 21:28:58 +08:00
|
|
|
WORKDIR /build/zero
|
2020-12-05 21:48:09 +08:00
|
|
|
|
|
|
|
ADD go.mod .
|
|
|
|
ADD go.sum .
|
|
|
|
RUN go mod download
|
2020-07-29 17:11:41 +08:00
|
|
|
COPY . .
|
2020-12-05 21:48:09 +08:00
|
|
|
COPY {{.GoRelPath}}/etc /app/etc
|
|
|
|
RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
|
2020-07-29 17:11:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
FROM alpine
|
|
|
|
|
|
|
|
RUN apk update --no-cache
|
|
|
|
RUN apk add --no-cache ca-certificates
|
|
|
|
RUN apk add --no-cache tzdata
|
|
|
|
ENV TZ Asia/Shanghai
|
|
|
|
|
|
|
|
WORKDIR /app
|
2020-12-05 21:48:09 +08:00
|
|
|
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}
|
2020-11-08 21:28:58 +08:00
|
|
|
COPY --from=builder /app/etc /app/etc
|
2020-07-29 17:11:41 +08:00
|
|
|
|
2020-12-05 21:48:09 +08:00
|
|
|
CMD ["./{{.ExeFile}}"{{.Argument}}]
|
2020-07-29 17:11:41 +08:00
|
|
|
`
|
2020-11-09 18:02:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func GenTemplates(_ *cli.Context) error {
|
|
|
|
return util.InitTemplates(category, map[string]string{
|
|
|
|
dockerTemplateFile: dockerTemplate,
|
|
|
|
})
|
|
|
|
}
|