create_unix.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "path/filepath"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/container"
  8. derr "github.com/docker/docker/errors"
  9. "github.com/docker/docker/pkg/stringid"
  10. containertypes "github.com/docker/engine-api/types/container"
  11. "github.com/opencontainers/runc/libcontainer/label"
  12. )
  13. // createContainerPlatformSpecificSettings performs platform specific container create functionality
  14. func (daemon *Daemon) createContainerPlatformSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig) error {
  15. if err := daemon.Mount(container); err != nil {
  16. return err
  17. }
  18. defer daemon.Unmount(container)
  19. if err := container.SetupWorkingDirectory(); err != nil {
  20. return err
  21. }
  22. for spec := range config.Volumes {
  23. name := stringid.GenerateNonCryptoID()
  24. destination := filepath.Clean(spec)
  25. // Skip volumes for which we already have something mounted on that
  26. // destination because of a --volume-from.
  27. if container.IsDestinationMounted(destination) {
  28. continue
  29. }
  30. path, err := container.GetResourcePath(destination)
  31. if err != nil {
  32. return err
  33. }
  34. stat, err := os.Stat(path)
  35. if err == nil && !stat.IsDir() {
  36. return derr.ErrorCodeMountOverFile.WithArgs(path)
  37. }
  38. v, err := daemon.volumes.CreateWithRef(name, hostConfig.VolumeDriver, container.ID, nil)
  39. if err != nil {
  40. return err
  41. }
  42. if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
  43. return err
  44. }
  45. container.AddMountPointWithVolume(destination, v, true)
  46. }
  47. return daemon.populateVolumes(container)
  48. }
  49. // populateVolumes copies data from the container's rootfs into the volume for non-binds.
  50. // this is only called when the container is created.
  51. func (daemon *Daemon) populateVolumes(c *container.Container) error {
  52. for _, mnt := range c.MountPoints {
  53. // skip binds and volumes referenced by other containers (ie, volumes-from)
  54. if mnt.Driver == "" || mnt.Volume == nil || len(daemon.volumes.Refs(mnt.Volume)) > 1 {
  55. continue
  56. }
  57. logrus.Debugf("copying image data from %s:%s, to %s", c.ID, mnt.Destination, mnt.Name)
  58. if err := c.CopyImagePathContent(mnt.Volume, mnt.Destination); err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }