2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2017-03-15 18:28:06 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-11-06 21:21:10 -05:00
|
|
|
"runtime"
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-25 17:03:29 -04:00
|
|
|
dockerimage "github.com/docker/docker/image"
|
2018-06-26 17:49:33 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-03-15 18:28:06 -04:00
|
|
|
)
|
|
|
|
|
2018-06-26 17:49:33 -04:00
|
|
|
type getAndMountFunc func(string, bool, *specs.Platform) (builder.Image, builder.ROLayer, error)
|
2017-05-05 18:52:11 -04: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-19 20:15:23 -04:00
|
|
|
mounts []*imageMount
|
2017-05-05 18:52:11 -04:00
|
|
|
getImage getAndMountFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func newImageSources(ctx context.Context, options builderOptions) *imageSources {
|
2018-06-26 17:49:33 -04:00
|
|
|
getAndMount := func(idOrRef string, localOnly bool, platform *specs.Platform) (builder.Image, builder.ROLayer, error) {
|
2017-06-19 20:15:23 -04:00
|
|
|
pullOption := backend.PullOptionNoPull
|
|
|
|
if !localOnly {
|
|
|
|
if options.Options.PullParent {
|
|
|
|
pullOption = backend.PullOptionForcePull
|
|
|
|
} else {
|
|
|
|
pullOption = backend.PullOptionPreferLocal
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return options.Backend.GetImageAndReleasableLayer(ctx, idOrRef, backend.GetImageAndLayerOptions{
|
2017-06-19 20:15:23 -04:00
|
|
|
PullOption: pullOption,
|
2017-05-05 18:52:11 -04:00
|
|
|
AuthConfig: options.Options.AuthConfigs,
|
|
|
|
Output: options.ProgressWriter.Output,
|
2018-06-26 17:49:33 -04:00
|
|
|
Platform: platform,
|
2017-05-05 18:52:11 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &imageSources{
|
|
|
|
byImageID: make(map[string]*imageMount),
|
|
|
|
getImage: getAndMount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:49:33 -04:00
|
|
|
func (m *imageSources) Get(idOrRef string, localOnly bool, platform *specs.Platform) (*imageMount, error) {
|
2017-05-05 18:52:11 -04:00
|
|
|
if im, ok := m.byImageID[idOrRef]; ok {
|
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:49:33 -04:00
|
|
|
image, layer, err := m.getImage(idOrRef, localOnly, platform)
|
2017-05-05 18:52:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
im := newImageMount(image, layer)
|
2017-05-25 17:03:29 -04:00
|
|
|
m.Add(im)
|
2017-05-05 18:52:11 -04:00
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *imageSources) Unmount() (retErr error) {
|
2017-06-19 20:15:23 -04:00
|
|
|
for _, im := range m.mounts {
|
2017-05-25 17:03:29 -04:00
|
|
|
if err := im.unmount(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 18:28:06 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:03:29 -04:00
|
|
|
func (m *imageSources) Add(im *imageMount) {
|
|
|
|
switch im.image {
|
|
|
|
case nil:
|
2017-11-06 21:21:10 -05:00
|
|
|
// set the OS for scratch images
|
|
|
|
os := runtime.GOOS
|
|
|
|
// Windows does not support scratch except for LCOW
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
os = "linux"
|
|
|
|
}
|
|
|
|
im.image = &dockerimage.Image{V1Image: dockerimage.V1Image{OS: os}}
|
2017-05-25 17:03:29 -04:00
|
|
|
default:
|
|
|
|
m.byImageID[im.image.ImageID()] = im
|
|
|
|
}
|
2017-06-19 20:15:23 -04:00
|
|
|
m.mounts = append(m.mounts, im)
|
2017-05-25 17:03:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 21:36:28 -04:00
|
|
|
// imageMount is a reference to an image that can be used as a builder.Source
|
2017-03-22 20:49:39 -04:00
|
|
|
type imageMount struct {
|
2017-05-05 18:52:11 -04:00
|
|
|
image builder.Image
|
|
|
|
source builder.Source
|
2018-02-16 16:50:57 -05:00
|
|
|
layer builder.ROLayer
|
2017-03-27 21:36:28 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 16:50:57 -05:00
|
|
|
func newImageMount(image builder.Image, layer builder.ROLayer) *imageMount {
|
2017-05-05 18:52:11 -04:00
|
|
|
im := &imageMount{image: image, layer: layer}
|
2017-03-27 21:36:28 -04:00
|
|
|
return im
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imageMount) unmount() error {
|
2017-05-05 13:05:25 -04:00
|
|
|
if im.layer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-27 21:36:28 -04:00
|
|
|
if err := im.layer.Release(); err != nil {
|
2017-05-05 18:52:11 -04:00
|
|
|
return errors.Wrapf(err, "failed to unmount previous build image %s", im.image.ImageID())
|
2017-03-22 20:49:39 -04:00
|
|
|
}
|
2017-05-25 17:03:29 -04:00
|
|
|
im.layer = nil
|
2017-03-22 20:49:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
func (im *imageMount) Image() builder.Image {
|
|
|
|
return im.image
|
2017-03-22 21:36:08 -04:00
|
|
|
}
|
2017-05-07 14:37:46 -04:00
|
|
|
|
2018-02-16 16:50:57 -05:00
|
|
|
func (im *imageMount) NewRWLayer() (builder.RWLayer, error) {
|
|
|
|
return im.layer.NewRWLayer()
|
2017-05-07 14:37:46 -04:00
|
|
|
}
|
2017-05-14 14:18:48 -04:00
|
|
|
|
2017-05-25 17:03:29 -04:00
|
|
|
func (im *imageMount) ImageID() string {
|
|
|
|
return im.image.ImageID()
|
2017-05-14 14:18:48 -04:00
|
|
|
}
|