imagecontext.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "context"
  4. "runtime"
  5. "github.com/docker/docker/api/types/backend"
  6. "github.com/docker/docker/builder"
  7. dockerimage "github.com/docker/docker/image"
  8. specs "github.com/opencontainers/image-spec/specs-go/v1"
  9. "github.com/pkg/errors"
  10. "github.com/sirupsen/logrus"
  11. )
  12. type getAndMountFunc func(string, bool, *specs.Platform) (builder.Image, builder.ROLayer, error)
  13. // imageSources mounts images and provides a cache for mounted images. It tracks
  14. // all images so they can be unmounted at the end of the build.
  15. type imageSources struct {
  16. byImageID map[string]*imageMount
  17. mounts []*imageMount
  18. getImage getAndMountFunc
  19. }
  20. func newImageSources(ctx context.Context, options builderOptions) *imageSources {
  21. getAndMount := func(idOrRef string, localOnly bool, platform *specs.Platform) (builder.Image, builder.ROLayer, error) {
  22. pullOption := backend.PullOptionNoPull
  23. if !localOnly {
  24. if options.Options.PullParent {
  25. pullOption = backend.PullOptionForcePull
  26. } else {
  27. pullOption = backend.PullOptionPreferLocal
  28. }
  29. }
  30. return options.Backend.GetImageAndReleasableLayer(ctx, idOrRef, backend.GetImageAndLayerOptions{
  31. PullOption: pullOption,
  32. AuthConfig: options.Options.AuthConfigs,
  33. Output: options.ProgressWriter.Output,
  34. Platform: platform,
  35. })
  36. }
  37. return &imageSources{
  38. byImageID: make(map[string]*imageMount),
  39. getImage: getAndMount,
  40. }
  41. }
  42. func (m *imageSources) Get(idOrRef string, localOnly bool, platform *specs.Platform) (*imageMount, error) {
  43. if im, ok := m.byImageID[idOrRef]; ok {
  44. return im, nil
  45. }
  46. image, layer, err := m.getImage(idOrRef, localOnly, platform)
  47. if err != nil {
  48. return nil, err
  49. }
  50. im := newImageMount(image, layer)
  51. m.Add(im)
  52. return im, nil
  53. }
  54. func (m *imageSources) Unmount() (retErr error) {
  55. for _, im := range m.mounts {
  56. if err := im.unmount(); err != nil {
  57. logrus.Error(err)
  58. retErr = err
  59. }
  60. }
  61. return
  62. }
  63. func (m *imageSources) Add(im *imageMount) {
  64. switch im.image {
  65. case nil:
  66. // set the OS for scratch images
  67. os := runtime.GOOS
  68. // Windows does not support scratch except for LCOW
  69. if runtime.GOOS == "windows" {
  70. os = "linux"
  71. }
  72. im.image = &dockerimage.Image{V1Image: dockerimage.V1Image{OS: os}}
  73. default:
  74. m.byImageID[im.image.ImageID()] = im
  75. }
  76. m.mounts = append(m.mounts, im)
  77. }
  78. // imageMount is a reference to an image that can be used as a builder.Source
  79. type imageMount struct {
  80. image builder.Image
  81. source builder.Source
  82. layer builder.ROLayer
  83. }
  84. func newImageMount(image builder.Image, layer builder.ROLayer) *imageMount {
  85. im := &imageMount{image: image, layer: layer}
  86. return im
  87. }
  88. func (im *imageMount) unmount() error {
  89. if im.layer == nil {
  90. return nil
  91. }
  92. if err := im.layer.Release(); err != nil {
  93. return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
  94. }
  95. im.layer = nil
  96. return nil
  97. }
  98. func (im *imageMount) Image() builder.Image {
  99. return im.image
  100. }
  101. func (im *imageMount) NewRWLayer() (builder.RWLayer, error) {
  102. return im.layer.NewRWLayer()
  103. }
  104. func (im *imageMount) ImageID() string {
  105. return im.image.ImageID()
  106. }