create_unix.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // +build !windows
  2. package daemon
  3. import (
  4. "os"
  5. "path/filepath"
  6. containertypes "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/container"
  8. derr "github.com/docker/docker/errors"
  9. "github.com/docker/docker/image"
  10. "github.com/docker/docker/pkg/stringid"
  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 *containertypes.Config, hostConfig *containertypes.HostConfig, img *image.Image) error {
  16. if err := daemon.Mount(container); err != nil {
  17. return err
  18. }
  19. defer daemon.Unmount(container)
  20. if err := container.SetupWorkingDirectory(); err != nil {
  21. return err
  22. }
  23. for spec := range config.Volumes {
  24. name := stringid.GenerateNonCryptoID()
  25. destination := filepath.Clean(spec)
  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 derr.ErrorCodeMountOverFile.WithArgs(path)
  38. }
  39. volumeDriver := hostConfig.VolumeDriver
  40. if destination != "" && img != nil {
  41. if _, ok := img.ContainerConfig.Volumes[destination]; ok {
  42. // check for whether bind is not specified and then set to local
  43. if _, ok := container.MountPoints[destination]; !ok {
  44. volumeDriver = volume.DefaultDriverName
  45. }
  46. }
  47. }
  48. v, err := daemon.volumes.CreateWithRef(name, volumeDriver, container.ID, nil)
  49. if err != nil {
  50. return err
  51. }
  52. if err := label.Relabel(v.Path(), container.MountLabel, true); err != nil {
  53. return err
  54. }
  55. // never attempt to copy existing content in a container FS to a shared volume
  56. if v.DriverName() == volume.DefaultDriverName {
  57. if err := container.CopyImagePathContent(v, destination); err != nil {
  58. return err
  59. }
  60. }
  61. container.AddMountPointWithVolume(destination, v, true)
  62. }
  63. return nil
  64. }