2018-02-05 21:05:59 +00:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2017-03-15 22:28:06 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2017-11-07 02:21:10 +00:00
|
|
|
"runtime"
|
|
|
|
|
2019-04-26 22:12:43 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2017-05-05 22:52:11 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2017-03-15 22:28:06 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-25 21:03:29 +00:00
|
|
|
dockerimage "github.com/docker/docker/image"
|
2023-05-08 09:57:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-03-15 22:28:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
type getAndMountFunc func(context.Context, string, bool, *ocispec.Platform) (builder.Image, builder.ROLayer, error)
|
2017-05-05 22:52:11 +00:00
|
|
|
|
|
|
|
// imageSources mounts images and provides a cache for mounted images. It tracks
|
|
|
|
// all images so they can be unmounted at the end of the build.
|
|
|
|
type imageSources struct {
|
|
|
|
byImageID map[string]*imageMount
|
2017-06-20 00:15:23 +00:00
|
|
|
mounts []*imageMount
|
2017-05-05 22:52:11 +00:00
|
|
|
getImage getAndMountFunc
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:40:06 +00:00
|
|
|
func newImageSources(options builderOptions) *imageSources {
|
2023-05-08 09:57:52 +00:00
|
|
|
getAndMount := func(ctx context.Context, idOrRef string, localOnly bool, platform *ocispec.Platform) (builder.Image, builder.ROLayer, error) {
|
2017-06-20 00:15:23 +00:00
|
|
|
pullOption := backend.PullOptionNoPull
|
|
|
|
if !localOnly {
|
|
|
|
if options.Options.PullParent {
|
|
|
|
pullOption = backend.PullOptionForcePull
|
|
|
|
} else {
|
|
|
|
pullOption = backend.PullOptionPreferLocal
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 22:52:11 +00:00
|
|
|
return options.Backend.GetImageAndReleasableLayer(ctx, idOrRef, backend.GetImageAndLayerOptions{
|
2017-06-20 00:15:23 +00:00
|
|
|
PullOption: pullOption,
|
2017-05-05 22:52:11 +00:00
|
|
|
AuthConfig: options.Options.AuthConfigs,
|
|
|
|
Output: options.ProgressWriter.Output,
|
2018-06-26 21:49:33 +00:00
|
|
|
Platform: platform,
|
2017-05-05 22:52:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &imageSources{
|
|
|
|
byImageID: make(map[string]*imageMount),
|
|
|
|
getImage: getAndMount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
func (m *imageSources) Get(ctx context.Context, idOrRef string, localOnly bool, platform *ocispec.Platform) (*imageMount, error) {
|
2017-05-05 22:52:11 +00:00
|
|
|
if im, ok := m.byImageID[idOrRef]; ok {
|
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 11:40:06 +00:00
|
|
|
image, layer, err := m.getImage(ctx, idOrRef, localOnly, platform)
|
2017-05-05 22:52:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
im := newImageMount(image, layer)
|
2019-04-26 22:12:43 +00:00
|
|
|
m.Add(im, platform)
|
2017-05-05 22:52:11 +00:00
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *imageSources) Unmount() (retErr error) {
|
2017-06-20 00:15:23 +00:00
|
|
|
for _, im := range m.mounts {
|
2017-05-25 21:03:29 +00:00
|
|
|
if err := im.unmount(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Error(err)
|
2017-05-25 21:03:29 +00:00
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 22:28:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:57:52 +00:00
|
|
|
func (m *imageSources) Add(im *imageMount, platform *ocispec.Platform) {
|
2017-05-25 21:03:29 +00:00
|
|
|
switch im.image {
|
|
|
|
case nil:
|
2019-04-26 22:12:43 +00:00
|
|
|
// Set the platform for scratch images
|
|
|
|
if platform == nil {
|
|
|
|
p := platforms.DefaultSpec()
|
|
|
|
platform = &p
|
|
|
|
}
|
|
|
|
|
2017-11-07 02:21:10 +00:00
|
|
|
// Windows does not support scratch except for LCOW
|
2019-04-26 22:12:43 +00:00
|
|
|
os := platform.OS
|
2017-11-07 02:21:10 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
os = "linux"
|
|
|
|
}
|
2019-04-26 22:12:43 +00:00
|
|
|
|
|
|
|
im.image = &dockerimage.Image{V1Image: dockerimage.V1Image{
|
|
|
|
OS: os,
|
|
|
|
Architecture: platform.Architecture,
|
|
|
|
Variant: platform.Variant,
|
|
|
|
}}
|
2017-05-25 21:03:29 +00:00
|
|
|
default:
|
|
|
|
m.byImageID[im.image.ImageID()] = im
|
|
|
|
}
|
2017-06-20 00:15:23 +00:00
|
|
|
m.mounts = append(m.mounts, im)
|
2017-05-25 21:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 01:36:28 +00:00
|
|
|
// imageMount is a reference to an image that can be used as a builder.Source
|
2017-03-23 00:49:39 +00:00
|
|
|
type imageMount struct {
|
2019-08-06 17:30:49 +00:00
|
|
|
image builder.Image
|
|
|
|
layer builder.ROLayer
|
2017-03-28 01:36:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
func newImageMount(image builder.Image, layer builder.ROLayer) *imageMount {
|
2017-05-05 22:52:11 +00:00
|
|
|
im := &imageMount{image: image, layer: layer}
|
2017-03-28 01:36:28 +00:00
|
|
|
return im
|
2017-03-23 00:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imageMount) unmount() error {
|
2017-05-05 17:05:25 +00:00
|
|
|
if im.layer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-28 01:36:28 +00:00
|
|
|
if err := im.layer.Release(); err != nil {
|
2017-05-05 22:52:11 +00:00
|
|
|
return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
|
2017-03-23 00:49:39 +00:00
|
|
|
}
|
2017-05-25 21:03:29 +00:00
|
|
|
im.layer = nil
|
2017-03-23 00:49:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-05 22:52:11 +00:00
|
|
|
func (im *imageMount) Image() builder.Image {
|
|
|
|
return im.image
|
2017-03-23 01:36:08 +00:00
|
|
|
}
|
2017-05-07 18:37:46 +00:00
|
|
|
|
2018-02-16 21:50:57 +00:00
|
|
|
func (im *imageMount) NewRWLayer() (builder.RWLayer, error) {
|
|
|
|
return im.layer.NewRWLayer()
|
2017-05-07 18:37:46 +00:00
|
|
|
}
|
2017-05-14 18:18:48 +00:00
|
|
|
|
2017-05-25 21:03:29 +00:00
|
|
|
func (im *imageMount) ImageID() string {
|
|
|
|
return im.image.ImageID()
|
2017-05-14 18:18:48 +00:00
|
|
|
}
|