imagecontext.go 3.0 KB

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