2015-07-16 17:14:58 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-09-09 22:23:06 -04:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-09 22:23:06 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/volume"
|
2015-07-16 17:14:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// createContainerPlatformSpecificSettings performs platform specific container create functionality
|
2016-01-08 13:19:25 -05:00
|
|
|
func (daemon *Daemon) createContainerPlatformSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig) error {
|
2016-05-26 16:24:22 -04:00
|
|
|
// Make sure the host config has the default daemon isolation if not specified by caller.
|
|
|
|
if containertypes.Isolation.IsDefault(containertypes.Isolation(hostConfig.Isolation)) {
|
|
|
|
hostConfig.Isolation = daemon.defaultIsolation
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:43:04 -04:00
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer daemon.Unmount(container)
|
|
|
|
if err := container.SetupWorkingDirectory(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
for spec := range config.Volumes {
|
|
|
|
|
Add new `HostConfig` field, `Mounts`.
`Mounts` allows users to specify in a much safer way the volumes they
want to use in the container.
This replaces `Binds` and `Volumes`, which both still exist, but
`Mounts` and `Binds`/`Volumes` are exclussive.
The CLI will continue to use `Binds` and `Volumes` due to concerns with
parsing the volume specs on the client side and cross-platform support
(for now).
The new API follows exactly the services mount API.
Example usage of `Mounts`:
```
$ curl -XPOST localhost:2375/containers/create -d '{
"Image": "alpine:latest",
"HostConfig": {
"Mounts": [{
"Type": "Volume",
"Target": "/foo"
},{
"Type": "bind",
"Source": "/var/run/docker.sock",
"Target": "/var/run/docker.sock",
},{
"Type": "volume",
"Name": "important_data",
"Target": "/var/data",
"ReadOnly": true,
"VolumeOptions": {
"DriverConfig": {
Name: "awesomeStorage",
Options: {"size": "10m"},
Labels: {"some":"label"}
}
}]
}
}'
```
There are currently 2 types of mounts:
- **bind**: Paths on the host that get mounted into the
container. Paths must exist prior to creating the container.
- **volume**: Volumes that persist after the
container is removed.
Not all fields are available in each type, and validation is done to
ensure these fields aren't mixed up between types.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2016-04-26 14:25:35 -04:00
|
|
|
mp, err := volume.ParseMountRaw(spec, hostConfig.VolumeDriver)
|
2015-09-09 22:23:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unrecognised volume spec: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the mountpoint doesn't have a name, generate one.
|
|
|
|
if len(mp.Name) == 0 {
|
|
|
|
mp.Name = stringid.GenerateNonCryptoID()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(mp.Destination) {
|
2015-09-09 22:23:06 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeDriver := hostConfig.VolumeDriver
|
|
|
|
|
|
|
|
// Create the volume in the volume driver. If it doesn't exist,
|
|
|
|
// a new one will be created.
|
2016-03-16 17:52:34 -04:00
|
|
|
v, err := daemon.volumes.CreateWithRef(mp.Name, volumeDriver, container.ID, nil, nil)
|
2015-09-09 22:23:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME Windows: This code block is present in the Linux version and
|
|
|
|
// allows the contents to be copied to the container FS prior to it
|
2015-12-13 11:00:39 -05:00
|
|
|
// being started. However, the function utilizes the FollowSymLinkInScope
|
2015-09-09 22:23:06 -04:00
|
|
|
// path which does not cope with Windows volume-style file paths. There
|
2015-12-13 11:00:39 -05:00
|
|
|
// is a separate effort to resolve this (@swernli), so this processing
|
2015-09-09 22:23:06 -04:00
|
|
|
// is deferred for now. A case where this would be useful is when
|
|
|
|
// a dockerfile includes a VOLUME statement, but something is created
|
|
|
|
// in that directory during the dockerfile processing. What this means
|
2016-04-06 15:01:29 -04:00
|
|
|
// on Windows for TP5 is that in that scenario, the contents will not
|
2015-09-09 22:23:06 -04:00
|
|
|
// copied, but that's (somewhat) OK as HCS will bomb out soon after
|
|
|
|
// at it doesn't support mapped directories which have contents in the
|
|
|
|
// destination path anyway.
|
|
|
|
//
|
|
|
|
// Example for repro later:
|
|
|
|
// FROM windowsservercore
|
|
|
|
// RUN mkdir c:\myvol
|
|
|
|
// RUN copy c:\windows\system32\ntdll.dll c:\myvol
|
|
|
|
// VOLUME "c:\myvol"
|
|
|
|
//
|
|
|
|
// Then
|
|
|
|
// docker build -t vol .
|
|
|
|
// docker run -it --rm vol cmd <-- This is where HCS will error out.
|
|
|
|
//
|
|
|
|
// // never attempt to copy existing content in a container FS to a shared volume
|
|
|
|
// if v.DriverName() == volume.DefaultDriverName {
|
2015-11-12 14:55:17 -05:00
|
|
|
// if err := container.CopyImagePathContent(v, mp.Destination); err != nil {
|
2015-09-09 22:23:06 -04:00
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Add it to container.MountPoints
|
2015-11-12 14:55:17 -05:00
|
|
|
container.AddMountPointWithVolume(mp.Destination, v, mp.RW)
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
return nil
|
|
|
|
}
|