Skip to content

Commit

Permalink
Add a dangling image prefix when tag name is not specified.
Browse files Browse the repository at this point in the history
Fixes: #1398
Signed-off-by: Manu Gupta <manugupt1@gmail.com>
  • Loading branch information
manugupt1 committed Nov 3, 2022
1 parent 817d6ec commit 6f81b76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg
}
tags[idx] = dockerreference.TagNameOnly(named).String()
}
} else if len(tags) == 0 {
output = output + ",dangling-name-prefix=none"
}

buildctlArgs = buildkitutil.BuildctlBaseArgs(buildkitHost)
Expand Down
31 changes: 31 additions & 0 deletions cmd/nerdctl/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -402,3 +403,33 @@ 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)
defer base.Cmd("builder", "prune").AssertOK()
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("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 == "<none>" {
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")
}

0 comments on commit 6f81b76

Please sign in to comment.