create_unix.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "github.com/containerd/log"
  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/errdefs"
  13. "github.com/docker/docker/oci"
  14. volumemounts "github.com/docker/docker/volume/mounts"
  15. volumeopts "github.com/docker/docker/volume/service/opts"
  16. "github.com/opencontainers/selinux/go-selinux/label"
  17. "github.com/pkg/errors"
  18. )
  19. // createContainerOSSpecificSettings performs host-OS specific container create functionality
  20. func (daemon *Daemon) createContainerOSSpecificSettings(container *container.Container, config *containertypes.Config, hostConfig *containertypes.HostConfig) error {
  21. if err := daemon.Mount(container); err != nil {
  22. return err
  23. }
  24. defer daemon.Unmount(container)
  25. rootIDs := daemon.idMapping.RootPair()
  26. if err := container.SetupWorkingDirectory(rootIDs); err != nil {
  27. return err
  28. }
  29. // Set the default masked and readonly paths with regard to the host config options if they are not set.
  30. if hostConfig.MaskedPaths == nil && !hostConfig.Privileged {
  31. hostConfig.MaskedPaths = oci.DefaultSpec().Linux.MaskedPaths // Set it to the default if nil
  32. container.HostConfig.MaskedPaths = hostConfig.MaskedPaths
  33. }
  34. if hostConfig.ReadonlyPaths == nil && !hostConfig.Privileged {
  35. hostConfig.ReadonlyPaths = oci.DefaultSpec().Linux.ReadonlyPaths // Set it to the default if nil
  36. container.HostConfig.ReadonlyPaths = hostConfig.ReadonlyPaths
  37. }
  38. for spec := range config.Volumes {
  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. log.G(context.TODO()).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(), "", 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. if err := daemon.populateVolume(c, mnt); err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. func (daemon *Daemon) populateVolume(c *container.Container, mnt *volumemounts.MountPoint) error {
  83. ctrDestPath, err := c.GetResourcePath(mnt.Destination)
  84. if err != nil {
  85. return err
  86. }
  87. if _, err := os.Stat(ctrDestPath); err != nil {
  88. if os.IsNotExist(err) {
  89. return nil
  90. }
  91. return err
  92. }
  93. volumePath, cleanup, err := mnt.Setup(c.MountLabel, daemon.idMapping.RootPair(), nil)
  94. if err != nil {
  95. if errdefs.IsNotFound(err) {
  96. return nil
  97. }
  98. log.G(context.TODO()).WithError(err).Debugf("can't copy data from %s:%s, to %s", c.ID, mnt.Destination, volumePath)
  99. return errors.Wrapf(err, "failed to populate volume")
  100. }
  101. defer mnt.Cleanup()
  102. defer cleanup()
  103. log.G(context.TODO()).Debugf("copying image data from %s:%s, to %s", c.ID, mnt.Destination, volumePath)
  104. if err := c.CopyImagePathContent(volumePath, ctrDestPath); err != nil {
  105. return err
  106. }
  107. return nil
  108. }