chore: reduce the docker image size (#1633)

* chore: reduce the docker image size

* chore: format dockerfile
This commit is contained in:
Kevin Wan
2022-03-11 11:30:21 +08:00
committed by GitHub
parent 4624390e54
commit d6a692971f
3 changed files with 35 additions and 22 deletions

View File

@@ -24,14 +24,16 @@ const (
// Docker describes a dockerfile // Docker describes a dockerfile
type Docker struct { type Docker struct {
Chinese bool Chinese bool
GoRelPath string GoRelPath string
GoFile string GoFile string
ExeFile string ExeFile string
HasPort bool HasPort bool
Port int Port int
Argument string Argument string
Version string Version string
HasTimezone bool
Timezone string
} }
// DockerCommand provides the entry for goctl docker // DockerCommand provides the entry for goctl docker
@@ -47,6 +49,7 @@ func DockerCommand(c *cli.Context) (err error) {
version := c.String("version") version := c.String("version")
remote := c.String("remote") remote := c.String("remote")
branch := c.String("branch") branch := c.String("branch")
timezone := c.String("tz")
if len(remote) > 0 { if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch) repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 { if len(repo) > 0 {
@@ -72,7 +75,7 @@ func DockerCommand(c *cli.Context) (err error) {
port := c.Int("port") port := c.Int("port")
if _, err := os.Stat(etcDir); os.IsNotExist(err) { if _, err := os.Stat(etcDir); os.IsNotExist(err) {
return generateDockerfile(goFile, port, version) return generateDockerfile(goFile, port, version, timezone)
} }
cfg, err := findConfig(goFile, etcDir) cfg, err := findConfig(goFile, etcDir)
@@ -80,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) {
return err return err
} }
if err := generateDockerfile(goFile, port, version, "-f", "etc/"+cfg); err != nil { if err := generateDockerfile(goFile, port, version, timezone, "-f", "etc/"+cfg); err != nil {
return err return err
} }
@@ -121,7 +124,7 @@ func findConfig(file, dir string) (string, error) {
return files[0], nil return files[0], nil
} }
func generateDockerfile(goFile string, port int, version string, args ...string) error { func generateDockerfile(goFile string, port int, version, timezone string, args ...string) error {
projPath, err := getFilePath(filepath.Dir(goFile)) projPath, err := getFilePath(filepath.Dir(goFile))
if err != nil { if err != nil {
return err return err
@@ -150,14 +153,16 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
_, offset := time.Now().Zone() _, offset := time.Now().Zone()
t := template.Must(template.New("dockerfile").Parse(text)) t := template.Must(template.New("dockerfile").Parse(text))
return t.Execute(out, Docker{ return t.Execute(out, Docker{
Chinese: offset == cstOffset, Chinese: offset == cstOffset,
GoRelPath: projPath, GoRelPath: projPath,
GoFile: goFile, GoFile: goFile,
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
HasPort: port > 0, HasPort: port > 0,
Port: port, Port: port,
Argument: builder.String(), Argument: builder.String(),
Version: version, Version: version,
HasTimezone: len(timezone) > 0,
Timezone: timezone,
}) })
} }

View File

@@ -15,6 +15,8 @@ LABEL stage=gobuilder
ENV CGO_ENABLED 0 ENV CGO_ENABLED 0
ENV GOOS linux ENV GOOS linux
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct {{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
{{end}}{{if .HasTimezone}}
RUN apk update --no-cache && apk add --no-cache tzdata
{{end}} {{end}}
WORKDIR /build WORKDIR /build
@@ -28,9 +30,10 @@ COPY . .
FROM alpine FROM alpine
RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata RUN apk update --no-cache && apk add --no-cache ca-certificates
ENV TZ Asia/Shanghai {{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}}
ENV TZ {{.Timezone}}
{{end}}
WORKDIR /app WORKDIR /app
COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}} COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}{{if .Argument}}
COPY --from=builder /app/etc /app/etc{{end}} COPY --from=builder /app/etc /app/etc{{end}}

View File

@@ -365,6 +365,11 @@ var commands = []cli.Command{
Name: "version", Name: "version",
Usage: "the goctl builder golang image version", Usage: "the goctl builder golang image version",
}, },
cli.StringFlag{
Name: "tz",
Usage: "the timezone of the container",
Value: "Asia/Shanghai",
},
}, },
Action: docker.DockerCommand, Action: docker.DockerCommand,
}, },