commit.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "golang.org/x/net/context"
  6. Cli "github.com/docker/docker/cli"
  7. "github.com/docker/docker/opts"
  8. flag "github.com/docker/docker/pkg/mflag"
  9. "github.com/docker/engine-api/types"
  10. "github.com/docker/engine-api/types/container"
  11. )
  12. // CmdCommit creates a new image from a container's changes.
  13. //
  14. // Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  15. func (cli *DockerCli) CmdCommit(args ...string) error {
  16. cmd := Cli.Subcmd("commit", []string{"CONTAINER [REPOSITORY[:TAG]]"}, Cli.DockerCommands["commit"].Description, true)
  17. flPause := cmd.Bool([]string{"p", "-pause"}, true, "Pause container during commit")
  18. flComment := cmd.String([]string{"m", "-message"}, "", "Commit message")
  19. flAuthor := cmd.String([]string{"a", "-author"}, "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")")
  20. flChanges := opts.NewListOpts(nil)
  21. cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
  22. // FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands.
  23. flConfig := cmd.String([]string{"#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands")
  24. cmd.Require(flag.Max, 2)
  25. cmd.Require(flag.Min, 1)
  26. cmd.ParseFlags(args, true)
  27. var (
  28. name = cmd.Arg(0)
  29. reference = cmd.Arg(1)
  30. )
  31. var config *container.Config
  32. if *flConfig != "" {
  33. config = &container.Config{}
  34. if err := json.Unmarshal([]byte(*flConfig), config); err != nil {
  35. return err
  36. }
  37. }
  38. options := types.ContainerCommitOptions{
  39. Reference: reference,
  40. Comment: *flComment,
  41. Author: *flAuthor,
  42. Changes: flChanges.GetAll(),
  43. Pause: *flPause,
  44. Config: config,
  45. }
  46. response, err := cli.client.ContainerCommit(context.Background(), name, options)
  47. if err != nil {
  48. return err
  49. }
  50. fmt.Fprintln(cli.out, response.ID)
  51. return nil
  52. }