github.com/portworx/docker@v1.12.1/api/client/container/commit.go (about) 1 package container 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/client" 10 "github.com/docker/docker/cli" 11 dockeropts "github.com/docker/docker/opts" 12 "github.com/docker/engine-api/types" 13 containertypes "github.com/docker/engine-api/types/container" 14 "github.com/spf13/cobra" 15 ) 16 17 type commitOptions struct { 18 container string 19 reference string 20 21 pause bool 22 comment string 23 author string 24 changes dockeropts.ListOpts 25 config string 26 } 27 28 // NewCommitCommand creats a new cobra.Command for `docker commit` 29 func NewCommitCommand(dockerCli *client.DockerCli) *cobra.Command { 30 var opts commitOptions 31 32 cmd := &cobra.Command{ 33 Use: "commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]", 34 Short: "Create a new image from a container's changes", 35 Args: cli.RequiresRangeArgs(1, 2), 36 RunE: func(cmd *cobra.Command, args []string) error { 37 opts.container = args[0] 38 if len(args) > 1 { 39 opts.reference = args[1] 40 } 41 return runCommit(dockerCli, &opts) 42 }, 43 } 44 cmd.SetFlagErrorFunc(flagErrorFunc) 45 46 flags := cmd.Flags() 47 flags.SetInterspersed(false) 48 49 flags.BoolVarP(&opts.pause, "pause", "p", true, "Pause container during commit") 50 flags.StringVarP(&opts.comment, "message", "m", "", "Commit message") 51 flags.StringVarP(&opts.author, "author", "a", "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")") 52 53 opts.changes = dockeropts.NewListOpts(nil) 54 flags.VarP(&opts.changes, "change", "c", "Apply Dockerfile instruction to the created image") 55 56 // FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands. 57 flags.StringVar(&opts.config, "run", "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands") 58 flags.MarkDeprecated("run", "it will be replaced with inline Dockerfile commands.") 59 60 return cmd 61 } 62 63 func runCommit(dockerCli *client.DockerCli, opts *commitOptions) error { 64 ctx := context.Background() 65 66 name := opts.container 67 reference := opts.reference 68 69 var config *containertypes.Config 70 if opts.config != "" { 71 config = &containertypes.Config{} 72 if err := json.Unmarshal([]byte(opts.config), config); err != nil { 73 return err 74 } 75 } 76 77 options := types.ContainerCommitOptions{ 78 Reference: reference, 79 Comment: opts.comment, 80 Author: opts.author, 81 Changes: opts.changes.GetAll(), 82 Pause: opts.pause, 83 Config: config, 84 } 85 86 response, err := dockerCli.Client().ContainerCommit(ctx, name, options) 87 if err != nil { 88 return err 89 } 90 91 fmt.Fprintln(dockerCli.Out(), response.ID) 92 return nil 93 }