chore: reduce the docker image size (#1633)

* chore: reduce the docker image size

* chore: format dockerfile
master
Kevin Wan 3 years ago committed by GitHub
parent 4624390e54
commit d6a692971f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,6 +32,8 @@ type Docker struct {
Port int
Argument string
Version string
HasTimezone bool
Timezone string
}
// DockerCommand provides the entry for goctl docker
@ -47,6 +49,7 @@ func DockerCommand(c *cli.Context) (err error) {
version := c.String("version")
remote := c.String("remote")
branch := c.String("branch")
timezone := c.String("tz")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@ -72,7 +75,7 @@ func DockerCommand(c *cli.Context) (err error) {
port := c.Int("port")
if _, err := os.Stat(etcDir); os.IsNotExist(err) {
return generateDockerfile(goFile, port, version)
return generateDockerfile(goFile, port, version, timezone)
}
cfg, err := findConfig(goFile, etcDir)
@ -80,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) {
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
}
@ -121,7 +124,7 @@ func findConfig(file, dir string) (string, error) {
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))
if err != nil {
return err
@ -158,6 +161,8 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
Port: port,
Argument: builder.String(),
Version: version,
HasTimezone: len(timezone) > 0,
Timezone: timezone,
})
}

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

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

Loading…
Cancel
Save