delete.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "github.com/Sirupsen/logrus"
  7. )
  8. type ContainerRmConfig struct {
  9. ForceRemove, RemoveVolume, RemoveLink bool
  10. }
  11. func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error {
  12. container, err := daemon.Get(name)
  13. if err != nil {
  14. return err
  15. }
  16. if config.RemoveLink {
  17. name, err := GetFullContainerName(name)
  18. if err != nil {
  19. return err
  20. }
  21. parent, n := path.Split(name)
  22. if parent == "/" {
  23. return fmt.Errorf("Conflict, cannot remove the default name of the container")
  24. }
  25. pe := daemon.ContainerGraph().Get(parent)
  26. if pe == nil {
  27. return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
  28. }
  29. if err := daemon.ContainerGraph().Delete(name); err != nil {
  30. return err
  31. }
  32. parentContainer, _ := daemon.Get(pe.ID())
  33. if parentContainer != nil {
  34. if err := parentContainer.UpdateNetwork(); err != nil {
  35. logrus.Debugf("Could not update network to remove link %s: %v", n, err)
  36. }
  37. }
  38. return nil
  39. }
  40. if err := daemon.rm(container, config.ForceRemove); err != nil {
  41. return fmt.Errorf("Cannot destroy container %s: %v", name, err)
  42. }
  43. container.removeMountPoints(config.RemoveVolume)
  44. return nil
  45. }
  46. // Destroy unregisters a container from the daemon and cleanly removes its contents from the filesystem.
  47. func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
  48. if container.IsRunning() {
  49. if !forceRemove {
  50. return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
  51. }
  52. if err := container.Kill(); err != nil {
  53. return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
  54. }
  55. }
  56. // stop collection of stats for the container regardless
  57. // if stats are currently getting collected.
  58. daemon.statsCollector.stopCollection(container)
  59. element := daemon.containers.Get(container.ID)
  60. if element == nil {
  61. return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.ID)
  62. }
  63. // Container state RemovalInProgress should be used to avoid races.
  64. if err = container.SetRemovalInProgress(); err != nil {
  65. return fmt.Errorf("Failed to set container state to RemovalInProgress: %s", err)
  66. }
  67. defer container.ResetRemovalInProgress()
  68. if err = container.Stop(3); err != nil {
  69. return err
  70. }
  71. // Mark container dead. We don't want anybody to be restarting it.
  72. container.SetDead()
  73. // Save container state to disk. So that if error happens before
  74. // container meta file got removed from disk, then a restart of
  75. // docker should not make a dead container alive.
  76. if err := container.ToDisk(); err != nil {
  77. logrus.Errorf("Error saving dying container to disk: %v", err)
  78. }
  79. // If force removal is required, delete container from various
  80. // indexes even if removal failed.
  81. defer func() {
  82. if err != nil && forceRemove {
  83. daemon.idIndex.Delete(container.ID)
  84. daemon.containers.Delete(container.ID)
  85. os.RemoveAll(container.root)
  86. container.LogEvent("destroy")
  87. }
  88. }()
  89. if _, err := daemon.containerGraph.Purge(container.ID); err != nil {
  90. logrus.Debugf("Unable to remove container from link graph: %s", err)
  91. }
  92. if err = daemon.driver.Remove(container.ID); err != nil {
  93. return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.driver, container.ID, err)
  94. }
  95. initID := fmt.Sprintf("%s-init", container.ID)
  96. if err := daemon.driver.Remove(initID); err != nil {
  97. return fmt.Errorf("Driver %s failed to remove init filesystem %s: %s", daemon.driver, initID, err)
  98. }
  99. if err = os.RemoveAll(container.root); err != nil {
  100. return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
  101. }
  102. if err = daemon.execDriver.Clean(container.ID); err != nil {
  103. return fmt.Errorf("Unable to remove execdriver data for %s: %s", container.ID, err)
  104. }
  105. selinuxFreeLxcContexts(container.ProcessLabel)
  106. daemon.idIndex.Delete(container.ID)
  107. daemon.containers.Delete(container.ID)
  108. container.LogEvent("destroy")
  109. return nil
  110. }
  111. // VolumeRm removes the volume with the given name.
  112. // If the volume is referenced by a container it is not removed
  113. // This is called directly from the remote API
  114. func (daemon *Daemon) VolumeRm(name string) error {
  115. v, err := daemon.volumes.Get(name)
  116. if err != nil {
  117. return err
  118. }
  119. if err := daemon.volumes.Remove(v); err != nil {
  120. if err == ErrVolumeInUse {
  121. return fmt.Errorf("Conflict: %v", err)
  122. }
  123. return err
  124. }
  125. return nil
  126. }