create_unix.go 2.6 KB

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