github.com/rawahars/moby@v24.0.4+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  	"github.com/containerd/containerd/log"
     9  	mounttypes "github.com/docker/docker/api/types/mount"
    10  	"github.com/docker/docker/container"
    11  	volumesservice "github.com/docker/docker/volume/service"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  func (daemon *Daemon) prepareMountPoints(container *container.Container) error {
    16  	alive := container.IsRunning()
    17  	for _, config := range container.MountPoints {
    18  		if err := daemon.lazyInitializeVolume(container.ID, config); err != nil {
    19  			return err
    20  		}
    21  		if config.Volume == nil {
    22  			// FIXME(thaJeztah): should we check for config.Type here as well? (i.e., skip bind-mounts etc)
    23  			continue
    24  		}
    25  		if alive {
    26  			log.G(context.TODO()).WithFields(logrus.Fields{
    27  				"container": container.ID,
    28  				"volume":    config.Volume.Name(),
    29  			}).Debug("Live-restoring volume for alive container")
    30  			if err := config.LiveRestore(context.TODO()); err != nil {
    31  				return err
    32  			}
    33  		}
    34  	}
    35  	return nil
    36  }
    37  
    38  func (daemon *Daemon) removeMountPoints(container *container.Container, rm bool) error {
    39  	var rmErrors []string
    40  	ctx := context.TODO()
    41  	for _, m := range container.MountPoints {
    42  		if m.Type != mounttypes.TypeVolume || m.Volume == nil {
    43  			continue
    44  		}
    45  		daemon.volumes.Release(ctx, m.Volume.Name(), container.ID)
    46  		if !rm {
    47  			continue
    48  		}
    49  
    50  		// Do not remove named mountpoints
    51  		// these are mountpoints specified like `docker run -v <name>:/foo`
    52  		if m.Spec.Source != "" {
    53  			continue
    54  		}
    55  
    56  		err := daemon.volumes.Remove(ctx, m.Volume.Name())
    57  		// Ignore volume in use errors because having this
    58  		// volume being referenced by other container is
    59  		// not an error, but an implementation detail.
    60  		// This prevents docker from logging "ERROR: Volume in use"
    61  		// where there is another container using the volume.
    62  		if err != nil && !volumesservice.IsInUse(err) {
    63  			rmErrors = append(rmErrors, err.Error())
    64  		}
    65  	}
    66  
    67  	if len(rmErrors) > 0 {
    68  		return fmt.Errorf("Error removing volumes:\n%v", strings.Join(rmErrors, "\n"))
    69  	}
    70  	return nil
    71  }