2015-07-16 21:14:58 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-17 18:54:14 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-08-12 02:27:33 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-08-12 02:27:33 +00:00
|
|
|
"github.com/docker/docker/volume"
|
2016-01-05 00:05:26 +00:00
|
|
|
containertypes "github.com/docker/engine-api/types/container"
|
2015-07-16 21:14:58 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createContainerPlatformSpecificSettings performs platform specific container create functionality
|
2015-12-18 18:36:17 +00:00
|
|
|
func (daemon *Daemon) createContainerPlatformSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig, img *image.Image) error {
|
2015-11-04 00:57:16 +00:00
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer daemon.Unmount(container)
|
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
for spec := range config.Volumes {
|
2015-11-04 01:03:40 +00:00
|
|
|
name := stringid.GenerateNonCryptoID()
|
|
|
|
destination := filepath.Clean(spec)
|
2015-09-29 14:01:57 +00:00
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
// Skip volumes for which we already have something mounted on that
|
|
|
|
// destination because of a --volume-from.
|
2015-11-12 19:55:17 +00:00
|
|
|
if container.IsDestinationMounted(destination) {
|
2015-07-16 21:14:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
path, err := container.GetResourcePath(destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err == nil && !stat.IsDir() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeMountOverFile.WithArgs(path)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 17:57:39 +00:00
|
|
|
volumeDriver := hostConfig.VolumeDriver
|
2015-08-12 02:27:33 +00:00
|
|
|
if destination != "" && img != nil {
|
|
|
|
if _, ok := img.ContainerConfig.Volumes[destination]; ok {
|
|
|
|
// check for whether bind is not specified and then set to local
|
|
|
|
if _, ok := container.MountPoints[destination]; !ok {
|
|
|
|
volumeDriver = volume.DefaultDriverName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:29:14 +00:00
|
|
|
v, err := daemon.volumes.CreateWithRef(name, volumeDriver, container.ID, nil)
|
2015-07-16 21:14:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
|
2015-09-11 20:17:59 +00:00
|
|
|
if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
|
2015-07-16 21:14:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:27:33 +00:00
|
|
|
// never attempt to copy existing content in a container FS to a shared volume
|
2015-09-02 00:13:38 +00:00
|
|
|
if v.DriverName() == volume.DefaultDriverName {
|
2015-11-12 19:55:17 +00:00
|
|
|
if err := container.CopyImagePathContent(v, destination); err != nil {
|
2015-08-12 02:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
container.AddMountPointWithVolume(destination, v, true)
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|