Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reduce the docker image size #1633

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: reduce the docker image size
  • Loading branch information
kevwan committed Mar 11, 2022
commit 67c2923842f4c422b7877b1fa5db38f5b8f7e797
43 changes: 24 additions & 19 deletions tools/goctl/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ const (

// Docker describes a dockerfile
type Docker struct {
Chinese bool
GoRelPath string
GoFile string
ExeFile string
HasPort bool
Port int
Argument string
Version string
Chinese bool
GoRelPath string
GoFile string
ExeFile string
HasPort bool
Port int
Argument string
Version string
HasTimezone bool
Timezone string
}

// DockerCommand provides the entry for goctl docker
Expand All @@ -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 {
Expand All @@ -72,15 +75,15 @@ 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)
if err != nil {
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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -150,14 +153,16 @@ func generateDockerfile(goFile string, port int, version string, args ...string)
_, offset := time.Now().Zone()
t := template.Must(template.New("dockerfile").Parse(text))
return t.Execute(out, Docker{
Chinese: offset == cstOffset,
GoRelPath: projPath,
GoFile: goFile,
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
HasPort: port > 0,
Port: port,
Argument: builder.String(),
Version: version,
Chinese: offset == cstOffset,
GoRelPath: projPath,
GoFile: goFile,
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
HasPort: port > 0,
Port: port,
Argument: builder.String(),
Version: version,
HasTimezone: len(timezone) > 0,
Timezone: timezone,
})
}

Expand Down
11 changes: 7 additions & 4 deletions tools/goctl/docker/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOOS linux
{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct
{{end}}
{{end}}{{if .HasTimezone}}
RUN apk update --no-cache && apk add --no-cache tzdata{{end}}

WORKDIR /build

ADD go.mod .
Expand All @@ -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}}
Expand Down
5 changes: 5 additions & 0 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down