You can identify unused Docker images using the 

docker images command with filters or by using the built-in docker image prune command. “Unused” can refer to two types of images: “dangling” images, which are untagged, or all images not currently associated with a container. 

List dangling images (untagged)

Dangling images are older versions of an image that are no longer tagged or referenced. You can see these layers by running: 

docker images --filter "dangling=true"

The output will show a list of images with <none> in the REPOSITORY and TAG columns. 

List all unused images

To list all images not associated with a container, including both dangling and tagged ones, combine the --filter flag with docker images. You can then use the --quiet or -q flag to see just the Image IDs. 

docker images -f "dangling=true" -q

You can also use this command to find all images, and then compare that with images used by running containers, as shown in this example: 

docker images --format "{{.ID}}" | grep -v "$(docker ps --format "{{.Image}}")"

Use the prune command

For a simpler, more powerful method that can also clean up other Docker resources, use the docker image prune command. 

Clean up dangling images only
This is the default behavior of docker image prune and is the safest option. 

docker image prune

Clean up all unused images
To remove all images that are not used by any existing containers (stopped or running), use the -a (or --all) flag. 

docker image prune -a

Clean up all system-wide unused resources
For a comprehensive cleanup that removes stopped containers, unused networks, and dangling images, use docker system prune. Add the -a flag to also remove all other unused images. 

docker system prune -a

Confirm before deleting
By default, the prune commands will prompt you for confirmation before deleting anything. To skip this prompt and force the deletion, add the --force or -f flag. 

Important considerations

  • Running containers: Docker will prevent you from removing an image that is still being used by a container.
  • Pruning is permanent: The prune command does not have a “dry-run” or “undo” option. Once you confirm, the images are deleted.
  • System vs. image pruning: Use docker image prune to target only images. Use docker system prune for a broader cleanup, but be aware that it affects other parts of your Docker environment. 

docker images –filter “dangling=true” REPOSITORY TAG IMAGE ID CREATED SIZE lscr.io/linuxserver/duckdns <none> 5ffaa03b018d 8 weeks ago 35.2MB linuxserver/duplicati <none> 2ec25d70accd 7 months ago 462MB lscr.io/linuxserver/duckdns <none> 20a6bb46ba3b 8 months ago 34.4MB lscr.io/linuxserver/duckdns <none> 3d00fd157f70 8 months ago 34.4MB wordpress <none> f5650165f084 10 months ago 701MB mariadb <none> 039bd724508b 2 years ago 410MB mariadb <none> a748acbaccae 2 years ago 410MB root@boomer-unraid:/mnt/user/tmp_files/docling/huggingface_cache# docker rmi 5ffaa03 Error response from daemon: conflict: unable to delete 5ffaa03b018d (cannot be forced) – image is being used by running container 82f5e61a6a58 Why does this happen?

The error conflict: unable to delete <image-id> (cannot be forced) - image is being used by running container <container-id> happens when you try to remove an image that is currently in use by an active container. Docker prevents this to avoid disrupting the container and potentially causing data loss or unexpected behavior. 

Even though you identified the image 5ffaa03b018d as “dangling,” this only means it lacks a user-friendly tag like lscr.io/linuxserver/duckdns:latest. It is not necessarily “unused”. When an image is updated or rebuilt, Docker assigns the new tag to the new image, and the old version becomes dangling. However, any container that was started from the old version will continue to use it until it is stopped and removed. 

How to resolve the error

To successfully delete the dangling image, you must first manage the container that is using it. You have a few options: 

Option 1: Stop and remove the specific container

  1. Stop the container: The error message helpfully provides the ID of the running container (82f5e61a6a58).shdocker stop 82f5e61a6a58 Use code with caution.
  2. Remove the container:shdocker rm 82f5e61a6a58 Use code with caution.
  3. Delete the image: After the container is gone, you can remove the dangling image.shdocker rmi 5ffaa03b018d Use code with caution.

Option 2: Use a single command to force-remove
You can also combine the process using a single, forced docker rm command. 

docker rm -f 82f5e61a6a58

This command first stops the container (-f flag) and then removes it. You can then delete the dangling image. 

Option 3: Use the prune command
A more efficient approach is to let Docker do the cleanup for you. The docker system prune command removes all stopped containers and dangling images. 

docker system prune

If you also want to remove all unused images (including non-dangling ones) and stopped containers, use the -a flag. 

docker system prune -a

This is often the safest and easiest method for clearing out old Docker resources, as it will skip any image still in use by a running container. 

How can I find unused images

The primary tool for finding and removing unused Docker images is the 

docker image prune command, with options for listing specific types of unused images. “Unused” can refer to images with no tags (dangling) or images that are not associated with any container. 

Find dangling images

Dangling images are older, untagged image layers that are no longer referenced. These often result from rebuilding an image with the same name and tag, which causes the previous version to lose its tag. 

To see a list of dangling images, run:

docker images --filter "dangling=true"

To list all images that are not used by any existing container (both running and stopped), you can use the prune command with the --all flag. This will give you a list of all images that would be removed, without actually deleting them yet. 

docker image prune --all --dry-run

Alternatively, you can get a list of all images and then filter out those used by containers: 

docker images --format '{{.ID}} {{.Repository}}:{{.Tag}}' | grep -v "$(docker ps -a --format '{{.Image}}' | xargs)"

Safely remove unused images

For a straightforward cleanup, use the docker image prune command. It will prompt you for confirmation before deleting anything.

  • Remove only dangling images:shdocker image prune Use code with caution.
  • Remove all unused images: To remove all images not associated with a container, use the --all (or -a) flag.shdocker image prune -a Use code with caution. 

Comprehensive system cleanup

For a full-system cleanup that removes all stopped containers, unused networks, and unused images, you can use docker system prune. This is a powerful command, especially when combined with the --all flag, so use it with caution in production environments. 

  • Show all potentially removable resources:shdocker system prune --all --volumes --dry-run Use code with caution.
  • Remove everything unused:shdocker system prune -a --volumes Use code with caution.You will be prompted to confirm before the command executes.

docker image prune –all –dry-run unknown flag: –dry-run See ‘docker image prune –help’.

unknown flag: --dry-run occurs because Docker’s prune commands do not have a built-in “dry-run” feature. The behavior of what gets pruned can change based on the state of your system, so a static list of objects to be removed would only be an approximation. 

Instead of a dry-run, you can use other methods to list and filter the unused resources you intend to clean up. 

How to preview what docker image prune -a will remove

1. Preview dangling images

To see only the dangling images that would be removed by docker image prune (without the -a flag), use the filter in the docker images command: 

docker images --filter "dangling=true"

2. Preview all unused images

To list all unused images (including both dangling and untagged images) that would be removed by docker image prune -a, you can use a combination of docker images and command-line tools. This command lists all images and then filters out any that are in use by a container, effectively showing you which images would be pruned: 

docker images --format "{{.ID}}\t{{.Repository}}\t{{.Tag}}" | grep -v "$(docker ps -a --format "{{.Image}}" | tr '\n' '|' | sed 's/|/|/g')"

Note: This command uses a few filters to clean up the output. The output will show images with the <none> tags first, followed by other unused images. 

3. Use the interactive prompt

When you run a prune command without the --force or -f flag, Docker gives you a warning and an interactive prompt that lists the resources it is about to remove. 

For example, running docker image prune -a will produce output similar to this:

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N]

Before you type y to confirm, you have a chance to see and analyze what will be removed. You can then press N to cancel and proceed with caution. 

<em># Remove all unused images older than 24 hours</em>
docker image prune -a --filter "until=24h"

Clean up on a schedule

Instead of relying on an interactive dry-run, you can schedule regular, automated cleanup jobs using a tool like cron or a CI/CD pipeline. This ensures your Docker environment stays clean without manual intervention. 

For example, a nightly job can be set up to prune resources that have been unused for a while:

<em># Prune all system-wide resources older than 7 days</em>
docker system prune -a --force --filter "until=168h"

Leave a Reply