2015-07-16 17:14:58 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-01-07 21:38:38 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-08-11 22:27:33 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-08-11 22:27:33 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2016-01-04 19:05:26 -05:00
|
|
|
containertypes "github.com/docker/engine-api/types/container"
|
2015-07-16 17:14:58 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createContainerPlatformSpecificSettings performs platform specific container create functionality
|
2015-12-18 13:36:17 -05:00
|
|
|
func (daemon *Daemon) createContainerPlatformSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig, img *image.Image) error {
|
2015-11-03 19:57:16 -05:00
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer daemon.Unmount(container)
|
|
|
|
|
2016-01-07 23:11:21 -05:00
|
|
|
if err := container.SetupWorkingDirectory(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
for spec := range config.Volumes {
|
2015-11-03 20:03:40 -05:00
|
|
|
name := stringid.GenerateNonCryptoID()
|
|
|
|
destination := filepath.Clean(spec)
|
2015-09-29 10:01:57 -04:00
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
// Skip volumes for which we already have something mounted on that
|
|
|
|
// destination because of a --volume-from.
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.IsDestinationMounted(destination) {
|
2015-07-16 17:14:58 -04: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 14:56:26 -04:00
|
|
|
return derr.ErrorCodeMountOverFile.WithArgs(path)
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2015-08-24 13:57:39 -04:00
|
|
|
volumeDriver := hostConfig.VolumeDriver
|
2015-08-11 22:27:33 -04: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 16:29:14 -04:00
|
|
|
v, err := daemon.volumes.CreateWithRef(name, volumeDriver, container.ID, nil)
|
2015-07-16 17:14:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-09-11 16:17:59 -04:00
|
|
|
if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
|
2015-07-16 17:14:58 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:38:38 -05:00
|
|
|
container.AddMountPointWithVolume(destination, v, true)
|
|
|
|
}
|
|
|
|
return daemon.populateVolumes(container)
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateVolumes copies data from the container's rootfs into the volume for non-binds.
|
|
|
|
// this is only called when the container is created.
|
|
|
|
func (daemon *Daemon) populateVolumes(c *container.Container) error {
|
|
|
|
for _, mnt := range c.MountPoints {
|
|
|
|
// skip binds and volumes referenced by other containers (ie, volumes-from)
|
|
|
|
if mnt.Driver == "" || mnt.Volume == nil || len(daemon.volumes.Refs(mnt.Volume)) > 1 {
|
|
|
|
continue
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:38:38 -05:00
|
|
|
logrus.Debugf("copying image data from %s:%s, to %s", c.ID, mnt.Destination, mnt.Name)
|
|
|
|
if err := c.CopyImagePathContent(mnt.Volume, mnt.Destination); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|