Skip to content

Commit

Permalink
Add support for rmi --force for nerdctl images
Browse files Browse the repository at this point in the history
Signed-off-by: Manu Gupta <[email protected]>
  • Loading branch information
manugupt1 committed Oct 22, 2024
1 parent beaa39d commit b16fed6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cmd/nerdctl/image/image_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,18 @@ func TestIssue3016(t *testing.T) {

testCase.Run(t)
}

func TestForceRemoveImage(t *testing.T) {
base := testutil.NewBase(t)
tID := testutil.Identifier(t)
base.Cmd("image", "prune", "--force", "--all").AssertOK()
base.Cmd("pull", testutil.CommonImage).AssertOK()
base.Cmd("run", "-d", "--name", tID+"-force", testutil.CommonImage, "sleep", "10s").AssertOK()
base.Cmd("rmi", "-f", testutil.CommonImage).AssertOK()
// Ensure that the image is deleted
base.Cmd("images").AssertOutNotContains(testutil.ImageRepo(testutil.CommonImage))
// Ensure that the container is still running
base.Cmd("ps", "-a").AssertOutContains(tID + "-force")
// Force remove to clean
base.Cmd("rm", "-f", tID+"-force").AssertOK()
}
4 changes: 2 additions & 2 deletions pkg/cmd/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
return nil
}

if cid, ok := runningImages[found.Image.Name]; ok {
return fmt.Errorf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", found.Req, cid)
if cid, ok := runningImages[found.Image.Name]; !options.Force && ok {
return fmt.Errorf("conflict: unable to delete %s - image is being used by running container %s", found.Req, cid)
}
if cid, ok := usedImages[found.Image.Name]; ok && !options.Force {
return fmt.Errorf("conflict: unable to delete %s (must be forced) - image is being used by stopped container %s", found.Req, cid)
Expand Down

0 comments on commit b16fed6

Please sign in to comment.