delete.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package 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/layer"
  11. "github.com/docker/docker/pkg/system"
  12. volumestore "github.com/docker/docker/volume/store"
  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 stateConflictError{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 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 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) (err 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 stateConflictError{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. metadata, err := daemon.stores[container.Platform].layerStore.ReleaseRWLayer(container.RWLayer)
  103. layer.LogReleaseMetadata(metadata)
  104. if err != nil && err != layer.ErrMountDoesNotExist && !os.IsNotExist(errors.Cause(err)) {
  105. return errors.Wrapf(err, "driver %q failed to remove root filesystem for %s", daemon.GraphDriverName(container.Platform), container.ID)
  106. }
  107. }
  108. if err := system.EnsureRemoveAll(container.Root); err != nil {
  109. return errors.Wrapf(err, "unable to remove filesystem for %s", container.ID)
  110. }
  111. linkNames := daemon.linkIndex.delete(container)
  112. selinuxFreeLxcContexts(container.ProcessLabel)
  113. daemon.idIndex.Delete(container.ID)
  114. daemon.containers.Delete(container.ID)
  115. daemon.containersReplica.Delete(container)
  116. if e := daemon.removeMountPoints(container, removeVolume); e != nil {
  117. logrus.Error(e)
  118. }
  119. for _, name := range linkNames {
  120. daemon.releaseName(name)
  121. }
  122. container.SetRemoved()
  123. stateCtr.del(container.ID)
  124. daemon.LogContainerEvent(container, "destroy")
  125. return nil
  126. }
  127. // VolumeRm removes the volume with the given name.
  128. // If the volume is referenced by a container it is not removed
  129. // This is called directly from the Engine API
  130. func (daemon *Daemon) VolumeRm(name string, force bool) error {
  131. err := daemon.volumeRm(name)
  132. if err != nil && volumestore.IsInUse(err) {
  133. return stateConflictError{err}
  134. }
  135. if err == nil || force {
  136. daemon.volumes.Purge(name)
  137. return nil
  138. }
  139. return err
  140. }
  141. func (daemon *Daemon) volumeRm(name string) error {
  142. v, err := daemon.volumes.Get(name)
  143. if err != nil {
  144. return err
  145. }
  146. if err := daemon.volumes.Remove(v); err != nil {
  147. return errors.Wrap(err, "unable to remove volume")
  148. }
  149. daemon.LogVolumeEvent(v.Name(), "destroy", map[string]string{"driver": v.DriverName()})
  150. return nil
  151. }