Skip to content

Commit

Permalink
Merge pull request #3602 from haytok/isssue_3454
Browse files Browse the repository at this point in the history
fix: Allow to untag images associated with running or paused containe…
  • Loading branch information
djdongjin authored Nov 4, 2024
2 parents f12f7fe + b776706 commit c71e8d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
40 changes: 24 additions & 16 deletions cmd/nerdctl/image/image_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import (
func TestRemove(t *testing.T) {
testCase := nerdtest.Setup()

const (
imgShortIDKey = "imgShortID"
)

repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
nginxRepoName, _ := imgutil.ParseRepoTag(testutil.NginxAlpineImage)
// NOTES:
Expand Down Expand Up @@ -112,28 +116,30 @@ func TestRemove(t *testing.T) {
{
Description: "Remove image with running container - with -f",
NoParallel: true,
// FIXME: nerdctl is broken
// https://github.com/containerd/nerdctl/issues/3454
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
// In both cases, `nerdctl rmi -f` will fail.
Require: test.Require(
test.Not(nerdtest.Docker),
),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)

img := nerdtest.InspectImage(helpers, testutil.CommonImage)
repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
imgShortID := strings.TrimPrefix(img.RepoDigests[0], repoName+"@sha256:")[0:8]

data.Set(imgShortIDKey, imgShortID)
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rmi", "-f", data.Get(imgShortIDKey))
},
Command: test.Command("rmi", "-f", testutil.CommonImage),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errors.New("image is being used")},
ExitCode: 0,
Errors: []error{},
Output: func(stdout string, info string, t *testing.T) {
helpers.Command("images").Run(&test.Expected{
Output: test.Contains(repoName),
Output: test.Contains("<none>"),
})
},
}
Expand Down Expand Up @@ -219,28 +225,30 @@ func TestRemove(t *testing.T) {
NoParallel: true,
Require: test.Require(
nerdtest.CGroup,
// FIXME: nerdctl is broken
// https://github.com/containerd/nerdctl/issues/3454
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
// In both cases, `nerdctl rmi -f` will fail.
test.Not(nerdtest.Docker),
),
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)
helpers.Ensure("pause", data.Identifier())

img := nerdtest.InspectImage(helpers, testutil.CommonImage)
repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
imgShortID := strings.TrimPrefix(img.RepoDigests[0], repoName+"@sha256:")[0:8]

data.Set(imgShortIDKey, imgShortID)
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
helpers.Anyhow("rmi", "-f", data.Get(imgShortIDKey))
},
Command: test.Command("rmi", "-f", testutil.CommonImage),
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 1,
Errors: []error{errors.New("image is being used")},
ExitCode: 0,
Errors: []error{},
Output: func(stdout string, info string, t *testing.T) {
helpers.Command("images").Run(&test.Expected{
Output: test.Contains(repoName),
Output: test.Contains("<none>"),
})
},
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/cmd/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
}

if cid, ok := runningImages[found.Image.Name]; ok {
if options.Force {
if err = is.Delete(ctx, found.Image.Name); err != nil {
return err
}
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Name)
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Target.Digest.String())

found.Image.Name = ":"
if _, err = is.Create(ctx, found.Image); err != nil {
return err
}
return nil
}
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 := usedImages[found.Image.Name]; ok && !options.Force {
Expand Down

0 comments on commit c71e8d9

Please sign in to comment.