pause.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/containerd/log"
  6. "github.com/docker/docker/api/types/events"
  7. "github.com/docker/docker/container"
  8. )
  9. // ContainerPause pauses a container
  10. func (daemon *Daemon) ContainerPause(name string) error {
  11. ctr, err := daemon.GetContainer(name)
  12. if err != nil {
  13. return err
  14. }
  15. return daemon.containerPause(ctr)
  16. }
  17. // containerPause pauses the container execution without stopping the process.
  18. // The execution can be resumed by calling containerUnpause.
  19. func (daemon *Daemon) containerPause(container *container.Container) error {
  20. container.Lock()
  21. defer container.Unlock()
  22. // We cannot Pause the container which is not running
  23. tsk, err := container.GetRunningTask()
  24. if err != nil {
  25. return err
  26. }
  27. // We cannot Pause the container which is already paused
  28. if container.Paused {
  29. return errNotPaused(container.ID)
  30. }
  31. // We cannot Pause the container which is restarting
  32. if container.Restarting {
  33. return errContainerIsRestarting(container.ID)
  34. }
  35. if err := tsk.Pause(context.Background()); err != nil {
  36. return fmt.Errorf("cannot pause container %s: %s", container.ID, err)
  37. }
  38. container.Paused = true
  39. daemon.setStateCounter(container)
  40. daemon.updateHealthMonitor(container)
  41. daemon.LogContainerEvent(container, events.ActionPause)
  42. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  43. log.G(context.TODO()).WithError(err).Warn("could not save container to disk")
  44. }
  45. return nil
  46. }