github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/daemon/mounts.go (about)

     1  package daemon // import "github.com/docker/docker/daemon"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	mounttypes "github.com/docker/docker/api/types/mount"
     9  	"github.com/docker/docker/container"
    10  	volumesservice "github.com/docker/docker/volume/service"
    11  )
    12  
    13  func (daemon *Daemon) prepareMountPoints(container *container.Container) error {
    14  	for _, config := range container.MountPoints {
    15  		if err := daemon.lazyInitializeVolume(container.ID, config); err != nil {
    16  			return err
    17  		}
    18  	}
    19  	return nil
    20  }
    21  
    22  func (daemon *Daemon) removeMountPoints(container *container.Container, rm bool) error {
    23  	var rmErrors []string
    24  	ctx := context.TODO()
    25  	for _, m := range container.MountPoints {
    26  		if m.Type != mounttypes.TypeVolume || m.Volume == nil {
    27  			continue
    28  		}
    29  		daemon.volumes.Release(ctx, m.Volume.Name(), container.ID)
    30  		if !rm {
    31  			continue
    32  		}
    33  
    34  		// Do not remove named mountpoints
    35  		// these are mountpoints specified like `docker run -v <name>:/foo`
    36  		if m.Spec.Source != "" {
    37  			continue
    38  		}
    39  
    40  		err := daemon.volumes.Remove(ctx, m.Volume.Name())
    41  		// Ignore volume in use errors because having this
    42  		// volume being referenced by other container is
    43  		// not an error, but an implementation detail.
    44  		// This prevents docker from logging "ERROR: Volume in use"
    45  		// where there is another container using the volume.
    46  		if err != nil && !volumesservice.IsInUse(err) {
    47  			rmErrors = append(rmErrors, err.Error())
    48  		}
    49  	}
    50  
    51  	if len(rmErrors) > 0 {
    52  		return fmt.Errorf("Error removing volumes:\n%v", strings.Join(rmErrors, "\n"))
    53  	}
    54  	return nil
    55  }