Skip to content

Commit c71e8d9

Browse files
authored
Merge pull request containerd#3602 from haytok/isssue_3454
fix: Allow to untag images associated with running or paused containe…
2 parents f12f7fe + b776706 commit c71e8d9

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

cmd/nerdctl/image/image_remove_test.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import (
3232
func TestRemove(t *testing.T) {
3333
testCase := nerdtest.Setup()
3434

35+
const (
36+
imgShortIDKey = "imgShortID"
37+
)
38+
3539
repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
3640
nginxRepoName, _ := imgutil.ParseRepoTag(testutil.NginxAlpineImage)
3741
// NOTES:
@@ -112,28 +116,30 @@ func TestRemove(t *testing.T) {
112116
{
113117
Description: "Remove image with running container - with -f",
114118
NoParallel: true,
115-
// FIXME: nerdctl is broken
116-
// https://github.com/containerd/nerdctl/issues/3454
117-
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
118-
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
119-
// In both cases, `nerdctl rmi -f` will fail.
120119
Require: test.Require(
121120
test.Not(nerdtest.Docker),
122121
),
123122
Setup: func(data test.Data, helpers test.Helpers) {
124123
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)
124+
125+
img := nerdtest.InspectImage(helpers, testutil.CommonImage)
126+
repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
127+
imgShortID := strings.TrimPrefix(img.RepoDigests[0], repoName+"@sha256:")[0:8]
128+
129+
data.Set(imgShortIDKey, imgShortID)
125130
},
126131
Cleanup: func(data test.Data, helpers test.Helpers) {
127132
helpers.Anyhow("rm", "-f", data.Identifier())
133+
helpers.Anyhow("rmi", "-f", data.Get(imgShortIDKey))
128134
},
129135
Command: test.Command("rmi", "-f", testutil.CommonImage),
130136
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
131137
return &test.Expected{
132-
ExitCode: 1,
133-
Errors: []error{errors.New("image is being used")},
138+
ExitCode: 0,
139+
Errors: []error{},
134140
Output: func(stdout string, info string, t *testing.T) {
135141
helpers.Command("images").Run(&test.Expected{
136-
Output: test.Contains(repoName),
142+
Output: test.Contains("<none>"),
137143
})
138144
},
139145
}
@@ -219,28 +225,30 @@ func TestRemove(t *testing.T) {
219225
NoParallel: true,
220226
Require: test.Require(
221227
nerdtest.CGroup,
222-
// FIXME: nerdctl is broken
223-
// https://github.com/containerd/nerdctl/issues/3454
224-
// If an image is associated with a running/paused containers, `docker rmi -f imageName`
225-
// untags `imageName` (left a `<none>` image) without deletion; `docker rmi -rf imageID` fails.
226-
// In both cases, `nerdctl rmi -f` will fail.
227228
test.Not(nerdtest.Docker),
228229
),
229230
Setup: func(data test.Data, helpers test.Helpers) {
230231
helpers.Ensure("run", "--pull", "always", "-d", "--name", data.Identifier(), testutil.CommonImage, "sleep", nerdtest.Infinity)
231232
helpers.Ensure("pause", data.Identifier())
233+
234+
img := nerdtest.InspectImage(helpers, testutil.CommonImage)
235+
repoName, _ := imgutil.ParseRepoTag(testutil.CommonImage)
236+
imgShortID := strings.TrimPrefix(img.RepoDigests[0], repoName+"@sha256:")[0:8]
237+
238+
data.Set(imgShortIDKey, imgShortID)
232239
},
233240
Cleanup: func(data test.Data, helpers test.Helpers) {
234241
helpers.Anyhow("rm", "-f", data.Identifier())
242+
helpers.Anyhow("rmi", "-f", data.Get(imgShortIDKey))
235243
},
236244
Command: test.Command("rmi", "-f", testutil.CommonImage),
237245
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
238246
return &test.Expected{
239-
ExitCode: 1,
240-
Errors: []error{errors.New("image is being used")},
247+
ExitCode: 0,
248+
Errors: []error{},
241249
Output: func(stdout string, info string, t *testing.T) {
242250
helpers.Command("images").Run(&test.Expected{
243-
Output: test.Contains(repoName),
251+
Output: test.Contains("<none>"),
244252
})
245253
},
246254
}

pkg/cmd/image/remove.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
7878
}
7979

8080
if cid, ok := runningImages[found.Image.Name]; ok {
81+
if options.Force {
82+
if err = is.Delete(ctx, found.Image.Name); err != nil {
83+
return err
84+
}
85+
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Name)
86+
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Target.Digest.String())
87+
88+
found.Image.Name = ":"
89+
if _, err = is.Create(ctx, found.Image); err != nil {
90+
return err
91+
}
92+
return nil
93+
}
8194
return fmt.Errorf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", found.Req, cid)
8295
}
8396
if cid, ok := usedImages[found.Image.Name]; ok && !options.Force {

0 commit comments

Comments
 (0)