create_unix.go 3.1 KB

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