moby: [1.9.0 RC-2] docker rmi fails when removing images with multiple tags

Description of problem: docker version:

Client:
 Version:      1.9.0-rc2
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   60d36f7
 Built:        Fri Oct 23 02:01:24 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.9.0-rc2
 API version:  1.21
 Go version:   go1.4.2
 Git commit:   60d36f7
 Built:        Fri Oct 23 02:01:24 UTC 2015
 OS/Arch:      linux/amd64

docker info:

Containers: 1
Images: 4
Server Version: 1.9.0-rc2
Storage Driver: overlay
 Backing Filesystem: extfs
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.2.0-1-amd64
Operating System: Debian GNU/Linux stretch/sid (containerized)
CPUs: 4
Total Memory: 5.312 GiB
Name: ike
ID: 3TYO:NWYR:PAHR:FFIF:XCWO:H23J:RLA7:YOOQ:TUHP:AU2L:537X:G3GO
Username: jgkamat
Registry: https://index.docker.io/v1/
WARNING: No memory limit support
WARNING: No swap limit support

uname -a:

Linux ike 4.2.0-1-amd64 #1 SMP Debian 4.2.3-2 (2015-10-14) x86_64 GNU/Linux

Environment details (AWS, VirtualBox, physical, etc.): Physical Machine, Debian Testing, Up to date (Oct 23)

How reproducible: Always

Steps to Reproduce: Right now, ubuntu:latest and ubuntu:14.04 point to the same image. Given that:

  1. Clean all images from docker host (for consistency)
  2. docker pull ubuntu:14.04
  3. docker pull ubuntu:latest
  4. docker rmi $(docker images -q) # Delete images based on ImageID

Actual Results:

[jay@ike ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               1d073211c498        9 hours ago         187.9 MB
ubuntu              latest              1d073211c498        9 hours ago         187.9 MB
[jay@ike ~]$ docker rmi $(docker images -q)
Error response from daemon: conflict: unable to delete 1d073211c498 (must be forced) - image is referenced in one or more repositories
Error response from daemon: conflict: unable to delete 1d073211c498 (must be forced) - image is referenced in one or more repositories
Error: failed to remove images: [1d073211c498 1d073211c498]

Expected Results: Both images deleted successfully.

Additional info: Might be related to #16890? I don’t know though. I’m pretty sure this was working in 1.9.0 RC-1, and this was definitely working on 1.8.3. Is this expected (new) behavior? I’m pretty sure some automation somewhere relies on docker rmi $(docker images -q) to delete all images.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 22 (21 by maintainers)

Most upvoted comments

I think the problem in this case is that the image ID you are removing has more than 1 named reference. See https://github.com/docker/docker/pull/15606#issuecomment-134744099

The way that the maintainers intend for people to use Images is to never (ideally) have any unreferenced image IDs and that users would hopefully always refer to images by a given name rather than the randomly generated ID.

One usability issue with the CLI wrt images is that there exists no separate command for simply dereferencing (or _un_tagging) an image. docker rmi is overloaded to perform both actions. Because we want users to be careful to never accidentally delete an image that is referenced multiple times, it’s currently not allowed unless you force it.

What would probably work for you is if you referenced the image names when using docker rmi. Then you will not get this error.

Let’s take a look at the issue you described in more detail:

[jay@ike ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               1d073211c498        9 hours ago         187.9 MB
ubuntu              latest              1d073211c498        9 hours ago         187.9 MB
[jay@ike ~]$ docker rmi $(docker images -q)
Error response from daemon: conflict: unable to delete 1d073211c498 (must be forced) - image is referenced in one or more repositories
Error response from daemon: conflict: unable to delete 1d073211c498 (must be forced) - image is referenced in one or more repositories
Error: failed to remove images: [1d073211c498 1d073211c498]

docker images -q produces only the IDs as output. You’ll notice that both of these images have the same ID, so your command is equivalent to running:

$ docker rmi 1d073211c498 1d073211c498

Which attempts to delete the same image twice. First of all, assuming that the deletion did succeed, it would then immediately error out because when it gets to the second one an image with that ID would no longer exist. What you probably want to do is delete these images by their named references like so:

$ docker images ubuntu | tail -n +2 | awk '{ print $1 ":" $2}' | xargs docker rmi
Untagged: ubuntu:14.04
Untagged: ubuntu:latest

It’s a bit unfortunate to have to use tail and awk here, but it’s the only way to programmatically generate a list of image references that you want to delete. In this form, docker rmi knows that what you really want to do is untag these images. If there is no longer any references to those images and the image has no child images* then (and only then) will the images be removed.

A good change to the docker images command would be an option to complement -q which lists only the named references instead of the image IDs.

Yep, I think to have the old behavior back, a docker rmi -f $(docker images -q) should work… doesn’t feel so bad. @jgkamat wdyt ?

Having a --format flag on docker images (similar to the flag that’s recently been added to docker ps) would definitely help here