pause.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package daemon
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/container"
  5. )
  6. // ContainerPause pauses a container
  7. func (daemon *Daemon) ContainerPause(name string) error {
  8. container, err := daemon.GetContainer(name)
  9. if err != nil {
  10. return err
  11. }
  12. if err := daemon.containerPause(container); err != nil {
  13. return err
  14. }
  15. return nil
  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. if !container.Running {
  24. return errNotRunning{container.ID}
  25. }
  26. // We cannot Pause the container which is already paused
  27. if container.Paused {
  28. return fmt.Errorf("Container %s is already paused", 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 := daemon.containerd.Pause(container.ID); err != nil {
  35. return fmt.Errorf("Cannot pause container %s: %s", container.ID, err)
  36. }
  37. return nil
  38. }