delete.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. container, 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 := container.SetRemovalInProgress(); inProgress {
  28. err := fmt.Errorf("removal of container %s is already in progress", name)
  29. return errdefs.Conflict(err)
  30. }
  31. defer container.ResetRemovalInProgress()
  32. // check if container wasn't deregistered by previous rm since Get
  33. if c := daemon.containers.Get(container.ID); c == nil {
  34. return nil
  35. }
  36. if config.RemoveLink {
  37. return daemon.rmLink(container, name)
  38. }
  39. err = daemon.cleanupContainer(container, config.ForceRemove, config.RemoveVolume)
  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, forceRemove, removeVolume bool) error {
  69. if container.IsRunning() {
  70. if !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. if err := daemon.containerStop(container, 3); err != nil {
  87. return err
  88. }
  89. // Mark container dead. We don't want anybody to be restarting it.
  90. container.Lock()
  91. container.Dead = true
  92. // Save container state to disk. So that if error happens before
  93. // container meta file got removed from disk, then a restart of
  94. // docker should not make a dead container alive.
  95. if err := container.CheckpointTo(daemon.containersReplica); err != nil && !os.IsNotExist(err) {
  96. logrus.Errorf("Error saving dying container to disk: %v", err)
  97. }
  98. container.Unlock()
  99. // When container creation fails and `RWLayer` has not been created yet, we
  100. // do not call `ReleaseRWLayer`
  101. if container.RWLayer != nil {
  102. if err := daemon.imageService.ReleaseLayer(container.RWLayer); err != nil {
  103. err = errors.Wrapf(err, "container %s", container.ID)
  104. container.SetRemovalError(err)
  105. return err
  106. }
  107. container.RWLayer = nil
  108. }
  109. if err := containerfs.EnsureRemoveAll(container.Root); err != nil {
  110. err = errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
  111. container.SetRemovalError(err)
  112. return err
  113. }
  114. linkNames := daemon.linkIndex.delete(container)
  115. selinux.ReleaseLabel(container.ProcessLabel)
  116. daemon.idIndex.Delete(container.ID)
  117. daemon.containers.Delete(container.ID)
  118. daemon.containersReplica.Delete(container)
  119. if err := daemon.removeMountPoints(container, removeVolume); err != nil {
  120. logrus.Error(err)
  121. }
  122. for _, name := range linkNames {
  123. daemon.releaseName(name)
  124. }
  125. container.SetRemoved()
  126. stateCtr.del(container.ID)
  127. daemon.LogContainerEvent(container, "destroy")
  128. return nil
  129. }