Skip to content

Conversation

@adshmh
Copy link
Contributor

@adshmh adshmh commented Aug 4, 2017

docker image rm -f should exit with error code 1 if the image was not removed. Fixes #394

Signed-off-by: Arash Deshmeh adeshmeh@ca.ibm.com

- What I did
Added logic that makes 'docker image rm -f ' exit with 1 instead of 0 when an image cannot be removed (e.g. because running containers are using the image)

- How I did it

  • Added a new type of error (imageRemoveConflict) to docker/docker/client/errors.go, and converted the 409 status code to the new error in docker/docker/client/image_remove.go.
  • The image rm command uses the new type of error to exit.

- How to verify it

  • Run 'docker image rm -f ' for an image used by running container(s).
  • On the master branch, the exit code will be 0:

$ docker image rm -f 7328f6f8b418
Error response from daemon: conflict: unable to delete 7328f6f8b418 (cannot be forced) - image is being used by running container 36ea0cc00015
$ echo $?
0

  • With this PR, the exit code will be 1:

$ docker image rm -f 7328f6f8b418
Error response from daemon: conflict: unable to delete 7328f6f8b418 (cannot be forced) - image is being used by running container 36ea0cc00015
$ echo $?
1

- Description for the changelog
docker image rm -f exits with 1 if the image is not removed.

- A picture of a cute animal (not mandatory but encouraged)

@adshmh
Copy link
Contributor Author

adshmh commented Aug 4, 2017

I edited the files under vendor directory so all changes are seen together in the PR. If the changes are approved, I will submit a PR against docker/docker (with the addition of tests for new type of error and the API client).

if err != nil {
if apiclient.IsErrImageRemoveConflict(err) {
removeConflict = true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should invert this. Any NotFound can be skipped in the force case, but any other errors should still be reported as errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I just updated the PR. Two of the unit tests also needed to be changed, as with the new logic, the -f option can only skip imageNotFound errors.

@adshmh adshmh force-pushed the 394-image-rm-exit-with-1-if-not-removed branch from 449d66a to 2df56fa Compare August 8, 2017 18:26
@codecov-io
Copy link

codecov-io commented Aug 8, 2017

Codecov Report

Merging #418 into master will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #418      +/-   ##
==========================================
+ Coverage   46.28%   46.28%   +<.01%     
==========================================
  Files         194      194              
  Lines       16134    16138       +4     
==========================================
+ Hits         7467     7470       +3     
- Misses       8278     8279       +1     
  Partials      389      389

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is looking good, just a few minor comments

return []types.ImageDeleteResponseItem{{Deleted: image}}, nil
},
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should still have a case here "Image not found with force option"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I added a test case.
Would it be a good idea for the docker/docker/client package to export the notFound* errors to make testing easier? I think in addition to the new test, stack/deploy_composefile_test.go also implements a type of notFound error for testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. The problem with exporting them is that other packages could start to use them to report errors, which is not really the intent.

for _, img := range images {
dels, err := client.ImageRemove(ctx, img, options)
if err != nil {
if !apiclient.IsErrImageNotFound(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor, but these object specific error checkers should be deprecated. Please use IsErrNotFound() instead. IsErrImageNotfound() just calls that directly anyway.

}

var errs []string
var unresolvableErr = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: I'm not sure about the term "resolvable" here. Maybe "unrecoverable" or just "fatal" ?

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
@adshmh adshmh force-pushed the 394-image-rm-exit-with-1-if-not-removed branch from 2df56fa to 020703e Compare August 10, 2017 17:06
@dnephin
Copy link
Contributor

dnephin commented Aug 14, 2017

Thanks, looks good! Please ping me on the moby/moby PR to apply this fix to client/

@dnephin
Copy link
Contributor

dnephin commented Sep 5, 2017

@adshmh #424 has merged, so you should be able to rebase now and the vendor changes you need may already be in vendor/

@adshmh
Copy link
Contributor Author

adshmh commented Sep 8, 2017

@dnephin thank you for the reminder, it seems the changes are now in vendor/. I will rebase.

@thaJeztah
Copy link
Member

ping @adshmh could you rebase? I think docker 17.11-rc1 is scheduled to be cut off today; would be nice to get this in 👍

@dnephin
Copy link
Contributor

dnephin commented Oct 30, 2017

I'll carry this

@thaJeztah
Copy link
Member

@dnephin noticed this one as well (probably related); #567

@dnephin
Copy link
Contributor

dnephin commented Oct 30, 2017

Yes, carrying this will also close #567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants