2018-02-07 20:52:47 +00:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2018-02-21 22:20:58 +00:00
|
|
|
|
|
|
|
import (
|
2023-01-12 14:14:12 +00:00
|
|
|
"context"
|
2018-02-21 22:20:58 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/backend"
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2018-02-02 22:18:46 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-21 22:20:58 +00:00
|
|
|
)
|
|
|
|
|
2018-02-02 22:18:46 +00:00
|
|
|
// CommitImage creates a new image from a commit config
|
2023-01-12 14:14:12 +00:00
|
|
|
func (i *ImageService) CommitImage(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:34:08 +00:00
|
|
|
rwTar, err := exportContainerRw(i.layerStore, c.ContainerID, c.ContainerMountLabel)
|
2018-02-21 22:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if rwTar != nil {
|
|
|
|
rwTar.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var parent *image.Image
|
|
|
|
if c.ParentImageID == "" {
|
|
|
|
parent = new(image.Image)
|
|
|
|
parent.RootFS = image.NewRootFS()
|
|
|
|
} else {
|
2018-02-02 22:18:46 +00:00
|
|
|
parent, err = i.imageStore.Get(image.ID(c.ParentImageID))
|
2018-02-21 22:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:34:08 +00:00
|
|
|
l, err := i.layerStore.Register(rwTar, parent.RootFS.ChainID())
|
2018-02-21 22:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-03-19 14:34:08 +00:00
|
|
|
defer layer.ReleaseAndLog(i.layerStore, l)
|
2018-02-21 22:20:58 +00:00
|
|
|
|
|
|
|
cc := image.ChildConfig{
|
|
|
|
ContainerID: c.ContainerID,
|
|
|
|
Author: c.Author,
|
|
|
|
Comment: c.Comment,
|
|
|
|
ContainerConfig: c.ContainerConfig,
|
|
|
|
Config: c.Config,
|
|
|
|
DiffID: l.DiffID(),
|
|
|
|
}
|
|
|
|
config, err := json.Marshal(image.NewChildImage(parent, cc, c.ContainerOS))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-02-02 22:18:46 +00:00
|
|
|
id, err := i.imageStore.Create(config)
|
2018-02-21 22:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ParentImageID != "" {
|
2018-02-02 22:18:46 +00:00
|
|
|
if err := i.imageStore.SetParent(id, image.ID(c.ParentImageID)); err != nil {
|
2018-02-21 22:20:58 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportContainerRw(layerStore layer.Store, id, mountLabel string) (arch io.ReadCloser, err error) {
|
|
|
|
rwlayer, err := layerStore.GetRWLayer(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
layerStore.ReleaseRWLayer(rwlayer)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO: this mount call is not necessary as we assume that TarStream() should
|
|
|
|
// mount the layer if needed. But the Diff() function for windows requests that
|
|
|
|
// the layer should be mounted when calling it. So we reserve this mount call
|
|
|
|
// until windows driver can implement Diff() interface correctly.
|
|
|
|
_, err = rwlayer.Mount(mountLabel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := rwlayer.TarStream()
|
|
|
|
if err != nil {
|
|
|
|
rwlayer.Unmount()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
archive.Close()
|
|
|
|
err = rwlayer.Unmount()
|
|
|
|
layerStore.ReleaseRWLayer(rwlayer)
|
|
|
|
return err
|
|
|
|
}),
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitBuildStep is used by the builder to create an image for each step in
|
|
|
|
// the build.
|
|
|
|
//
|
|
|
|
// This method is different from CreateImageFromContainer:
|
2022-07-08 16:27:07 +00:00
|
|
|
// - it doesn't attempt to validate container state
|
|
|
|
// - it doesn't send a commit action to metrics
|
|
|
|
// - it doesn't log a container commit event
|
2018-02-21 22:20:58 +00:00
|
|
|
//
|
|
|
|
// This is a temporary shim. Should be removed when builder stops using commit.
|
2023-01-12 14:14:12 +00:00
|
|
|
func (i *ImageService) CommitBuildStep(ctx context.Context, c backend.CommitConfig) (image.ID, error) {
|
2022-08-08 10:55:10 +00:00
|
|
|
ctr := i.containers.Get(c.ContainerID)
|
|
|
|
if ctr == nil {
|
2018-02-02 22:18:46 +00:00
|
|
|
// TODO: use typed error
|
|
|
|
return "", errors.Errorf("container not found: %s", c.ContainerID)
|
2018-02-21 22:20:58 +00:00
|
|
|
}
|
2022-08-08 10:55:10 +00:00
|
|
|
c.ContainerMountLabel = ctr.MountLabel
|
|
|
|
c.ContainerOS = ctr.OS
|
|
|
|
c.ParentImageID = string(ctr.ImageID)
|
2023-01-12 14:14:12 +00:00
|
|
|
return i.CommitImage(ctx, c)
|
2018-02-21 22:20:58 +00:00
|
|
|
}
|