5.2. Listing and Removing Images
Table of Contents
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: 0
Where to Move
Move chapter:
- ☰ 1. Introduction
- ☰ 1.1. What is Docker?
- ☰ 1.2. Why Docker?
- ☰ 1.3. Virtual Machines vs Containers
- ☰ 1.4. When Not to Use Docker
- ☰ 1.5. How Docker Is Used in Real Projects
-
- ☰ 2. Core Concepts
- ☰ 2.1. Images
- ☰ 2.2. Containers
- ☰ 2.3. Docker Engine and Architecture
- ☰ 2.4. Docker Hub and Container Registries
- ☰ 2.5. Tags, Versions, and Digests
-
- ☰ 3. Installing Docker
- ☰ 3.1. Docker on Linux
- ☰ 3.2. Docker on Windows
- ☰ 3.3. Docker on macOS
- ☰ 3.4. Verifying the Installation
- ☰ 3.5. Common Installation Issues
-
- ☰ 4. Your First Docker Container
- ☰ 4.1. Running Your First Container
- ☰ 4.2. Understanding `docker run`
- ☰ 4.3. Interactive vs Detached Mode
- ☰ 4.4. Stopping and Removing Containers
- ☰ 4.5. Listing Containers
-
- ☰ 5. Working with Docker Images
- ☰ 5.1. Pulling Images from Docker Hub
- ☰ 5.2. Listing and Removing Images
- ☰ 5.3. Image Layers Explained
- ☰ 5.4. Image Size Optimization
- ☰ 5.5. Inspecting Images
-
- ☰ 6. Dockerfiles – Building Your Own Images
- ☰ 6.1. What Is a Dockerfile?
- ☰ 6.2. Basic Dockerfile Structure
- ☰ 6.3. Common Instructions (`FROM`, `RUN`, `COPY`, `CMD`, `ENTRYPOINT`)
- ☰ 6.4. Building Images with `docker build`
- ☰ 6.5. Best Practices for Dockerfiles
- ☰ 6.6. Multi-Stage Builds
-
- ☰ 7. Managing Data with Volumes
- ☰ 7.1. Why Containers Are Ephemeral
- ☰ 7.2. Bind Mounts vs Volumes
- ☰ 7.3. Creating and Using Volumes
- ☰ 7.4. Inspecting and Removing Volumes
- ☰ 7.5. Real-World Examples
-
- ☰ 8. Networking in Docker
- ☰ 8.1. Docker Networking Basics
- ☰ 8.2. Bridge Networks
- ☰ 8.3. Port Mapping
- ☰ 8.4. Container-to-Container Communication
- ☰ 8.5. Custom Networks
-
- ☰ 9. Docker Compose
- ☰ 9.1. Why Docker Compose?
- ☰ 9.2. `docker-compose.yml` Structure
- ☰ 9.3. Defining Services, Networks, and Volumes
- ☰ 9.4. Running Multi-Container Applications
- ☰ 9.5. Web App and Database Example
- ☰ 9.6. Docker Compose Best Practices
-
- ☰ 10. Debugging and Monitoring Containers
- ☰ 10.1. Viewing Logs
- ☰ 10.2. Executing Commands in Running Containers
- ☰ 10.3. Inspecting Containers
- ☰ 10.4. Resource Usage
- ☰ 10.5. Common Errors and Fixes
-
- ☰ 11. Security Basics
- ☰ 11.1. Running Containers as Non-Root
- ☰ 11.2. Image Trust and Verification
- ☰ 11.3. Secrets and Environment Variables
- ☰ 11.4. Reducing Attack Surface
- ☰ 11.5. Security Best Practices
-
- ☰ 12. Performance and Optimization
- ☰ 12.1. Reducing Image Size
- ☰ 12.2. Caching and Layer Ordering
- ☰ 12.3. Resource Limits
- ☰ 12.4. Choosing the Right Base Image
-
- ☰ 13. Docker in Development Workflows
- ☰ 13.1. Docker for Local Development
- ☰ 13.2. Hot Reloading with Volumes
- ☰ 13.3. Docker and Git
- ☰ 13.4. Docker in CI/CD Pipelines
- ☰ 13.5. Common Development Patterns
-
- ☰ 14. Deployment Basics
- ☰ 14.1. Running Containers in Production
- ☰ 14.2. Environment Configuration
- ☰ 14.3. Restart Policies
- ☰ 14.4. Logging Strategies
- ☰ 14.5. Basic Scaling Concepts
-
- ☰ 15. Beyond the Basics
- ☰ 15.1. Docker Swarm Overview
- ☰ 15.2. Kubernetes vs Docker
- ☰ 15.3. Container Registries
- ☰ 15.4. When to Move Beyond Docker
-
- ☰ 16. Practical Projects
- ☰ 16.1. Dockerizing a Simple Web App
- ☰ 16.2. Dockerizing a Backend API
- ☰ 16.3. Full-Stack App with Docker Compose
- ☰ 16.4. Common Project Pitfalls
-
- ☰ 17. Troubleshooting and FAQ
- ☰ 17.1. Common Beginner Mistakes
- ☰ 17.2. Docker Command Cheat Sheet
- ☰ 17.3. FAQ
- ☰ 17.4. Useful Resources
-
Comments
Please login to add a comment.
Don't have an account? Register now!