create_unix.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // +build !windows
  2. package daemon
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/docker/docker/pkg/stringid"
  9. "github.com/docker/docker/runconfig"
  10. "github.com/opencontainers/runc/libcontainer/label"
  11. )
  12. // createContainerPlatformSpecificSettings performs platform specific container create functionality
  13. func createContainerPlatformSpecificSettings(container *Container, config *runconfig.Config) error {
  14. for spec := range config.Volumes {
  15. var (
  16. name, destination string
  17. parts = strings.Split(spec, ":")
  18. )
  19. switch len(parts) {
  20. case 2:
  21. name, destination = parts[0], filepath.Clean(parts[1])
  22. default:
  23. name = stringid.GenerateNonCryptoID()
  24. destination = filepath.Clean(parts[0])
  25. }
  26. // Skip volumes for which we already have something mounted on that
  27. // destination because of a --volume-from.
  28. if container.isDestinationMounted(destination) {
  29. continue
  30. }
  31. path, err := container.GetResourcePath(destination)
  32. if err != nil {
  33. return err
  34. }
  35. stat, err := os.Stat(path)
  36. if err == nil && !stat.IsDir() {
  37. return fmt.Errorf("cannot mount volume over existing file, file exists %s", path)
  38. }
  39. v, err := createVolume(name, config.VolumeDriver)
  40. if err != nil {
  41. return err
  42. }
  43. if err := label.Relabel(v.Path(), container.MountLabel, "z"); err != nil {
  44. return err
  45. }
  46. if err := container.copyImagePathContent(v, destination); err != nil {
  47. return err
  48. }
  49. container.addMountPointWithVolume(destination, v, true)
  50. }
  51. return nil
  52. }