From eefc49ff72eee5305f9bd4c4efc700d11aa64db6 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 22 May 2018 15:58:22 -0700 Subject: [PATCH] dockerfile: detect empty images early Signed-off-by: Tonis Tiigi --- frontend/dockerfile/dockerfile2llb/convert.go | 10 +++++++++- solver/llbsolver/bridge.go | 6 ++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/dockerfile/dockerfile2llb/convert.go b/frontend/dockerfile/dockerfile2llb/convert.go index c09239a3..0f8bd631 100644 --- a/frontend/dockerfile/dockerfile2llb/convert.go +++ b/frontend/dockerfile/dockerfile2llb/convert.go @@ -156,6 +156,7 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, return err } d.stage.BaseName = reference.TagNameOnly(ref).String() + var isScratch bool if metaResolver != nil && reachable { dgst, dt, err := metaResolver.ResolveImageConfig(ctx, d.stage.BaseName) if err == nil { // handle the error while builder is actually running @@ -173,9 +174,16 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State, } d.stage.BaseName = ref.String() _ = ref + if len(img.RootFS.DiffIDs) == 0 { + isScratch = true + } } } - d.state = llb.Image(d.stage.BaseName, dfCmd(d.stage.SourceCode)) + if isScratch { + d.state = llb.Scratch() + } else { + d.state = llb.Image(d.stage.BaseName, dfCmd(d.stage.SourceCode)) + } return nil }) }(i, d) diff --git a/solver/llbsolver/bridge.go b/solver/llbsolver/bridge.go index b2b40cd0..98dc2ad5 100644 --- a/solver/llbsolver/bridge.go +++ b/solver/llbsolver/bridge.go @@ -88,8 +88,10 @@ func (b *llbBridge) Solve(ctx context.Context, req frontend.SolveRequest) (res s if !ok { return nil, nil, errors.Errorf("invalid reference for exporting: %T", res.Sys()) } - if err := wr.ImmutableRef.Finalize(ctx); err != nil { - return nil, nil, err + if wr.ImmutableRef != nil { + if err := wr.ImmutableRef.Finalize(ctx); err != nil { + return nil, nil, err + } } } return