commit.go 2.3 KB

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