github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/daemon/delete.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/docker/docker/api/errors"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/container"
    13  	"github.com/docker/docker/layer"
    14  	volumestore "github.com/docker/docker/volume/store"
    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 *types.ContainerRmConfig) error {
    22  	container, err := daemon.GetContainer(name)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	// Container state RemovalInProgress should be used to avoid races.
    28  	if inProgress := container.SetRemovalInProgress(); inProgress {
    29  		err := fmt.Errorf("removal of container %s is already in progress", name)
    30  		return errors.NewBadRequestError(err)
    31  	}
    32  	defer container.ResetRemovalInProgress()
    33  
    34  	// check if container wasn't deregistered by previous rm since Get
    35  	if c := daemon.containers.Get(container.ID); c == nil {
    36  		return nil
    37  	}
    38  
    39  	if config.RemoveLink {
    40  		return daemon.rmLink(container, name)
    41  	}
    42  
    43  	return daemon.cleanupContainer(container, config.ForceRemove, config.RemoveVolume)
    44  }
    45  
    46  func (daemon *Daemon) rmLink(container *container.Container, name string) error {
    47  	if name[0] != '/' {
    48  		name = "/" + name
    49  	}
    50  	parent, n := path.Split(name)
    51  	if parent == "/" {
    52  		return fmt.Errorf("Conflict, cannot remove the default name of the container")
    53  	}
    54  
    55  	parent = strings.TrimSuffix(parent, "/")
    56  	pe, err := daemon.nameIndex.Get(parent)
    57  	if err != nil {
    58  		return fmt.Errorf("Cannot get parent %s for name %s", parent, name)
    59  	}
    60  
    61  	daemon.releaseName(name)
    62  	parentContainer, _ := daemon.GetContainer(pe)
    63  	if parentContainer != nil {
    64  		daemon.linkIndex.unlink(name, container, parentContainer)
    65  		if err := daemon.updateNetwork(parentContainer); err != nil {
    66  			logrus.Debugf("Could not update network to remove link %s: %v", n, err)
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  // cleanupContainer unregisters a container from the daemon, stops stats
    73  // collection and cleanly removes contents and metadata from the filesystem.
    74  func (daemon *Daemon) cleanupContainer(container *container.Container, forceRemove, removeVolume bool) (err error) {
    75  	if container.IsRunning() {
    76  		if !forceRemove {
    77  			err := fmt.Errorf("You cannot remove a running container %s. Stop the container before attempting removal or use -f", container.ID)
    78  			return errors.NewRequestConflictError(err)
    79  		}
    80  		if err := daemon.Kill(container); err != nil {
    81  			return fmt.Errorf("Could not kill running container %s, cannot remove - %v", container.ID, err)
    82  		}
    83  	}
    84  
    85  	// stop collection of stats for the container regardless
    86  	// if stats are currently getting collected.
    87  	daemon.statsCollector.stopCollection(container)
    88  
    89  	if err = daemon.containerStop(container, 3); err != nil {
    90  		return err
    91  	}
    92  
    93  	// Mark container dead. We don't want anybody to be restarting it.
    94  	container.SetDead()
    95  
    96  	// Save container state to disk. So that if error happens before
    97  	// container meta file got removed from disk, then a restart of
    98  	// docker should not make a dead container alive.
    99  	if err := container.ToDiskLocking(); err != nil && !os.IsNotExist(err) {
   100  		logrus.Errorf("Error saving dying container to disk: %v", err)
   101  	}
   102  
   103  	// If force removal is required, delete container from various
   104  	// indexes even if removal failed.
   105  	defer func() {
   106  		if err == nil || forceRemove {
   107  			daemon.nameIndex.Delete(container.ID)
   108  			daemon.linkIndex.delete(container)
   109  			selinuxFreeLxcContexts(container.ProcessLabel)
   110  			daemon.idIndex.Delete(container.ID)
   111  			daemon.containers.Delete(container.ID)
   112  			if e := daemon.removeMountPoints(container, removeVolume); e != nil {
   113  				logrus.Error(e)
   114  			}
   115  			daemon.LogContainerEvent(container, "destroy")
   116  		}
   117  	}()
   118  
   119  	if err = os.RemoveAll(container.Root); err != nil {
   120  		return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
   121  	}
   122  
   123  	// When container creation fails and `RWLayer` has not been created yet, we
   124  	// do not call `ReleaseRWLayer`
   125  	if container.RWLayer != nil {
   126  		metadata, err := daemon.layerStore.ReleaseRWLayer(container.RWLayer)
   127  		layer.LogReleaseMetadata(metadata)
   128  		if err != nil && err != layer.ErrMountDoesNotExist {
   129  			return fmt.Errorf("Driver %s failed to remove root filesystem %s: %s", daemon.GraphDriverName(), container.ID, err)
   130  		}
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  // VolumeRm removes the volume with the given name.
   137  // If the volume is referenced by a container it is not removed
   138  // This is called directly from the remote API
   139  func (daemon *Daemon) VolumeRm(name string, force bool) error {
   140  	err := daemon.volumeRm(name)
   141  	if err == nil || force {
   142  		daemon.volumes.Purge(name)
   143  		return nil
   144  	}
   145  	return err
   146  }
   147  
   148  func (daemon *Daemon) volumeRm(name string) error {
   149  	v, err := daemon.volumes.Get(name)
   150  	if err != nil {
   151  		return err
   152  	}
   153  
   154  	if err := daemon.volumes.Remove(v); err != nil {
   155  		if volumestore.IsInUse(err) {
   156  			err := fmt.Errorf("Unable to remove volume, volume still in use: %v", err)
   157  			return errors.NewRequestConflictError(err)
   158  		}
   159  		return fmt.Errorf("Error while removing volume %s: %v", name, err)
   160  	}
   161  	daemon.LogVolumeEvent(v.Name(), "destroy", map[string]string{"driver": v.DriverName()})
   162  	return nil
   163  }