delete.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. parentContainer, _ := daemon.Get(pe.ID())
  30. if err := daemon.ContainerGraph().Delete(name); err != nil {
  31. return err
  32. }
  33. if parentContainer != nil {
  34. parentContainer.DisableLink(n)
  35. }
  36. return nil
  37. }
  38. if err := daemon.rm(container, config.ForceRemove); err != nil {
  39. return fmt.Errorf("Cannot destroy container %s: %v", name, err)
  40. }
  41. container.LogEvent("destroy")
  42. if config.RemoveVolume {
  43. container.removeMountPoints()
  44. }
  45. return nil
  46. }
  47. // Destroy unregisters a container from the daemon and cleanly removes its contents from the filesystem.
  48. func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
  49. if container.IsRunning() {
  50. if !forceRemove {
  51. return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
  52. }
  53. if err := container.Kill(); err != nil {
  54. return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
  55. }
  56. }
  57. // stop collection of stats for the container regardless
  58. // if stats are currently getting collected.
  59. daemon.statsCollector.stopCollection(container)
  60. element := daemon.containers.Get(container.ID)
  61. if element == nil {
  62. return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.ID)
  63. }
  64. // Container state RemovalInProgress should be used to avoid races.
  65. if err = container.SetRemovalInProgress(); err != nil {
  66. return fmt.Errorf("Failed to set container state to RemovalInProgress: %s", err)
  67. }
  68. defer container.ResetRemovalInProgress()
  69. if err = container.Stop(3); err != nil {
  70. return err
  71. }
  72. // Mark container dead. We don't want anybody to be restarting it.
  73. container.SetDead()
  74. // Save container state to disk. So that if error happens before
  75. // container meta file got removed from disk, then a restart of
  76. // docker should not make a dead container alive.
  77. container.ToDisk()
  78. // If force removal is required, delete container from various
  79. // indexes even if removal failed.
  80. defer func() {
  81. if err != nil && forceRemove {
  82. daemon.idIndex.Delete(container.ID)
  83. daemon.containers.Delete(container.ID)
  84. os.RemoveAll(container.root)
  85. }
  86. }()
  87. if _, err := daemon.containerGraph.Purge(container.ID); err != nil {
  88. logrus.Debugf("Unable to remove container from link graph: %s", err)
  89. }
  90. if err = daemon.driver.Remove(container.ID); err != nil {
  91. return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.driver, container.ID, err)
  92. }
  93. initID := fmt.Sprintf("%s-init", container.ID)
  94. if err := daemon.driver.Remove(initID); err != nil {
  95. return fmt.Errorf("Driver %s failed to remove init filesystem %s: %s", daemon.driver, initID, err)
  96. }
  97. if err = os.RemoveAll(container.root); err != nil {
  98. return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
  99. }
  100. if err = daemon.execDriver.Clean(container.ID); err != nil {
  101. return fmt.Errorf("Unable to remove execdriver data for %s: %s", container.ID, err)
  102. }
  103. selinuxFreeLxcContexts(container.ProcessLabel)
  104. daemon.idIndex.Delete(container.ID)
  105. daemon.containers.Delete(container.ID)
  106. return nil
  107. }
  108. func (daemon *Daemon) DeleteVolumes(c *Container) error {
  109. return c.removeMountPoints()
  110. }