github.com/kunnos/engine@v1.13.1/cli/command/container/commit.go (about) 1 package container 2 3 import ( 4 "fmt" 5 6 "golang.org/x/net/context" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/cli" 10 "github.com/docker/docker/cli/command" 11 dockeropts "github.com/docker/docker/opts" 12 "github.com/spf13/cobra" 13 ) 14 15 type commitOptions struct { 16 container string 17 reference string 18 19 pause bool 20 comment string 21 author string 22 changes dockeropts.ListOpts 23 } 24 25 // NewCommitCommand creates a new cobra.Command for `docker commit` 26 func NewCommitCommand(dockerCli *command.DockerCli) *cobra.Command { 27 var opts commitOptions 28 29 cmd := &cobra.Command{ 30 Use: "commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]", 31 Short: "Create a new image from a container's changes", 32 Args: cli.RequiresRangeArgs(1, 2), 33 RunE: func(cmd *cobra.Command, args []string) error { 34 opts.container = args[0] 35 if len(args) > 1 { 36 opts.reference = args[1] 37 } 38 return runCommit(dockerCli, &opts) 39 }, 40 } 41 42 flags := cmd.Flags() 43 flags.SetInterspersed(false) 44 45 flags.BoolVarP(&opts.pause, "pause", "p", true, "Pause container during commit") 46 flags.StringVarP(&opts.comment, "message", "m", "", "Commit message") 47 flags.StringVarP(&opts.author, "author", "a", "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")") 48 49 opts.changes = dockeropts.NewListOpts(nil) 50 flags.VarP(&opts.changes, "change", "c", "Apply Dockerfile instruction to the created image") 51 52 return cmd 53 } 54 55 func runCommit(dockerCli *command.DockerCli, opts *commitOptions) error { 56 ctx := context.Background() 57 58 name := opts.container 59 reference := opts.reference 60 61 options := types.ContainerCommitOptions{ 62 Reference: reference, 63 Comment: opts.comment, 64 Author: opts.author, 65 Changes: opts.changes.GetAll(), 66 Pause: opts.pause, 67 } 68 69 response, err := dockerCli.Client().ContainerCommit(ctx, name, options) 70 if err != nil { 71 return err 72 } 73 74 fmt.Fprintln(dockerCli.Out(), response.ID) 75 return nil 76 }