delete.go 5.3 KB

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