delete.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strings"
  7. "time"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/api/errors"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/container"
  12. "github.com/docker/docker/layer"
  13. volumestore "github.com/docker/docker/volume/store"
  14. )
  15. // ContainerRm removes the container id from the filesystem. An error
  16. // is returned if the container is not found, or if the remove
  17. // fails. If the remove succeeds, the container name is released, and
  18. // network links are removed.
  19. func (daemon *Daemon) ContainerRm(name string, config *types.ContainerRmConfig) error {
  20. start := time.Now()
  21. container, err := daemon.GetContainer(name)
  22. if err != nil {
  23. return err
  24. }
  25. // Container state RemovalInProgress should be used to avoid races.
  26. if inProgress := container.SetRemovalInProgress(); inProgress {
  27. err := fmt.Errorf("removal of container %s is already in progress", name)
  28. return errors.NewBadRequestError(err)
  29. }
  30. defer container.ResetRemovalInProgress()
  31. // check if container wasn't deregistered by previous rm since Get
  32. if c := daemon.containers.Get(container.ID); c == nil {
  33. return nil
  34. }
  35. if config.RemoveLink {
  36. return daemon.rmLink(container, name)
  37. }
  38. err = daemon.cleanupContainer(container, config.ForceRemove, config.RemoveVolume)
  39. containerActions.WithValues("delete").UpdateSince(start)
  40. return err
  41. }
  42. func (daemon *Daemon) rmLink(container *container.Container, name string) error {
  43. if name[0] != '/' {
  44. name = "/" + name
  45. }
  46. parent, n := path.Split(name)
  47. if parent == "/" {
  48. return fmt.Errorf("Conflict, cannot remove the default name of the container")
  49. }
  50. parent = strings.TrimSuffix(parent, "/")
  51. pe, err := daemon.nameIndex.Get(parent)
  52. if err != nil {
  53. return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
  54. }
  55. daemon.releaseName(name)
  56. parentContainer, _ := daemon.GetContainer(pe)
  57. if parentContainer != nil {
  58. daemon.linkIndex.unlink(name, container, parentContainer)
  59. if err := daemon.updateNetwork(parentContainer); err != nil {
  60. logrus.Debugf("Could not update network to remove link %s: %v", n, err)
  61. }
  62. }
  63. return nil
  64. }
  65. // cleanupContainer unregisters a container from the daemon, stops stats
  66. // collection and cleanly removes contents and metadata from the filesystem.
  67. func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove, removeVolume bool) (err error) {
  68. if container.IsRunning() {
  69. if !forceRemove {
  70. err := fmt.Errorf("You cannot remove a running container %s. Stop the container before attempting removal or use -f", container.ID)
  71. return errors.NewRequestConflictError(err)
  72. }
  73. if err := daemon.Kill(container); err != nil {
  74. return fmt.Errorf("Could not kill running container %s, cannot remove - %v", container.ID, err)
  75. }
  76. }
  77. // stop collection of stats for the container regardless
  78. // if stats are currently getting collected.
  79. daemon.statsCollector.stopCollection(container)
  80. if err = daemon.containerStop(container, 3); err != nil {
  81. return err
  82. }
  83. // Mark container dead. We don't want anybody to be restarting it.
  84. container.SetDead()
  85. // Save container state to disk. So that if error happens before
  86. // container meta file got removed from disk, then a restart of
  87. // docker should not make a dead container alive.
  88. if err := container.ToDiskLocking(); err != nil && !os.IsNotExist(err) {
  89. logrus.Errorf("Error saving dying container to disk: %v", err)
  90. }
  91. // If force removal is required, delete container from various
  92. // indexes even if removal failed.
  93. defer func() {
  94. if err == nil || forceRemove {
  95. daemon.nameIndex.Delete(container.ID)
  96. daemon.linkIndex.delete(container)
  97. selinuxFreeLxcContexts(container.ProcessLabel)
  98. daemon.idIndex.Delete(container.ID)
  99. daemon.containers.Delete(container.ID)
  100. if e := daemon.removeMountPoints(container, removeVolume); e != nil {
  101. logrus.Error(e)
  102. }
  103. daemon.LogContainerEvent(container, "destroy")
  104. }
  105. }()
  106. if err = os.RemoveAll(container.Root); err != nil {
  107. return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
  108. }
  109. // When container creation fails and `RWLayer` has not been created yet, we
  110. // do not call `ReleaseRWLayer`
  111. if container.RWLayer != nil {
  112. metadata, err := daemon.layerStore.ReleaseRWLayer(container.RWLayer)
  113. layer.LogReleaseMetadata(metadata)
  114. if err != nil && err != layer.ErrMountDoesNotExist {
  115. return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.GraphDriverName(), container.ID, err)
  116. }
  117. }
  118. return nil
  119. }
  120. // VolumeRm removes the volume with the given name.
  121. // If the volume is referenced by a container it is not removed
  122. // This is called directly from the Engine API
  123. func (daemon *Daemon) VolumeRm(name string, force bool) error {
  124. err := daemon.volumeRm(name)
  125. if err == nil || force {
  126. daemon.volumes.Purge(name)
  127. return nil
  128. }
  129. return err
  130. }
  131. func (daemon *Daemon) volumeRm(name string) error {
  132. v, err := daemon.volumes.Get(name)
  133. if err != nil {
  134. return err
  135. }
  136. if err := daemon.volumes.Remove(v); err != nil {
  137. if volumestore.IsInUse(err) {
  138. err := fmt.Errorf("Unable to remove volume, volume still in use: %v", err)
  139. return errors.NewRequestConflictError(err)
  140. }
  141. return fmt.Errorf("Error while removing volume %s: %v", name, err)
  142. }
  143. daemon.LogVolumeEvent(v.Name(), "destroy", map[string]string{"driver": v.DriverName()})
  144. return nil
  145. }