create.go 1.2 KB

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