delete.go 4.2 KB

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