From aad193fea4155fa8c5f66a85c7876548c090830e Mon Sep 17 00:00:00 2001 From: Aditya R Date: Thu, 14 Sep 2023 14:51:16 +0530 Subject: [PATCH] stage_executor: allow images without layers `Docker` and `Buildkit` creates no layers when only `FROM scratch` is used as content for building image, buildah must do the same. Signed-off-by: Aditya R --- imagebuildah/stage_executor.go | 9 ++++++++- tests/bud.bats | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/imagebuildah/stage_executor.go b/imagebuildah/stage_executor.go index e901e7c8627..6199c58c15b 100644 --- a/imagebuildah/stage_executor.go +++ b/imagebuildah/stage_executor.go @@ -1033,7 +1033,14 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string, // squash the contents of the base image. Whichever is // the case, we need to commit() to create a new image. logCommit(s.output, -1) - if imgID, ref, err = s.commit(ctx, s.getCreatedBy(nil, ""), false, s.output, s.executor.squash, lastStage); err != nil { + emptyLayer := false + if s.builder.FromImageID == "" && s.builder.FromImage == "" { + // If we only have a base image and its `scratch` and + // since there are no other childrens in this branch + // then treat this as an `empty_layer`. + emptyLayer = true + } + if imgID, ref, err = s.commit(ctx, s.getCreatedBy(nil, ""), emptyLayer, s.output, s.executor.squash, lastStage); err != nil { return "", nil, fmt.Errorf("committing base container: %w", err) } // Generate build output if needed. diff --git a/tests/bud.bats b/tests/bud.bats index 5c1b5cba372..f02153ef525 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -89,6 +89,15 @@ _EOF validate_instance_compression "3" "$list" "arm64" "zstd" } +@test "no layer should be created on scratch" { + run_buildah build --layers --label "label1=value1" -t test -f $BUDFILES/from-scratch/Containerfile + run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' test + expect_output "0" "layer should not exist" + run_buildah build --layers -t test -f $BUDFILES/from-scratch/Containerfile + run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' test + expect_output "0" "layer should not exist" +} + @test "bud: build push with --force-compression" { skip_if_no_podman local contextdir=${TEST_SCRATCH_DIR}/bud/platform