simon
simon

Reputation: 2821

why does docker prune not remove my dangling images?

Tthe docs say docker prune "removes all dangling images". And it even issues a warning "this will remove all dangling images". So why does it not remove mine?

 ubuntu@ip-172-31-26-48:~$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0 B
ubuntu@ip-172-31-26-48:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
simonm3/registrm    latest              bd3ef6d73785        7 minutes ago       468 MB
simonm3/registr     latest              7dd15943671b        2 hours ago         2.51 GB
simonm3/registrm    <none>              f8f22986e85a        45 hours ago        2.93 GB
simonm3/registrm    <none>              c1b9d8606371        45 hours ago        2.93 GB
simonm3/registrm    <none>              f1ca97467106        45 hours ago        2.93 GB
simonm3/registr     <none>              6a5af88f93a7        45 hours ago        4.42 GB
simonm3/registrm    <none>              93dc81b0fa7a        45 hours ago        2.93 GB
simonm3/registrm    <none>              dbc50510d499        45 hours ago        2.93 GB
simonm3/registr     <none>              0101ca5d4a25        45 hours ago        4.42 GB
simonm3/registrm    <none>              d8f85df39f0a        46 hours ago        2.93 GB
simonm3/registrm    <none>              053892e9798b        46 hours ago        2.93 GB
simonm3/registrm    <none>              3845056cf241        46 hours ago        2.92 GB
simonm3/registr     <none>              349ef3c589bd        46 hours ago        4.42 GB
simonm3/registrm    <none>              5dcbb0db4710        2 days ago          2.92 GB
simonm3/registr     <none>              e403f74861f6        2 days ago          4.32 GB
simonm3/registr     <none>              c12ca8819c24        5 days ago          4.32 GB
simonm3/registrm    <none>              4bb8ed8154b7        6 days ago          2.92 GB
ubuntu@ip-172-31-26-48:~$ docker images -q -f dangling=true
f8f22986e85a
c1b9d8606371
f1ca97467106
6a5af88f93a7
93dc81b0fa7a
dbc50510d499
0101ca5d4a25
d8f85df39f0a
053892e9798b
3845056cf241
349ef3c589bd
5dcbb0db4710
e403f74861f6
c12ca8819c24
4bb8ed8154b7

Upvotes: 33

Views: 19614

Answers (4)

mirekphd
mirekphd

Reputation: 6791

If you try to remove all those images tagged with <none> the docker rmi command will refuse (even when forced with -f), claiming they are used in other child images:

$ docker rmi -f $(docker images -a | grep '<none>' | awk '{print $3}' | head -10) 
Error response from daemon: conflict: unable to delete e6da252e9c6d (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete acfa42da25c4 (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete 9b0ab509f17e (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete 60be66128f80 (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete 5806afd2a6d2 (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete cb0db4d86d68 (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete a55dc64b78d0 (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete cf9654bfcfbd (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete bedcaa9e433f (cannot be forced) - image has dependent child images
Error response from daemon: conflict: unable to delete 1e113b1d7a73 (cannot be forced) - image has dependent child images

The only working method is a destructive one - to remove all non-running containers using docker system prune -a, but wait, there is a workaround - first start (via docker run) all useful containers that you want to keep before your run that docker system prune -a command, as it won't remove images used in running containers. If you forget to start those useful containers (and keep them running, if necessary with an infinite loop, like this), it is likely that after system prune -a finishes, the number of all container images (including not just the dangling ones) returned by docker images -a | wc -l will be exactly zero.

Upvotes: 0

damd
damd

Reputation: 6957

My problem was that I misunderstood what "dangling image" actually meant. I wanted to delete all unused images, not just the dangling images.

  • An unused image is one that is not currently assigned to any container.
  • A dangling image is one that has not been tagged. Remember that Docker images are identified by their sha256 digests, not their tags.

I actually wanted to remove all unused Docker images, which you would need to use -a/--all for.

$ docker help image prune

Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation

So in other words, the solution for me was:

$ docker image prune -a

Upvotes: 45

11k
11k

Reputation: 319

Docker won't prune dangling images that still have containers, even if they're no longer running. Use docker container prune to remove stopped containers, first, then docker image prune will remove dangling images the pruned containers relied on.

Upvotes: 21

vmonteco
vmonteco

Reputation: 15403

I think that's because these are probably intermediary images for some image building.

Therefore not really dangling images.

Upvotes: 0

Related Questions