delete.go 4.9 KB

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