pause.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package daemon
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/docker/container"
  6. "github.com/sirupsen/logrus"
  7. )
  8. // ContainerPause pauses a container
  9. func (daemon *Daemon) ContainerPause(name string) error {
  10. container, err := daemon.GetContainer(name)
  11. if err != nil {
  12. return err
  13. }
  14. return daemon.containerPause(container)
  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. if !container.Running {
  23. return errNotRunning(container.ID)
  24. }
  25. // We cannot Pause the container which is already paused
  26. if container.Paused {
  27. return errNotPaused(container.ID)
  28. }
  29. // We cannot Pause the container which is restarting
  30. if container.Restarting {
  31. return errContainerIsRestarting(container.ID)
  32. }
  33. if err := daemon.containerd.Pause(context.Background(), container.ID); err != nil {
  34. return fmt.Errorf("Cannot pause container %s: %s", container.ID, err)
  35. }
  36. container.Paused = true
  37. daemon.setStateCounter(container)
  38. daemon.updateHealthMonitor(container)
  39. daemon.LogContainerEvent(container, "pause")
  40. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  41. logrus.WithError(err).Warn("could not save container to disk")
  42. }
  43. return nil
  44. }