delete.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/errdefs"
  11. "github.com/docker/docker/pkg/containerfs"
  12. "github.com/opencontainers/selinux/go-selinux"
  13. "github.com/pkg/errors"
  14. "github.com/sirupsen/logrus"
  15. )
  16. // ContainerRm removes the container id from the filesystem. An error
  17. // is returned if the container is not found, or if the remove
  18. // fails. If the remove succeeds, the container name is released, and
  19. // network links are removed.
  20. func (daemon *Daemon) ContainerRm(name string, config *types.ContainerRmConfig) error {
  21. start := time.Now()
  22. ctr, err := daemon.GetContainer(name)
  23. if err != nil {
  24. return err
  25. }
  26. // Container state RemovalInProgress should be used to avoid races.
  27. if inProgress := ctr.SetRemovalInProgress(); inProgress {
  28. err := fmt.Errorf("removal of container %s is already in progress", name)
  29. return errdefs.Conflict(err)
  30. }
  31. defer ctr.ResetRemovalInProgress()
  32. // check if container wasn't deregistered by previous rm since Get
  33. if c := daemon.containers.Get(ctr.ID); c == nil {
  34. return nil
  35. }
  36. if config.RemoveLink {
  37. return daemon.rmLink(ctr, name)
  38. }
  39. err = daemon.cleanupContainer(ctr, *config)
  40. containerActions.WithValues("delete").UpdateSince(start)
  41. return err
  42. }
  43. func (daemon *Daemon) rmLink(container *container.Container, name string) error {
  44. if name[0] != '/' {
  45. name = "/" + name
  46. }
  47. parent, n := path.Split(name)
  48. if parent == "/" {
  49. return fmt.Errorf("Conflict, cannot remove the default link name of the container")
  50. }
  51. parent = strings.TrimSuffix(parent, "/")
  52. pe, err := daemon.containersReplica.Snapshot().GetID(parent)
  53. if err != nil {
  54. return fmt.Errorf("Cannot get parent %s for link name %s", parent, name)
  55. }
  56. daemon.releaseName(name)
  57. parentContainer, _ := daemon.GetContainer(pe)
  58. if parentContainer != nil {
  59. daemon.linkIndex.unlink(name, container, parentContainer)
  60. if err := daemon.updateNetwork(parentContainer); err != nil {
  61. logrus.Debugf("Could not update network to remove link %s: %v", n, err)
  62. }
  63. }
  64. return nil
  65. }
  66. // cleanupContainer unregisters a container from the daemon, stops stats
  67. // collection and cleanly removes contents and metadata from the filesystem.
  68. func (daemon *Daemon) cleanupContainer(container *container.Container, config types.ContainerRmConfig) error {
  69. if container.IsRunning() {
  70. if !config.ForceRemove {
  71. state := container.StateString()
  72. procedure := "Stop the container before attempting removal or force remove"
  73. if state == "paused" {
  74. procedure = "Unpause and then " + strings.ToLower(procedure)
  75. }
  76. err := fmt.Errorf("You cannot remove a %s container %s. %s", state, container.ID, procedure)
  77. return errdefs.Conflict(err)
  78. }
  79. if err := daemon.Kill(container); err != nil {
  80. return fmt.Errorf("Could not kill running container %s, cannot remove - %v", container.ID, err)
  81. }
  82. }
  83. // stop collection of stats for the container regardless
  84. // if stats are currently getting collected.
  85. daemon.statsCollector.StopCollection(container)
  86. // stopTimeout is the number of seconds to wait for the container to stop
  87. // gracefully before forcibly killing it.
  88. //
  89. // Why 3 seconds? The timeout specified here was originally added in commit
  90. // 1615bb08c7c3fc6c4b22db0a633edda516f97cf0, which added a custom timeout to
  91. // some commands, but lacking an option for a timeout on "docker rm", was
  92. // hardcoded to 10 seconds. Commit 28fd289b448164b77affd8103c0d96fd8110daf9
  93. // later on updated this to 3 seconds (but no background on that change).
  94. //
  95. // If you arrived here and know the answer, you earned yourself a picture
  96. // of a cute animal of your own choosing.
  97. var stopTimeout = 3
  98. if err := daemon.containerStop(container, &stopTimeout); err != nil {
  99. return err
  100. }
  101. // Mark container dead. We don't want anybody to be restarting it.
  102. container.Lock()
  103. container.Dead = true
  104. // Save container state to disk. So that if error happens before
  105. // container meta file got removed from disk, then a restart of
  106. // docker should not make a dead container alive.
  107. if err := container.CheckpointTo(daemon.containersReplica); err != nil && !os.IsNotExist(err) {
  108. logrus.Errorf("Error saving dying container to disk: %v", err)
  109. }
  110. container.Unlock()
  111. // When container creation fails and `RWLayer` has not been created yet, we
  112. // do not call `ReleaseRWLayer`
  113. if container.RWLayer != nil {
  114. if err := daemon.imageService.ReleaseLayer(container.RWLayer); err != nil {
  115. err = errors.Wrapf(err, "container %s", container.ID)
  116. container.SetRemovalError(err)
  117. return err
  118. }
  119. container.RWLayer = nil
  120. }
  121. if err := containerfs.EnsureRemoveAll(container.Root); err != nil {
  122. err = errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
  123. container.SetRemovalError(err)
  124. return err
  125. }
  126. linkNames := daemon.linkIndex.delete(container)
  127. selinux.ReleaseLabel(container.ProcessLabel)
  128. daemon.idIndex.Delete(container.ID)
  129. daemon.containers.Delete(container.ID)
  130. daemon.containersReplica.Delete(container)
  131. if err := daemon.removeMountPoints(container, config.RemoveVolume); err != nil {
  132. logrus.Error(err)
  133. }
  134. for _, name := range linkNames {
  135. daemon.releaseName(name)
  136. }
  137. container.SetRemoved()
  138. stateCtr.del(container.ID)
  139. daemon.LogContainerEvent(container, "destroy")
  140. return nil
  141. }