create_unix.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //go:build !windows
  2. // +build !windows
  3. package daemon // import "github.com/docker/docker/daemon"
  4. import (
  5. "context"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. mounttypes "github.com/docker/docker/api/types/mount"
  11. "github.com/docker/docker/container"
  12. "github.com/docker/docker/oci"
  13. "github.com/docker/docker/pkg/stringid"
  14. volumeopts "github.com/docker/docker/volume/service/opts"
  15. "github.com/opencontainers/selinux/go-selinux/label"
  16. "github.com/sirupsen/logrus"
  17. )
  18. // createContainerOSSpecificSettings performs host-OS specific container create functionality
  19. func (daemon *Daemon) createContainerOSSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig) error {
  20. if err := daemon.Mount(container); err != nil {
  21. return err
  22. }
  23. defer daemon.Unmount(container)
  24. rootIDs := daemon.idMapping.RootPair()
  25. if err := container.SetupWorkingDirectory(rootIDs); err != nil {
  26. return err
  27. }
  28. // Set the default masked and readonly paths with regard to the host config options if they are not set.
  29. if hostConfig.MaskedPaths == nil && !hostConfig.Privileged {
  30. hostConfig.MaskedPaths = oci.DefaultSpec().Linux.MaskedPaths // Set it to the default if nil
  31. container.HostConfig.MaskedPaths = hostConfig.MaskedPaths
  32. }
  33. if hostConfig.ReadonlyPaths == nil && !hostConfig.Privileged {
  34. hostConfig.ReadonlyPaths = oci.DefaultSpec().Linux.ReadonlyPaths // Set it to the default if nil
  35. container.HostConfig.ReadonlyPaths = hostConfig.ReadonlyPaths
  36. }
  37. for spec := range config.Volumes {
  38. name := stringid.GenerateRandomID()
  39. destination := filepath.Clean(spec)
  40. // Skip volumes for which we already have something mounted on that
  41. // destination because of a --volume-from.
  42. if container.HasMountFor(destination) {
  43. logrus.WithField("container", container.ID).WithField("destination", spec).Debug("mountpoint already exists, skipping anonymous volume")
  44. // Not an error, this could easily have come from the image config.
  45. continue
  46. }
  47. path, err := container.GetResourcePath(destination)
  48. if err != nil {
  49. return err
  50. }
  51. stat, err := os.Stat(path)
  52. if err == nil && !stat.IsDir() {
  53. return fmt.Errorf("cannot mount volume over existing file, file exists %s", path)
  54. }
  55. v, err := daemon.volumes.Create(context.TODO(), name, hostConfig.VolumeDriver, volumeopts.WithCreateReference(container.ID))
  56. if err != nil {
  57. return err
  58. }
  59. if err := label.Relabel(v.Mountpoint, container.MountLabel, true); err != nil {
  60. return err
  61. }
  62. container.AddMountPointWithVolume(destination, &volumeWrapper{v: v, s: daemon.volumes}, true)
  63. }
  64. return daemon.populateVolumes(container)
  65. }
  66. // populateVolumes copies data from the container's rootfs into the volume for non-binds.
  67. // this is only called when the container is created.
  68. func (daemon *Daemon) populateVolumes(c *container.Container) error {
  69. for _, mnt := range c.MountPoints {
  70. if mnt.Volume == nil {
  71. continue
  72. }
  73. if mnt.Type != mounttypes.TypeVolume || !mnt.CopyData {
  74. continue
  75. }
  76. logrus.Debugf("copying image data from %s:%s, to %s", c.ID, mnt.Destination, mnt.Name)
  77. if err := c.CopyImagePathContent(mnt.Volume, mnt.Destination); err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }