create_unix.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // +build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "path/filepath"
  6. derr "github.com/docker/docker/errors"
  7. "github.com/docker/docker/image"
  8. "github.com/docker/docker/pkg/stringid"
  9. "github.com/docker/docker/runconfig"
  10. "github.com/docker/docker/volume"
  11. "github.com/opencontainers/runc/libcontainer/label"
  12. )
  13. // createContainerPlatformSpecificSettings performs platform specific container create functionality
  14. func createContainerPlatformSpecificSettings(container *Container, config *runconfig.Config, hostConfig *runconfig.HostConfig, img *image.Image) error {
  15. var name, destination string
  16. for spec := range config.Volumes {
  17. name = stringid.GenerateNonCryptoID()
  18. destination = filepath.Clean(spec)
  19. // Skip volumes for which we already have something mounted on that
  20. // destination because of a --volume-from.
  21. if container.isDestinationMounted(destination) {
  22. continue
  23. }
  24. path, err := container.GetResourcePath(destination)
  25. if err != nil {
  26. return err
  27. }
  28. stat, err := os.Stat(path)
  29. if err == nil && !stat.IsDir() {
  30. return derr.ErrorCodeMountOverFile.WithArgs(path)
  31. }
  32. volumeDriver := hostConfig.VolumeDriver
  33. if destination != "" && img != nil {
  34. if _, ok := img.ContainerConfig.Volumes[destination]; ok {
  35. // check for whether bind is not specified and then set to local
  36. if _, ok := container.MountPoints[destination]; !ok {
  37. volumeDriver = volume.DefaultDriverName
  38. }
  39. }
  40. }
  41. v, err := container.daemon.createVolume(name, volumeDriver, nil)
  42. if err != nil {
  43. return err
  44. }
  45. if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
  46. return err
  47. }
  48. // never attempt to copy existing content in a container FS to a shared volume
  49. if v.DriverName() == volume.DefaultDriverName {
  50. if err := container.copyImagePathContent(v, destination); err != nil {
  51. return err
  52. }
  53. }
  54. container.addMountPointWithVolume(destination, v, true)
  55. }
  56. return nil
  57. }