pause.go 1.4 KB

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