create_unix.go 2.1 KB

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