feat: support -base to specify base image for goctl docker (#1668)

* feat: support -base to specify base image for goctl docker

* chore: update usage
master
Kevin Wan 3 years ago committed by GitHub
parent 194f55e08e
commit d29dfa12e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ type Docker struct {
GoRelPath string
GoFile string
ExeFile string
Scratch bool
BaseImage string
HasPort bool
Port int
Argument string
@ -74,10 +74,10 @@ func DockerCommand(c *cli.Context) (err error) {
return fmt.Errorf("file %q not found", goFile)
}
scratch := c.Bool("scratch")
base := c.String("base")
port := c.Int("port")
if _, err := os.Stat(etcDir); os.IsNotExist(err) {
return generateDockerfile(goFile, scratch, port, version, timezone)
return generateDockerfile(goFile, base, port, version, timezone)
}
cfg, err := findConfig(goFile, etcDir)
@ -85,7 +85,7 @@ func DockerCommand(c *cli.Context) (err error) {
return err
}
if err := generateDockerfile(goFile, scratch, port, version, timezone, "-f", "etc/"+cfg); err != nil {
if err := generateDockerfile(goFile, base, port, version, timezone, "-f", "etc/"+cfg); err != nil {
return err
}
@ -126,7 +126,7 @@ func findConfig(file, dir string) (string, error) {
return files[0], nil
}
func generateDockerfile(goFile string, scratch bool, port int, version, timezone string, args ...string) error {
func generateDockerfile(goFile, base string, port int, version, timezone string, args ...string) error {
projPath, err := getFilePath(filepath.Dir(goFile))
if err != nil {
return err
@ -159,7 +159,7 @@ func generateDockerfile(goFile string, scratch bool, port int, version, timezone
GoRelPath: projPath,
GoFile: goFile,
ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)),
Scratch: scratch,
BaseImage: base,
HasPort: port > 0,
Port: port,
Argument: builder.String(),

@ -27,9 +27,9 @@ COPY . .
{{end}}RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
FROM {{if .Scratch}}scratch{{else}}alpine{{end}}
FROM {{.BaseImage}}
{{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}}
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
{{if .HasTimezone}}COPY --from=builder /usr/share/zoneinfo/{{.Timezone}} /usr/share/zoneinfo/{{.Timezone}}
ENV TZ {{.Timezone}}
{{end}}

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

Loading…
Cancel
Save