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