create_unix.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // +build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "path/filepath"
  6. "github.com/docker/docker/container"
  7. derr "github.com/docker/docker/errors"
  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 (daemon *Daemon) createContainerPlatformSpecificSettings(container *container.Container, config *runconfig.Config, hostConfig *runconfig.HostConfig, img *image.Image) error {
  16. if err := daemon.Mount(container); err != nil {
  17. return err
  18. }
  19. defer daemon.Unmount(container)
  20. for spec := range config.Volumes {
  21. name := stringid.GenerateNonCryptoID()
  22. destination := filepath.Clean(spec)
  23. // Skip volumes for which we already have something mounted on that
  24. // destination because of a --volume-from.
  25. if container.IsDestinationMounted(destination) {
  26. continue
  27. }
  28. path, err := container.GetResourcePath(destination)
  29. if err != nil {
  30. return err
  31. }
  32. stat, err := os.Stat(path)
  33. if err == nil && !stat.IsDir() {
  34. return derr.ErrorCodeMountOverFile.WithArgs(path)
  35. }
  36. volumeDriver := hostConfig.VolumeDriver
  37. if destination != "" && img != nil {
  38. if _, ok := img.ContainerConfig.Volumes[destination]; ok {
  39. // check for whether bind is not specified and then set to local
  40. if _, ok := container.MountPoints[destination]; !ok {
  41. volumeDriver = volume.DefaultDriverName
  42. }
  43. }
  44. }
  45. v, err := daemon.createVolume(name, volumeDriver, nil)
  46. if err != nil {
  47. return err
  48. }
  49. if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
  50. return err
  51. }
  52. // never attempt to copy existing content in a container FS to a shared volume
  53. if v.DriverName() == volume.DefaultDriverName {
  54. if err := container.CopyImagePathContent(v, destination); err != nil {
  55. return err
  56. }
  57. }
  58. container.AddMountPointWithVolume(destination, v, true)
  59. }
  60. return nil
  61. }