Skip to content

Commit b776706

Browse files
committed
fix: Allow to untag images associated with running or paused containers by nerdctl rmi -f
In Docker, running `docker rmi -f <Image Names>` on images associated with running or stopped containers will untag the images, leaving <none> images. The specific behavior in Docker is as follows. ``` > docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 91ef0af61f39 6 weeks ago 7.8MB > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fe4caab5cf42 alpine "sleep infinity" 4 minutes ago Up 4 minutes test > docker rmi -f alpine Untagged: alpine:latest Untagged: alpine@sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d > docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 91ef0af61f39 6 weeks ago 7.8MB ``` On the other hand, the same operation described above with nerdctl will result in the following error. ``` > nerdctl rmi -f alpine FATA[0000] 1 errors: conflict: unable to delete alpine (cannot be forced) - image is being used by running container 59261bebc8113ca1ea102203137c32406742c2ec43ca3b108a314e9bfb4657fb ``` This befavior is reported in the following: - containerd#3454 Therefore, this commit fixes it so that `nerdctl rmi -f <Image Names>` can be performed on images associated with running or stopped containers. The behaviour in nerdctl after this modification is as follows. ``` > nerdctl images REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE alpine latest beefdbd8a1da 5 seconds ago linux/amd64 8.458MB 3.626MB > nerdctl ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 28c9db821576 docker.io/library/alpine:latest "sleep infinity" 6 seconds ago Up alpine-28c9d > nerdctl rmi -f alpine Untagged: docker.io/library/alpine:latest Untagged: sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d > nerdctl images REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE <none> <none> beefdbd8a1da 3 seconds ago linux/amd64 8.458MB 3.626MB ``` Signed-off-by: Hayato Kiwata <[email protected]>
1 parent f12f7fe commit b776706

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)