create_unix.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. for spec := range config.Volumes {
  16. name := stringid.GenerateNonCryptoID()
  17. destination := filepath.Clean(spec)
  18. // Skip volumes for which we already have something mounted on that
  19. // destination because of a --volume-from.
  20. if container.isDestinationMounted(destination) {
  21. continue
  22. }
  23. path, err := container.GetResourcePath(destination)
  24. if err != nil {
  25. return err
  26. }
  27. stat, err := os.Stat(path)
  28. if err == nil && !stat.IsDir() {
  29. return derr.ErrorCodeMountOverFile.WithArgs(path)
  30. }
  31. volumeDriver := hostConfig.VolumeDriver
  32. if destination != "" && img != nil {
  33. if _, ok := img.ContainerConfig.Volumes[destination]; ok {
  34. // check for whether bind is not specified and then set to local
  35. if _, ok := container.MountPoints[destination]; !ok {
  36. volumeDriver = volume.DefaultDriverName
  37. }
  38. }
  39. }
  40. v, err := container.daemon.createVolume(name, volumeDriver, nil)
  41. if err != nil {
  42. return err
  43. }
  44. if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
  45. return err
  46. }
  47. // never attempt to copy existing content in a container FS to a shared volume
  48. if v.DriverName() == volume.DefaultDriverName {
  49. if err := container.copyImagePathContent(v, destination); err != nil {
  50. return err
  51. }
  52. }
  53. container.addMountPointWithVolume(destination, v, true)
  54. }
  55. return nil
  56. }