Kahibaro
Discord Login Register

5.2 Listing and Removing Images

Understanding Existing Images on Your System

When you start using Docker regularly, your system quickly accumulates many images. Some are actively used by containers, others are leftovers from experiments and builds. Being able to list and remove images is essential to keep your environment clean and your disk from filling up.

This chapter focuses on working with existing images only. It does not explain how to pull or build them, since those topics belong to other chapters.

Listing Docker Images

Docker keeps images in a local image store. To see what is currently available on your machine, you list images. The basic listing command shows an overview with key information such as repository, tag, image ID, and size. You can also adjust the level of detail and filter the result to make large collections manageable.

By default, the output groups images by repository name and tag. If the image originates from Docker Hub and has not been given a custom tag, you will see an automatically assigned tag such as latest. When an image has multiple tags, it appears multiple times in the listing, but they all refer to the same underlying image ID.

Boxed information in the output, such as the repository and tag, is crucial when you later decide which images to delete or which one to run.

Full vs Filtered Listings

The complete list of images contains visible top level images and also parent images that exist internally as layers. You can choose to display only the top level images or include intermediate ones, depending on what you are trying to inspect.

Filtering becomes important when the number of images grows. Filters can restrict the output based on reference patterns or labels, or only show images that meet specific criteria like being dangling or unused. This helps you avoid accidentally working with the wrong image when many share similar names.

There is also a way to return only the IDs of images instead of full detail. This is useful when you want to combine commands or script cleanups. In such cases, human friendly information is less important than machine friendly identifiers.

Identifying Unused and Dangling Images

Over time you will accumulate images that are no longer used by any containers. Some of these are clearly obsolete, others may be temporary images left after builds. Understanding the difference between regular, unused, and dangling images makes removal decisions safer.

A dangling image is usually an image that has no repository and no tag. It often appears as a leftover when you build new versions of an image. These images still consume disk space even though they are not associated with a meaningful name. They are safe to remove in most cases, as they are normally not referenced by containers.

Unused images are those that are not currently used by any existing container. They might still have meaningful names but are not tied to any running or stopped containers. Removing them frees space but also makes them unavailable without pulling or rebuilding them again.

Important rule: Removing dangling images is usually safe, but always check before removing images with valid names, since containers may still depend on them.

Removing Specific Images Safely

Once you have identified the image you no longer need, you can remove it using its name and tag or its ID. Removing by ID is precise and avoids ambiguity if multiple images share similar names. Removing by repository and tag is more readable and easier to remember.

If an image is still used by a container, the removal will fail until you remove or detach that container from the image first. This prevents you from accidentally breaking a container that depends on it. You must decide whether to delete the container, or to keep the image available for possible restarts.

Sometimes you may want to remove multiple images in one action. You can do this by specifying several image names or IDs, or by generating the list programmatically and passing it to the removal command. This is particularly useful when cleaning up tags for a project that has produced many intermediate versions.

Cleaning Up Dangling and Unused Images

Manual removal is practical for a few images, but long term use usually requires periodic cleanup. Docker provides a way to remove groups of images that meet specific conditions, such as being dangling or not used by containers.

You can instruct Docker to remove all dangling images. Since these have no usable name and are usually not referenced by containers, this is the most common automated cleanup action. You can also perform a more aggressive cleanup that includes all unused images, not only dangling ones.

When performing aggressive cleanup, remember that every removed image must be pulled or rebuilt if you need it again. For development machines this might be acceptable and even desirable, but for systems with slow networks or limited build capacity it can have a noticeable cost.

Important rule: Automated cleanup that removes all unused images can free a lot of space, but verify that you understand which images are considered unused on that system before running it.

Handling Conflicts and Forced Removal

Sometimes you attempt to remove an image and Docker reports that it is still in use, even though you believe you no longer need the container that references it. In this situation, inspect the containers on your system and confirm whether any running or stopped containers still rely on that image.

After you delete or prune those containers, the removal of the image will succeed. If you insist on keeping the containers, you must keep the image as well, since the container’s filesystem is based on it.

Forced removal is possible but should be used with care. It ignores some safety checks and will remove the image even if containers reference it. This can immediately break dependent containers or leave them in an unusable state. For beginners it is better to adopt the habit of inspecting and cleaning containers first, then removing images.

Important rule: Avoid forcing image removal while learning Docker. First remove or inspect containers that use the image so you do not accidentally disrupt running workloads.

Practical Cleanup Strategy

A simple and safe strategy for beginners is to periodically list images, identify obviously obsolete ones, and remove them explicitly. Combine this with occasional removal of dangling images, which is usually low risk. As you become more confident, you can start using broader prune commands and filter options to automate more of the cleanup.

Maintain a mental or written map of which projects are active and which images belong to them. If you work in multiple projects, consider using naming conventions in repository and tag names so it is easier to see at a glance which images may be candidates for removal and which must stay.

By regularly listing and thoughtfully removing images, your Docker environment remains manageable, and you reduce clutter that can complicate future work.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!