github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/api/client/commit.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/url" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/opts" 10 flag "github.com/docker/docker/pkg/mflag" 11 "github.com/docker/docker/pkg/parsers" 12 "github.com/docker/docker/registry" 13 "github.com/docker/docker/runconfig" 14 ) 15 16 // CmdCommit creates a new image from a container's changes. 17 // 18 // Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] 19 func (cli *DockerCli) CmdCommit(args ...string) error { 20 cmd := cli.Subcmd("commit", []string{"CONTAINER [REPOSITORY[:TAG]]"}, "Create a new image from a container's changes", true) 21 flPause := cmd.Bool([]string{"p", "-pause"}, true, "Pause container during commit") 22 flComment := cmd.String([]string{"m", "-message"}, "", "Commit message") 23 flAuthor := cmd.String([]string{"a", "#author", "-author"}, "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")") 24 flChanges := opts.NewListOpts(nil) 25 cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image") 26 // FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands. 27 flConfig := cmd.String([]string{"#run", "#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands") 28 cmd.Require(flag.Max, 2) 29 cmd.Require(flag.Min, 1) 30 cmd.ParseFlags(args, true) 31 32 var ( 33 name = cmd.Arg(0) 34 repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1)) 35 ) 36 37 //Check if the given image name can be resolved 38 if repository != "" { 39 if err := registry.ValidateRepositoryName(repository); err != nil { 40 return err 41 } 42 } 43 44 v := url.Values{} 45 v.Set("container", name) 46 v.Set("repo", repository) 47 v.Set("tag", tag) 48 v.Set("comment", *flComment) 49 v.Set("author", *flAuthor) 50 for _, change := range flChanges.GetAll() { 51 v.Add("changes", change) 52 } 53 54 if *flPause != true { 55 v.Set("pause", "0") 56 } 57 58 var ( 59 config *runconfig.Config 60 response types.ContainerCommitResponse 61 ) 62 63 if *flConfig != "" { 64 config = &runconfig.Config{} 65 if err := json.Unmarshal([]byte(*flConfig), config); err != nil { 66 return err 67 } 68 } 69 stream, _, _, err := cli.call("POST", "/commit?"+v.Encode(), config, nil) 70 if err != nil { 71 return err 72 } 73 74 if err := json.NewDecoder(stream).Decode(&response); err != nil { 75 return err 76 } 77 78 fmt.Fprintln(cli.out, response.ID) 79 return nil 80 }