Description: delete original image when squash 
 docker-ce (5:20.10.3-17) buster; urgency=low
   * Version: 20.10.3-17
Author: wanghuaiqing <wanghuaiqing@loongson.com>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: 2022-06-22

--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/cli/cli/command/image/build.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/cli/cli/command/image/build.go
@@ -66,6 +66,7 @@ type buildOptions struct {
 	securityOpt    []string
 	networkMode    string
 	squash         bool
+	squashdelete   bool
 	target         string
 	imageIDFile    string
 	stream         bool
@@ -165,6 +166,7 @@ func NewBuildCommand(dockerCli command.C
 	flags.SetAnnotation("platform", "buildkit", nil)
 
 	flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
+	flags.BoolVar(&options.squashdelete, "squashdelete", false, "Squashdelete delete original image that generate squash image when squash enable")
 	flags.SetAnnotation("squash", "experimental", nil)
 	flags.SetAnnotation("squash", "version", []string{"1.25"})
 
@@ -604,6 +606,7 @@ func imageBuildOptions(dockerCli command
 		SecurityOpt:    options.securityOpt,
 		NetworkMode:    options.networkMode,
 		Squash:         options.squash,
+		SquashDelete:   options.squashdelete,
 		ExtraHosts:     options.extraHosts.GetAll(),
 		Target:         options.target,
 		Platform:       options.platform,
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/cli/cli/command/image/cmd.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/cli/cli/command/image/cmd.go
@@ -30,8 +30,6 @@ func NewImageCommand(dockerCli command.C
 		newRemoveCommand(dockerCli),
 		newInspectCommand(dockerCli),
 		NewPruneCommand(dockerCli),
-		NewGenCommand(dockerCli),
-		NewAppComposeCommand(dockerCli),
 	)
 	return cmd
 }
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/cli/vendor/github.com/docker/docker/api/types/client.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/cli/vendor/github.com/docker/docker/api/types/client.go
@@ -173,6 +173,7 @@ type ImageBuildOptions struct {
 	// preserves the original image and creates a new one from the parent with all
 	// the changes applied to a single layer
 	Squash bool
+	SquashDelete bool
 	// CacheFrom specifies images that are used for matching cache. Images
 	// specified here do not need to have a valid parent chain to match cache.
 	CacheFrom   []string
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/cli/vendor/github.com/docker/docker/client/image_build.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/cli/vendor/github.com/docker/docker/client/image_build.go
@@ -79,6 +79,9 @@ func (cli *Client) imageBuildOptionsToQu
 			return query, err
 		}
 		query.Set("squash", "1")
+		if options.SquashDelete {
+			query.Set("squashdelete", "1")
+		}
 	}
 
 	if !container.Isolation.IsDefault(options.Isolation) {
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/engine/api/server/backend/build/backend.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/engine/api/server/backend/build/backend.go
@@ -22,6 +22,7 @@ import (
 type ImageComponent interface {
 	SquashImage(from string, to string) (string, error)
 	TagImageWithReference(image.ID, reference.Named) error
+	ImageDelete(imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error)
 }
 
 // Builder defines interface for running a build
@@ -77,10 +78,17 @@ func (b *Backend) Build(ctx context.Cont
 	}
 
 	var imageID = build.ImageID
+	stdout := config.ProgressWriter.StdoutFormatter
 	if options.Squash {
 		if imageID, err = squashBuild(build, b.imageComponent); err != nil {
 			return "", err
 		}
+		if options.SquashDelete {
+			if err = squashRmBuild(build, b.imageComponent); err != nil {
+				return "", err
+			}
+			fmt.Fprintf(stdout, "delete original image:%s\n", build.ImageID)
+		}
 		if config.ProgressWriter.AuxFormatter != nil {
 			if err = config.ProgressWriter.AuxFormatter.Emit("moby.image.id", types.BuildResult{ID: imageID}); err != nil {
 				return "", err
@@ -89,7 +97,6 @@ func (b *Backend) Build(ctx context.Cont
 	}
 
 	if !useBuildKit {
-		stdout := config.ProgressWriter.StdoutFormatter
 		fmt.Fprintf(stdout, "Successfully built %s\n", stringid.TruncateID(imageID))
 	}
 	if imageID != "" {
@@ -128,3 +135,7 @@ func squashBuild(build *builder.Result,
 	}
 	return imageID, nil
 }
+func squashRmBuild(build *builder.Result, imageComponent ImageComponent) error {
+	_, err := imageComponent.ImageDelete(build.ImageID, true, true)
+	return err
+}
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/engine/api/server/router/build/build_routes.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/engine/api/server/router/build/build_routes.go
@@ -57,6 +57,7 @@ func newImageBuildOptions(ctx context.Co
 		ExtraHosts:     r.Form["extrahosts"],
 		SecurityOpt:    r.Form["securityopt"],
 		Squash:         httputils.BoolValue(r, "squash"),
+		SquashDelete:   httputils.BoolValue(r, "squashdelete"),
 		Target:         r.FormValue("target"),
 		RemoteContext:  r.FormValue("remote"),
 		SessionID:      r.FormValue("session"),
--- lcf-utils-1.0.1/src/loongker-ce-20.10.3.orig/sources/engine/api/types/client.go
+++ lcf-utils-1.0.1/src/loongker-ce-20.10.3/sources/engine/api/types/client.go
@@ -173,6 +173,8 @@ type ImageBuildOptions struct {
 	// preserves the original image and creates a new one from the parent with all
 	// the changes applied to a single layer
 	Squash bool
+	// SquashDelete describe whether delete original image that generate squash image
+	SquashDelete bool
 	// CacheFrom specifies images that are used for matching cache. Images
 	// specified here do not need to have a valid parent chain to match cache.
 	CacheFrom   []string
