github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/daemon/create_unix.go (about)

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