github.com/boynux/docker@v1.11.0-rc4/api/client/commit.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 8 "golang.org/x/net/context" 9 10 Cli "github.com/docker/docker/cli" 11 "github.com/docker/docker/opts" 12 flag "github.com/docker/docker/pkg/mflag" 13 "github.com/docker/docker/reference" 14 "github.com/docker/engine-api/types" 15 "github.com/docker/engine-api/types/container" 16 ) 17 18 // CmdCommit creates a new image from a container's changes. 19 // 20 // Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] 21 func (cli *DockerCli) CmdCommit(args ...string) error { 22 cmd := Cli.Subcmd("commit", []string{"CONTAINER [REPOSITORY[:TAG]]"}, Cli.DockerCommands["commit"].Description, true) 23 flPause := cmd.Bool([]string{"p", "-pause"}, true, "Pause container during commit") 24 flComment := cmd.String([]string{"m", "-message"}, "", "Commit message") 25 flAuthor := cmd.String([]string{"a", "-author"}, "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")") 26 flChanges := opts.NewListOpts(nil) 27 cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image") 28 // FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands. 29 flConfig := cmd.String([]string{"#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands") 30 cmd.Require(flag.Max, 2) 31 cmd.Require(flag.Min, 1) 32 33 cmd.ParseFlags(args, true) 34 35 var ( 36 name = cmd.Arg(0) 37 repositoryAndTag = cmd.Arg(1) 38 repositoryName string 39 tag string 40 ) 41 42 //Check if the given image name can be resolved 43 if repositoryAndTag != "" { 44 ref, err := reference.ParseNamed(repositoryAndTag) 45 if err != nil { 46 return err 47 } 48 49 repositoryName = ref.Name() 50 51 switch x := ref.(type) { 52 case reference.Canonical: 53 return errors.New("cannot commit to digest reference") 54 case reference.NamedTagged: 55 tag = x.Tag() 56 } 57 } 58 59 var config *container.Config 60 if *flConfig != "" { 61 config = &container.Config{} 62 if err := json.Unmarshal([]byte(*flConfig), config); err != nil { 63 return err 64 } 65 } 66 67 options := types.ContainerCommitOptions{ 68 ContainerID: name, 69 RepositoryName: repositoryName, 70 Tag: tag, 71 Comment: *flComment, 72 Author: *flAuthor, 73 Changes: flChanges.GetAll(), 74 Pause: *flPause, 75 Config: config, 76 } 77 78 response, err := cli.client.ContainerCommit(context.Background(), options) 79 if err != nil { 80 return err 81 } 82 83 fmt.Fprintln(cli.out, response.ID) 84 return nil 85 }