Skip to content

Commit

Permalink
Merge pull request #13314 from flouthoc/container-commit-squash
Browse files Browse the repository at this point in the history
container-commit: support `--squash` to squash layers into one if users want.
  • Loading branch information
openshift-merge-robot authored Feb 23, 2022
2 parents 113f855 + fbbcb95 commit 991c90d
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/podman/containers/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func commitFlags(cmd *cobra.Command) {

flags.BoolVarP(&commitOptions.Pause, "pause", "p", false, "Pause container during commit")
flags.BoolVarP(&commitOptions.Quiet, "quiet", "q", false, "Suppress output")
flags.BoolVarP(&commitOptions.Squash, "squash", "s", false, "squash newly built layers into a single new layer")
flags.BoolVar(&commitOptions.IncludeVolumes, "include-volumes", false, "Include container volumes as image volumes")
}

Expand Down
5 changes: 5 additions & 0 deletions docs/source/markdown/podman-commit.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Set commit message for committed image.\
Pause the container when creating an image.\
The default is **false**.

#### **--squash**, **-s**

Squash newly built layers into a single new layer.\
The default is **false**.

#### **--quiet**, **-q**

Suppresses output.\
Expand Down
2 changes: 2 additions & 0 deletions libpod/container_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ContainerCommitOptions struct {
Author string
Message string
Changes []string
Squash bool
}

// Commit commits the changes between a container and its image, creating a new
Expand Down Expand Up @@ -63,6 +64,7 @@ func (c *Container) Commit(ctx context.Context, destImage string, options Contai
commitOptions := buildah.CommitOptions{
SignaturePolicyPath: options.SignaturePolicyPath,
ReportWriter: options.ReportWriter,
Squash: options.Squash,
SystemContext: c.runtime.imageContext,
PreferredManifestType: options.PreferredManifestType,
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/compat/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) {
Comment string `schema:"comment"`
Container string `schema:"container"`
Pause bool `schema:"pause"`
Squash bool `schema:"squash"`
Repo string `schema:"repo"`
Tag string `schema:"tag"`
// fromSrc string # fromSrc is currently unused
Expand Down Expand Up @@ -138,6 +139,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) {
options.Message = query.Comment
options.Author = query.Author
options.Pause = query.Pause
options.Squash = query.Squash
for _, change := range query.Changes {
options.Changes = append(options.Changes, strings.Split(change, "\n")...)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/libpod/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) {
Container string `schema:"container"`
Format string `schema:"format"`
Pause bool `schema:"pause"`
Squash bool `schema:"squash"`
Repo string `schema:"repo"`
Tag string `schema:"tag"`
}{
Expand Down Expand Up @@ -543,6 +544,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) {
options.Message = query.Comment
options.Author = query.Author
options.Pause = query.Pause
options.Squash = query.Squash
options.Changes = query.Changes
ctr, err := runtime.LookupContainer(query.Container)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/server/register_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ func (s *APIServer) registerImagesHandlers(r *mux.Router) error {
// name: changes
// type: string
// description: instructions to apply while committing in Dockerfile format
// - in: query
// name: squash
// type: boolean
// description: squash newly built layers into a single new layer
// produces:
// - application/json
// responses:
Expand Down
1 change: 1 addition & 0 deletions pkg/bindings/containers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type CommitOptions struct {
Comment *string
Format *string
Pause *bool
Squash *bool
Repo *string
Tag *string
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/bindings/containers/types_commit_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/domain/entities/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type CommitOptions struct {
Message string
Pause bool
Quiet bool
Squash bool
Writer io.Writer
}

Expand Down
1 change: 1 addition & 0 deletions pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func (ic *ContainerEngine) ContainerCommit(ctx context.Context, nameOrID string,
Message: options.Message,
Changes: options.Changes,
Author: options.Author,
Squash: options.Squash,
}
newImage, err := ctr.Commit(ctx, options.ImageName, opts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/tunnel/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (ic *ContainerEngine) ContainerCommit(ctx context.Context, nameOrID string,
return nil, errors.Errorf("invalid image name %q", opts.ImageName)
}
}
options := new(containers.CommitOptions).WithAuthor(opts.Author).WithChanges(opts.Changes).WithComment(opts.Message)
options := new(containers.CommitOptions).WithAuthor(opts.Author).WithChanges(opts.Changes).WithComment(opts.Message).WithSquash(opts.Squash)
options.WithFormat(opts.Format).WithPause(opts.Pause).WithRepo(repo).WithTag(tag)
response, err := containers.Commit(ic.ClientCtx, nameOrID, options)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

. "github.com/containers/podman/v4/test/utils"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -133,6 +134,23 @@ var _ = Describe("Podman commit", func() {
Expect(foundBlue).To(Equal(true))
})

It("podman commit container with --squash", func() {
test := podmanTest.Podman([]string{"run", "--name", "test1", "-d", ALPINE, "ls"})
test.WaitWithDefaultTimeout()
Expect(test).Should(Exit(0))
Expect(podmanTest.NumberOfContainers()).To(Equal(1))

session := podmanTest.Podman([]string{"commit", "--squash", "test1", "foobar.com/test1-image:latest"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"inspect", "--format", "{{.RootFS.Layers}}", "foobar.com/test1-image:latest"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// Check for one layers
Expect(strings.Fields(session.OutputToString())).To(HaveLen(1))
})

It("podman commit container with change flag and JSON entrypoint with =", func() {
test := podmanTest.Podman([]string{"run", "--name", "test1", "-d", ALPINE, "ls"})
test.WaitWithDefaultTimeout()
Expand Down

0 comments on commit 991c90d

Please sign in to comment.