create.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // +build experimental
  2. package checkpoint
  3. import (
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/spf13/cobra"
  9. )
  10. type createOptions struct {
  11. container string
  12. checkpoint string
  13. leaveRunning bool
  14. }
  15. func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
  16. var opts createOptions
  17. cmd := &cobra.Command{
  18. Use: "create CONTAINER CHECKPOINT",
  19. Short: "Create a checkpoint from a running container",
  20. Args: cli.ExactArgs(2),
  21. RunE: func(cmd *cobra.Command, args []string) error {
  22. opts.container = args[0]
  23. opts.checkpoint = args[1]
  24. return runCreate(dockerCli, opts)
  25. },
  26. }
  27. flags := cmd.Flags()
  28. flags.BoolVar(&opts.leaveRunning, "leave-running", false, "leave the container running after checkpoing")
  29. return cmd
  30. }
  31. func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
  32. client := dockerCli.Client()
  33. checkpointOpts := types.CheckpointCreateOptions{
  34. CheckpointID: opts.checkpoint,
  35. Exit: !opts.leaveRunning,
  36. }
  37. err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts)
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }