How to Remove All Unused and Dangling Docker Images ?
This tutorial will guide you on how to remove all unused and dangling docker images. You need to remove or cleanup images which are not used or when you don’t want to do anything with that. Let’s see how to remove images effectively.
Remove all unused and dangling docker images
There are some data management docker commands which will help us to visualize how much space the docker images, containers, volumes etc., are consuming on disk. The following command will provide summary about different docker objects.
$ docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 5 2 80.68MB 79.45MB (98%) Containers 3 0 147.8MB 147.8MB (100%) Local Volumes 1 1 0B 0B Build Cache 0 0 0B 0B
Use the following docker management command to check the space usage details.
$ docker system df -v Images space usage: REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS <none> <none> c996d73e5113 13 minutes ago 73.86MB 73.86MB 0B 0 ubuntuv1 latest 8f1442d03996 24 minutes ago 73.86MB 73.86MB 0B 0 busybox latest 018c9d7b792b 2 days ago 1.224MB 0B 1.224MB 1 ubuntu latest 1e4467b07108 5 days ago 73.86MB 73.86MB 0B 2 gcr.io/sneppets-gcp/hellodocker-image latest 40041a9ff2e2 4 months ago 5.596MB 0B 5.596MB 0 Containers space usage: CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES ce8ed5705333 ubuntu "/bin/bash -c 'apt-g…" 0 148MB 27 minutes ago Exited (1) 27 minutes ago hungry_jones 5abcfb446878 busybox "ls /world" 1 0B 28 minutes ago Exited (0) 28 minutes ago gallant_ellis d41a74c34984 ubuntu "bash" 0 0B 29 minutes ago Exited (0) 29 minutes ago priceless_benz Local Volumes space usage: VOLUME NAME LINKS SIZE hello 1 0B Build cache usage: 0B CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
You can cross check once using the following docker command to see list of docker images.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> c996d73e5113 17 minutes ago 73.9MB ubuntuv1 latest 8f1442d03996 28 minutes ago 73.9MB busybox latest 018c9d7b792b 2 days ago 1.22MB ubuntu latest 1e4467b07108 5 days ago 73.9MB gcr.io/sneppets-gcp/hellodocker-image latest 40041a9ff2e2 4 months ago 5.6MB
Then do docker ps -a
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce8ed5705333 ubuntu "/bin/bash -c 'apt-g…" 35 minutes ago Exited (1) 34 minutes ago hungry_jones 5abcfb446878 busybox "ls /world" 36 minutes ago Exited (0) 36 minutes ago gallant_ellis d41a74c34984 ubuntu "bash" 37 minutes ago Exited (0) 37 minutes ago priceless_benz
Therefore, there are some unused docker images, and one even without any tag hanging there and occupying the disk space unnecessarily. And we could see some images that are not being used by containers at all.
Remove all dangling docker images
The following command is used to remove all dangling docker images i.e., the docker images no longer referenced with and tag/ label (<none>).
$ docker image prune WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N] y Deleted Images: deleted: sha256:c996d73e5113960ddcc390658ea5a70583072a7371c91ac0fcf6112b5dbd4ab4 Total reclaimed space: 0B $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntuv1 latest 8f1442d03996 37 minutes ago 73.9MB busybox latest 018c9d7b792b 2 days ago 1.22MB ubuntu latest 1e4467b07108 5 days ago 73.9MB gcr.io/sneppets-gcp/hellodocker-image latest 40041a9ff2e2 4 months ago 5.6MB
Remove all unused docker images
To remove all unused docker images i.e., the docker images that do not have at-least one container associated to them.
$ docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Deleted Images: untagged: gcr.io/sneppets-gcp/hellodocker-image:latest untagged: gcr.io/sneppets-gcp/hellodocker-image@sha256:20da7a3d10a6cdbc541a1f582530907b13a98429e594fa8b56e0236cb187a0e1 deleted: sha256:40041a9ff2e2d5a5e2ed368b09a94ff20f55208c52d072fdc74726060f8219a7 deleted: sha256:2731f56a29a1666251c705101281c6b46700989018f64c76183fcce1086c9fc4 deleted: sha256:beee9f30bc1f711043e78d4a2be0668955d4b761d587d6f60c2c8dc081efb203 untagged: ubuntuv1:latest deleted: sha256:8f1442d0399640a440650cdcd36126e7ff6cf00e1480b7ee7d2905c2fc3a8d31 Total reclaimed space: 5.596MB $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 018c9d7b792b 2 days ago 1.22MB ubuntu latest 1e4467b07108 5 days ago 73.9MB
Therefore when you use option “-a” or “–all“, it will remove both dangling and unused docker images.
Delete All unused docker objects
To delete all unused data in order (stopped containers, networks not used , dangling image etc.,) use the following command.
$ docker system prune -a WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N] y Deleted Containers: ce8ed57053339454c8870adc807dad9b9e7db6a97af0ddac852672d713be1c7f 5abcfb446878d16f8bbc82037c5505c22f1ca45d6c48907c1ab6caa0e53424a7 d41a74c34984b7c056657723d55ed66543019cf11dbeca603eeb669586a1d1b0 Deleted Images: deleted: sha256:4e6d127b5544c1d9922704683f6e4458a4f10db85be732c33895adb8253d09a0 untagged: busybox:latest untagged: busybox@sha256:4f47c01fa91355af2865ac10fef5bf6ec9c7f42ad2321377c21e844427972977 deleted: sha256:018c9d7b792b4be80095d957533667279843acf9a46c973067c8d1dff31ea8b4 deleted: sha256:514c3a3e64d4ebf15f482c9e8909d130bcd53bcc452f0225b0a04744de7b8c43 untagged: ubuntu:latest untagged: ubuntu@sha256:5d1d5407f353843ecf8b16524bc5565aa332e9e6a1297c73a92d3e754b8a636d untagged: gcr.io/sneppets-gcp/hellodocker-image:latest untagged: gcr.io/sneppets-gcp/hellodocker-image@sha256:20da7a3d10a6cdbc541a1f582530907b13a98429e594fa8b56e0236cb187a0e1 deleted: sha256:40041a9ff2e2d5a5e2ed368b09a94ff20f55208c52d072fdc74726060f8219a7 deleted: sha256:2731f56a29a1666251c705101281c6b46700989018f64c76183fcce1086c9fc4 deleted: sha256:beee9f30bc1f711043e78d4a2be0668955d4b761d587d6f60c2c8dc081efb203 untagged: ubuntuv1:latest deleted: sha256:f5914386e10761361857beb08a00e5b20df6b4828cc31278ec85a3a0db0058eb deleted: sha256:1e4467b07108685c38297025797890f0492c4ec509212e2e4b4822d367fe6bc8 deleted: sha256:7515ee845913c8df9826c988341a09e0240e291c66bdc436a067e070d7910a1f deleted: sha256:50ebe6a0675f1ed7ca499a2ec7d8cc993d495dd66ca1035c218ec5efcb6fbb8c deleted: sha256:2515e0ecfb82d58c004c4b53fcf9230d9eca8d0f5f823c20172be01eec587ccb deleted: sha256:ce30112909569cead47eac188789d0cf95924b166405aa4b71fb500d6e4ae08d Total reclaimed space: 228.4MB
Hope this helped 🙂
Also See:
- Difference between CMD and ENTRYPOINT in Dockerfile ?
- How to run a command in a running docker container ?
- Ping inside the docker container from host
- Docker: Error response from daemon: OCI runtime create failed: container_linux.go:349
- List the directories inside the docker container
- Add new user to the Docker container using Dockerfile
- Docker images are storage location
- Difference between Docker Images, Containers and Registries
- Docker: Difference between ADD and COPY commands in a Dockerfile ?