2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-04-27 16:25:38 +00:00
|
|
|
|
2015-06-03 16:26:41 +00:00
|
|
|
import (
|
2023-06-09 18:43:45 +00:00
|
|
|
"context"
|
2015-11-03 19:25:22 +00:00
|
|
|
"sort"
|
|
|
|
|
2023-06-09 18:43:45 +00:00
|
|
|
"github.com/containerd/log"
|
2018-01-18 21:55:27 +00:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2023-06-09 18:43:45 +00:00
|
|
|
"github.com/docker/docker/internal/cleanups"
|
2023-07-27 12:56:28 +00:00
|
|
|
"github.com/docker/docker/internal/compatcontext"
|
2017-05-19 22:06:46 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2018-04-17 20:50:28 +00:00
|
|
|
volumemounts "github.com/docker/docker/volume/mounts"
|
2015-06-03 16:26:41 +00:00
|
|
|
)
|
2015-05-19 20:05:25 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
// setupMounts configures the mount points for a container by appending each
|
2016-03-18 19:43:17 +00:00
|
|
|
// of the configured mounts on the container to the OCI mount structure
|
|
|
|
// which will ultimately be passed into the oci runtime during container creation.
|
2017-02-16 12:08:57 +00:00
|
|
|
// It also ensures each of the mounts are lexicographically sorted.
|
2023-06-09 18:43:45 +00:00
|
|
|
//
|
|
|
|
// The cleanup function should be called as soon as the container has been
|
|
|
|
// started.
|
|
|
|
//
|
2016-03-18 18:53:27 +00:00
|
|
|
// BUGBUG TODO Windows containerd. This would be much better if it returned
|
2016-09-27 17:26:59 +00:00
|
|
|
// an array of runtime spec mounts, not container mounts. Then no need to
|
2016-03-18 18:53:27 +00:00
|
|
|
// do multiple transitions.
|
2023-07-27 12:56:28 +00:00
|
|
|
func (daemon *Daemon) setupMounts(ctx context.Context, c *container.Container) ([]container.Mount, func(context.Context) error, error) {
|
2023-06-09 18:43:45 +00:00
|
|
|
cleanups := cleanups.Composite{}
|
|
|
|
defer func() {
|
2023-07-27 12:56:28 +00:00
|
|
|
if err := cleanups.Call(compatcontext.WithoutCancel(ctx)); err != nil {
|
|
|
|
log.G(ctx).WithError(err).Warn("failed to cleanup temporary mounts created by MountPoint.Setup")
|
2023-06-09 18:43:45 +00:00
|
|
|
}
|
|
|
|
}()
|
2016-03-18 18:53:27 +00:00
|
|
|
|
|
|
|
var mnts []container.Mount
|
2018-04-17 20:50:28 +00:00
|
|
|
for _, mount := range c.MountPoints { // type is volumemounts.MountPoint
|
2016-03-18 18:53:27 +00:00
|
|
|
if err := daemon.lazyInitializeVolume(c.ID, mount); err != nil {
|
2023-06-09 18:43:45 +00:00
|
|
|
return nil, nil, err
|
2015-12-09 19:39:31 +00:00
|
|
|
}
|
2023-07-27 12:56:28 +00:00
|
|
|
s, c, err := mount.Setup(ctx, c.MountLabel, idtools.Identity{}, nil)
|
2016-09-19 20:57:55 +00:00
|
|
|
if err != nil {
|
2023-06-09 18:43:45 +00:00
|
|
|
return nil, nil, err
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
2023-06-09 18:43:45 +00:00
|
|
|
cleanups.Add(c)
|
2016-09-19 20:57:55 +00:00
|
|
|
|
2016-03-18 18:53:27 +00:00
|
|
|
mnts = append(mnts, container.Mount{
|
2015-09-10 02:23:06 +00:00
|
|
|
Source: s,
|
|
|
|
Destination: mount.Destination,
|
|
|
|
Writable: mount.RW,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(mounts(mnts))
|
2023-06-09 18:43:45 +00:00
|
|
|
return mnts, cleanups.Release(), nil
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-06-08 20:45:28 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
// setBindModeIfNull is platform specific processing which is a no-op on
|
|
|
|
// Windows.
|
2018-04-17 20:50:28 +00:00
|
|
|
func setBindModeIfNull(bind *volumemounts.MountPoint) {
|
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 18:25:35 +00:00
|
|
|
return
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
2018-01-18 21:55:27 +00:00
|
|
|
|
|
|
|
func (daemon *Daemon) validateBindDaemonRoot(m mount.Mount) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|