feat: support scratch as the base docker image (#1634)

master
Kevin Wan 3 years ago committed by GitHub
parent d6a692971f
commit 26a33932cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,6 +28,7 @@ type Docker struct {
GoRelPath string GoRelPath string
GoFile string GoFile string
ExeFile string ExeFile string
Scratch bool
HasPort bool HasPort bool
Port int Port int
Argument string Argument string
@ -73,9 +74,10 @@ func DockerCommand(c *cli.Context) (err error) {
return fmt.Errorf("file %q not found", goFile) return fmt.Errorf("file %q not found", goFile)
} }
scratch := c.Bool("scratch")
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, timezone) return generateDockerfile(goFile, scratch, port, version, timezone)
} }
cfg, err := findConfig(goFile, etcDir) cfg, err := findConfig(goFile, etcDir)
@ -83,7 +85,7 @@ func DockerCommand(c *cli.Context) (err error) {
return err return err
} }
if err := generateDockerfile(goFile, port, version, timezone, "-f", "etc/"+cfg); err != nil { if err := generateDockerfile(goFile, scratch, port, version, timezone, "-f", "etc/"+cfg); err != nil {
return err return err
} }
@ -124,7 +126,7 @@ func findConfig(file, dir string) (string, error) {
return files[0], nil return files[0], nil
} }
func generateDockerfile(goFile string, port int, version, timezone string, args ...string) error { func generateDockerfile(goFile string, scratch bool, 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
@ -157,6 +159,7 @@ func generateDockerfile(goFile string, port int, version, timezone string, args
GoRelPath: projPath, GoRelPath: projPath,
GoFile: goFile, GoFile: goFile,
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
Scratch: scratch,
HasPort: port > 0, HasPort: port > 0,
Port: port, Port: port,
Argument: builder.String(), Argument: builder.String(),

@ -28,9 +28,9 @@ COPY . .
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}} {{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
FROM alpine FROM {{if .Scratch}}scratch{{else}}alpine{{end}}
RUN apk update --no-cache && apk add --no-cache ca-certificates {{if .Scratch}}COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt{{else}}RUN apk update --no-cache && apk add --no-cache ca-certificates{{end}}
{{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}} {{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}}
ENV TZ {{.Timezone}} ENV TZ {{.Timezone}}
{{end}} {{end}}

@ -341,6 +341,10 @@ var commands = []cli.Command{
Name: "go", Name: "go",
Usage: "the file that contains main function", Usage: "the file that contains main function",
}, },
cli.BoolFlag{
Name: "scratch",
Usage: "use scratch for the base docker image",
},
cli.IntFlag{ cli.IntFlag{
Name: "port", Name: "port",
Usage: "the port to expose, default none", Usage: "the port to expose, default none",

Loading…
Cancel
Save