From 99fc1b37fd7f8369cff32235c1209f862832205f Mon Sep 17 00:00:00 2001 From: Manu Gupta Date: Sun, 23 Oct 2022 20:22:54 -0700 Subject: [PATCH] Add a dangling image prefix when tag name is not specified. Fixes: https://github.com/containerd/nerdctl/issues/1398 Signed-off-by: Manu Gupta --- cmd/nerdctl/build.go | 1 + cmd/nerdctl/build_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/nerdctl/build.go b/cmd/nerdctl/build.go index 1035e4ade1c..268e1c1ee6d 100644 --- a/cmd/nerdctl/build.go +++ b/cmd/nerdctl/build.go @@ -329,6 +329,7 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg return "", nil, false, "", nil, nil, err } + output = output + ",dangling-name-prefix=none" buildctlArgs = append(buildctlArgs, []string{ "build", "--progress=" + progressValue, diff --git a/cmd/nerdctl/build_test.go b/cmd/nerdctl/build_test.go index fcb9812b0e7..bd3ecd17d38 100644 --- a/cmd/nerdctl/build_test.go +++ b/cmd/nerdctl/build_test.go @@ -17,6 +17,7 @@ package main import ( + "encoding/json" "fmt" "os" "path/filepath" @@ -402,3 +403,32 @@ CMD ["echo", "dockerfile"] base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("run", "--rm", imageName).AssertOutExactly("dockerfile\n") } + +func TestBuildNoTag(t *testing.T) { + testutil.RequiresBuild(t) + testutil.DockerIncompatible(t) + base := testutil.NewBase(t) + dockerfile := fmt.Sprintf(`FROM %s +CMD ["echo", "nerdctl-build-notag-string"] + `, testutil.CommonImage) + buildCtx, err := createBuildContext(dockerfile) + assert.NilError(t, err) + defer os.RemoveAll(buildCtx) + + base.Cmd("--debug", "build", buildCtx).AssertOK() + imgs := base.Cmd("images", "--format", "json").OutLines() + imgs = imgs[:len(imgs)-1] + var noneImage *imagePrintable + var foundNoneImage bool + for _, img := range imgs { + err := json.Unmarshal([]byte(img), &noneImage) + assert.NilError(t, err, img) + if noneImage.Tag == "" { + foundNoneImage = true + break + } + } + assert.Assert(t, foundNoneImage) + defer base.Cmd("image", "rm", noneImage.ID).AssertOK() + base.Cmd("run", "--rm", noneImage.ID).AssertOutExactly("nerdctl-build-notag-string\n") +}