commit.go 2.2 KB

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