github.com/akashshinde/docker@v1.9.1/daemon/create_unix.go (about)

     1  // +build !windows
     2  
     3  package daemon
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  
     9  	derr "github.com/docker/docker/errors"
    10  	"github.com/docker/docker/image"
    11  	"github.com/docker/docker/pkg/stringid"
    12  	"github.com/docker/docker/runconfig"
    13  	"github.com/docker/docker/volume"
    14  	"github.com/opencontainers/runc/libcontainer/label"
    15  )
    16  
    17  // createContainerPlatformSpecificSettings performs platform specific container create functionality
    18  func createContainerPlatformSpecificSettings(container *Container, config *runconfig.Config, hostConfig *runconfig.HostConfig, img *image.Image) error {
    19  	for spec := range config.Volumes {
    20  		name := stringid.GenerateNonCryptoID()
    21  		destination := filepath.Clean(spec)
    22  
    23  		// Skip volumes for which we already have something mounted on that
    24  		// destination because of a --volume-from.
    25  		if container.isDestinationMounted(destination) {
    26  			continue
    27  		}
    28  		path, err := container.GetResourcePath(destination)
    29  		if err != nil {
    30  			return err
    31  		}
    32  
    33  		stat, err := os.Stat(path)
    34  		if err == nil && !stat.IsDir() {
    35  			return derr.ErrorCodeMountOverFile.WithArgs(path)
    36  		}
    37  
    38  		volumeDriver := hostConfig.VolumeDriver
    39  		if destination != "" && img != nil {
    40  			if _, ok := img.ContainerConfig.Volumes[destination]; ok {
    41  				// check for whether bind is not specified and then set to local
    42  				if _, ok := container.MountPoints[destination]; !ok {
    43  					volumeDriver = volume.DefaultDriverName
    44  				}
    45  			}
    46  		}
    47  
    48  		v, err := container.daemon.createVolume(name, volumeDriver, nil)
    49  		if err != nil {
    50  			return err
    51  		}
    52  
    53  		if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
    54  			return err
    55  		}
    56  
    57  		// never attempt to copy existing content in a container FS to a shared volume
    58  		if v.DriverName() == volume.DefaultDriverName {
    59  			if err := container.copyImagePathContent(v, destination); err != nil {
    60  				return err
    61  			}
    62  		}
    63  
    64  		container.addMountPointWithVolume(destination, v, true)
    65  	}
    66  	return nil
    67  }