restart.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. containertypes "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/api/types/events"
  7. "github.com/docker/docker/container"
  8. "github.com/docker/docker/internal/compatcontext"
  9. )
  10. // ContainerRestart stops and starts a container. It attempts to
  11. // gracefully stop the container within the given timeout, forcefully
  12. // stopping it if the timeout is exceeded. If given a negative
  13. // timeout, ContainerRestart will wait forever until a graceful
  14. // stop. Returns an error if the container cannot be found, or if
  15. // there is an underlying error at any stage of the restart.
  16. func (daemon *Daemon) ContainerRestart(ctx context.Context, name string, options containertypes.StopOptions) error {
  17. ctr, err := daemon.GetContainer(name)
  18. if err != nil {
  19. return err
  20. }
  21. err = daemon.containerRestart(ctx, daemon.config(), ctr, options)
  22. if err != nil {
  23. return fmt.Errorf("Cannot restart container %s: %v", name, err)
  24. }
  25. return nil
  26. }
  27. // containerRestart attempts to gracefully stop and then start the
  28. // container. When stopping, wait for the given duration in seconds to
  29. // gracefully stop, before forcefully terminating the container. If
  30. // given a negative duration, wait forever for a graceful stop.
  31. func (daemon *Daemon) containerRestart(ctx context.Context, daemonCfg *configStore, container *container.Container, options containertypes.StopOptions) error {
  32. // Restarting is expected to be an atomic operation, and cancelling
  33. // the request should not cancel the stop -> start sequence.
  34. ctx = compatcontext.WithoutCancel(ctx)
  35. // Determine isolation. If not specified in the hostconfig, use daemon default.
  36. actualIsolation := container.HostConfig.Isolation
  37. if containertypes.Isolation.IsDefault(actualIsolation) {
  38. actualIsolation = daemon.defaultIsolation
  39. }
  40. // Avoid unnecessarily unmounting and then directly mounting
  41. // the container when the container stops and then starts
  42. // again. We do not do this for Hyper-V isolated containers
  43. // (implying also on Windows) as the HCS must have exclusive
  44. // access to mount the containers filesystem inside the utility
  45. // VM.
  46. if !containertypes.Isolation.IsHyperV(actualIsolation) {
  47. if err := daemon.Mount(container); err == nil {
  48. defer daemon.Unmount(container)
  49. }
  50. }
  51. if container.IsRunning() {
  52. container.Lock()
  53. container.HasBeenManuallyRestarted = true
  54. container.Unlock()
  55. err := daemon.containerStop(ctx, container, options)
  56. if err != nil {
  57. return err
  58. }
  59. }
  60. if err := daemon.containerStart(ctx, daemonCfg, container, "", "", true); err != nil {
  61. return err
  62. }
  63. daemon.LogContainerEvent(container, events.ActionRestart)
  64. return nil
  65. }