delete.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // TODO: why was just job.Error(err) without return if the function cannot continue w/o container name?
  21. //job.Error(err)
  22. }
  23. parent, n := path.Split(name)
  24. if parent == "/" {
  25. return fmt.Errorf("Conflict, cannot remove the default name of the container")
  26. }
  27. pe := daemon.ContainerGraph().Get(parent)
  28. if pe == nil {
  29. return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
  30. }
  31. parentContainer, _ := daemon.Get(pe.ID())
  32. if parentContainer != nil {
  33. parentContainer.DisableLink(n)
  34. }
  35. if err := daemon.ContainerGraph().Delete(name); err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. if container != nil {
  41. // stop collection of stats for the container regardless
  42. // if stats are currently getting collected.
  43. daemon.statsCollector.stopCollection(container)
  44. if container.IsRunning() {
  45. if config.ForceRemove {
  46. if err := container.Kill(); err != nil {
  47. return fmt.Errorf("Could not kill running container, cannot remove - %v", err)
  48. }
  49. } else {
  50. return fmt.Errorf("Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f")
  51. }
  52. }
  53. if config.ForceRemove {
  54. if err := daemon.ForceRm(container); err != nil {
  55. logrus.Errorf("Cannot destroy container %s: %v", name, err)
  56. }
  57. } else {
  58. if err := daemon.Rm(container); err != nil {
  59. return fmt.Errorf("Cannot destroy container %s: %v", name, err)
  60. }
  61. }
  62. container.LogEvent("destroy")
  63. if config.RemoveVolume {
  64. daemon.DeleteVolumes(container.VolumePaths())
  65. }
  66. }
  67. return nil
  68. }
  69. func (daemon *Daemon) DeleteVolumes(volumeIDs map[string]struct{}) {
  70. for id := range volumeIDs {
  71. if err := daemon.volumes.Delete(id); err != nil {
  72. logrus.Infof("%s", err)
  73. continue
  74. }
  75. }
  76. }
  77. func (daemon *Daemon) Rm(container *Container) (err error) {
  78. return daemon.commonRm(container, false)
  79. }
  80. func (daemon *Daemon) ForceRm(container *Container) (err error) {
  81. return daemon.commonRm(container, true)
  82. }
  83. // Destroy unregisters a container from the daemon and cleanly removes its contents from the filesystem.
  84. func (daemon *Daemon) commonRm(container *Container, forceRemove bool) (err error) {
  85. if container == nil {
  86. return fmt.Errorf("The given container is <nil>")
  87. }
  88. element := daemon.containers.Get(container.ID)
  89. if element == nil {
  90. return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.ID)
  91. }
  92. // Container state RemovalInProgress should be used to avoid races.
  93. if err = container.SetRemovalInProgress(); err != nil {
  94. return fmt.Errorf("Failed to set container state to RemovalInProgress: %s", err)
  95. }
  96. defer container.ResetRemovalInProgress()
  97. if err = container.Stop(3); err != nil {
  98. return err
  99. }
  100. // Mark container dead. We don't want anybody to be restarting it.
  101. container.SetDead()
  102. // Save container state to disk. So that if error happens before
  103. // container meta file got removed from disk, then a restart of
  104. // docker should not make a dead container alive.
  105. container.ToDisk()
  106. // If force removal is required, delete container from various
  107. // indexes even if removal failed.
  108. defer func() {
  109. if err != nil && forceRemove {
  110. daemon.idIndex.Delete(container.ID)
  111. daemon.containers.Delete(container.ID)
  112. os.RemoveAll(container.root)
  113. }
  114. }()
  115. container.derefVolumes()
  116. if _, err := daemon.containerGraph.Purge(container.ID); err != nil {
  117. logrus.Debugf("Unable to remove container from link graph: %s", err)
  118. }
  119. if err = daemon.driver.Remove(container.ID); err != nil {
  120. return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.driver, container.ID, err)
  121. }
  122. initID := fmt.Sprintf("%s-init", container.ID)
  123. if err := daemon.driver.Remove(initID); err != nil {
  124. return fmt.Errorf("Driver %s failed to remove init filesystem %s: %s", daemon.driver, initID, err)
  125. }
  126. if err = os.RemoveAll(container.root); err != nil {
  127. return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
  128. }
  129. if err = daemon.execDriver.Clean(container.ID); err != nil {
  130. return fmt.Errorf("Unable to remove execdriver data for %s: %s", container.ID, err)
  131. }
  132. selinuxFreeLxcContexts(container.ProcessLabel)
  133. daemon.idIndex.Delete(container.ID)
  134. daemon.containers.Delete(container.ID)
  135. return nil
  136. }