restart.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package daemon
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/container"
  5. )
  6. // ContainerRestart stops and starts a container. It attempts to
  7. // gracefully stop the container within the given timeout, forcefully
  8. // stopping it if the timeout is exceeded. If given a negative
  9. // timeout, ContainerRestart will wait forever until a graceful
  10. // stop. Returns an error if the container cannot be found, or if
  11. // there is an underlying error at any stage of the restart.
  12. func (daemon *Daemon) ContainerRestart(name string, seconds int) error {
  13. container, err := daemon.GetContainer(name)
  14. if err != nil {
  15. return err
  16. }
  17. if err := daemon.containerRestart(container, seconds); err != nil {
  18. return fmt.Errorf("Cannot restart container %s: %v", name, err)
  19. }
  20. return nil
  21. }
  22. // containerRestart attempts to gracefully stop and then start the
  23. // container. When stopping, wait for the given duration in seconds to
  24. // gracefully stop, before forcefully terminating the container. If
  25. // given a negative duration, wait forever for a graceful stop.
  26. func (daemon *Daemon) containerRestart(container *container.Container, seconds int) error {
  27. // Avoid unnecessarily unmounting and then directly mounting
  28. // the container when the container stops and then starts
  29. // again
  30. if err := daemon.Mount(container); err == nil {
  31. defer daemon.Unmount(container)
  32. }
  33. if err := daemon.containerStop(container, seconds); err != nil {
  34. return err
  35. }
  36. if err := daemon.containerStart(container); err != nil {
  37. return err
  38. }
  39. daemon.LogContainerEvent(container, "restart")
  40. return nil
  41. }