Skip to content

Commit

Permalink
stage_executor: allow images without layers
Browse files Browse the repository at this point in the history
`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 <arajan@redhat.com>
  • Loading branch information
flouthoc committed Sep 14, 2023
1 parent 70d8ab9 commit aad193f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aad193f

Please sign in to comment.