github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/commit.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 8 Cli "github.com/docker/docker/cli" 9 "github.com/docker/docker/opts" 10 flag "github.com/docker/docker/pkg/mflag" 11 "github.com/docker/docker/reference" 12 "github.com/docker/engine-api/types" 13 "github.com/docker/engine-api/types/container" 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]]"}, Cli.DockerCommands["commit"].Description, 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 (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"}, "", "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 31 cmd.ParseFlags(args, true) 32 33 var ( 34 name = cmd.Arg(0) 35 repositoryAndTag = cmd.Arg(1) 36 repositoryName string 37 tag string 38 ) 39 40 //Check if the given image name can be resolved 41 if repositoryAndTag != "" { 42 ref, err := reference.ParseNamed(repositoryAndTag) 43 if err != nil { 44 return err 45 } 46 47 repositoryName = ref.Name() 48 49 switch x := ref.(type) { 50 case reference.Canonical: 51 return errors.New("cannot commit to digest reference") 52 case reference.NamedTagged: 53 tag = x.Tag() 54 } 55 } 56 57 var config *container.Config 58 if *flConfig != "" { 59 config = &container.Config{} 60 if err := json.Unmarshal([]byte(*flConfig), config); err != nil { 61 return err 62 } 63 } 64 65 options := types.ContainerCommitOptions{ 66 ContainerID: name, 67 RepositoryName: repositoryName, 68 Tag: tag, 69 Comment: *flComment, 70 Author: *flAuthor, 71 Changes: flChanges.GetAll(), 72 Pause: *flPause, 73 Config: config, 74 } 75 76 response, err := cli.client.ContainerCommit(options) 77 if err != nil { 78 return err 79 } 80 81 fmt.Fprintln(cli.out, response.ID) 82 return nil 83 }