Jelajahi Sumber

c8d/run: Allow running container without image

This allows the legacy builder to apply changes to the `FROM scratch`
layer.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit dfaff9598cf9207aaf2e275248d9cf8e8b9d3b54)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Paweł Gronowski 1 tahun lalu
induk
melakukan
63422515ba
1 mengubah file dengan 30 tambahan dan 27 penghapusan
  1. 30 27
      daemon/containerd/image_snapshot.go

+ 30 - 27
daemon/containerd/image_snapshot.go

@@ -18,42 +18,45 @@ import (
 
 // PrepareSnapshot prepares a snapshot from a parent image for a container
 func (i *ImageService) PrepareSnapshot(ctx context.Context, id string, parentImage string, platform *ocispec.Platform) error {
-	img, err := i.resolveImage(ctx, parentImage)
-	if err != nil {
-		return err
-	}
+	var parentSnapshot string
+	if parentImage != "" {
+		img, err := i.resolveImage(ctx, parentImage)
+		if err != nil {
+			return err
+		}
 
-	cs := i.client.ContentStore()
+		cs := i.client.ContentStore()
 
-	matcher := platforms.Default()
-	if platform != nil {
-		matcher = platforms.Only(*platform)
-	}
+		matcher := platforms.Default()
+		if platform != nil {
+			matcher = platforms.Only(*platform)
+		}
 
-	platformImg := containerd.NewImageWithPlatform(i.client, img, matcher)
-	unpacked, err := platformImg.IsUnpacked(ctx, i.snapshotter)
-	if err != nil {
-		return err
-	}
+		platformImg := containerd.NewImageWithPlatform(i.client, img, matcher)
+		unpacked, err := platformImg.IsUnpacked(ctx, i.snapshotter)
+		if err != nil {
+			return err
+		}
 
-	if !unpacked {
-		if err := platformImg.Unpack(ctx, i.snapshotter); err != nil {
+		if !unpacked {
+			if err := platformImg.Unpack(ctx, i.snapshotter); err != nil {
+				return err
+			}
+		}
+
+		desc, err := containerdimages.Config(ctx, cs, img.Target, matcher)
+		if err != nil {
 			return err
 		}
-	}
 
-	desc, err := containerdimages.Config(ctx, cs, img.Target, matcher)
-	if err != nil {
-		return err
-	}
+		diffIDs, err := containerdimages.RootFS(ctx, cs, desc)
+		if err != nil {
+			return err
+		}
 
-	diffIDs, err := containerdimages.RootFS(ctx, cs, desc)
-	if err != nil {
-		return err
+		parentSnapshot = identity.ChainID(diffIDs).String()
 	}
 
-	parent := identity.ChainID(diffIDs).String()
-
 	// Add a lease so that containerd doesn't garbage collect our snapshot
 	ls := i.client.LeasesService()
 	lease, err := ls.Create(ctx, leases.WithID(id))
@@ -69,7 +72,7 @@ func (i *ImageService) PrepareSnapshot(ctx context.Context, id string, parentIma
 	}
 
 	s := i.client.SnapshotService(i.StorageDriver())
-	_, err = s.Prepare(ctx, id, parent)
+	_, err = s.Prepare(ctx, id, parentSnapshot)
 	return err
 }