diff --git a/cli/command/image/remove.go b/cli/command/image/remove.go index 91bf2f8786..0a4bef3caf 100644 --- a/cli/command/image/remove.go +++ b/cli/command/image/remove.go @@ -56,9 +56,12 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error } var errs []string - for _, image := range images { - dels, err := client.ImageRemove(ctx, image, options) + for _, img := range images { + dels, err := client.ImageRemove(ctx, img, options) if err != nil { + if opts.force { + fmt.Fprintf(dockerCli.Out(), "NotFound: %s\n", img) + } errs = append(errs, err.Error()) } else { for _, del := range dels { @@ -71,7 +74,7 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error } } - if len(errs) > 0 { + if !opts.force && len(errs) > 0 { return errors.Errorf("%s", strings.Join(errs, "\n")) } return nil diff --git a/cli/command/image/remove_test.go b/cli/command/image/remove_test.go index 0915729b20..ff671285ee 100644 --- a/cli/command/image/remove_test.go +++ b/cli/command/image/remove_test.go @@ -67,6 +67,14 @@ func TestNewRemoveCommandSuccess(t *testing.T) { return []types.ImageDeleteResponseItem{{Deleted: image}}, nil }, }, + { + name: "Image Deleted with force option", + args: []string{"-f", "image1"}, + imageRemoveFunc: func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) { + assert.Equal(t, "image1", image) + return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image") + }, + }, { name: "Image Untagged", args: []string{"image1"}, diff --git a/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden b/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden new file mode 100644 index 0000000000..f965906f58 --- /dev/null +++ b/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden @@ -0,0 +1,2 @@ +NotFound:image1 +NotFound:image1 \ No newline at end of file