How can I remove old and unused Docker images?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Removing old and unused Docker images is an important maintenance task to keep your system tidy, save storage space, and ensure that your Docker environment runs efficiently. This article will guide you through the steps of identifying and removing these images from your system.
Understanding Docker Images
Docker images are the building blocks of your Docker containers. Each image is a snapshot of a filesystem with the application code and its dependencies. When you run an image, it becomes a container. Over time, as you upgrade applications or test new features, you accumulate images that are no longer required.
Why Remove Unused Docker Images?
- Save Storage Space: Old images consume valuable disk space that could otherwise be used for active workloads.
- Improve Performance: A cleaner environment means that Docker operations like building or pulling images execute faster.
- Better Organization: Reducing clutter helps in managing and locating the images you actually use.
Identifying Old and Unused Images
Determining which images are unused is a critical step before removal. Docker makes this relatively easy using the command line.
Listing All Docker Images
To list all Docker images stored locally, run the following command:
This command will display repository names, tags, image IDs, creation times, and disk sizes. However, it includes both used and unused images.
Finding Unused Images
Unused images can be identified as dangling images or images not associated with any container.
- Dangling Images: Images that do not have any tags and are usually remnants of previous builds.
- Use the following command to locate them:
- Images Not Associated With Containers:
- Locate these by listing all containers and cross-referencing with existing images. Executing the following command will provide this insight:
Removing Old and Unused Images
Removing Dangling Images
Once identified, dangling images can be removed using:
This command deletes all dangling images. For a more detailed cleanup (including unused containers and networks), use:
Removing Specific Images
If you want to remove a particular image by its ID or tag, use:
Removing All Unused Images
To remove all images not currently associated with containers, execute:
This command will prompt confirmation and then proceed to delete all unused images.
Automating Cleanup with Scripts
For regular maintenance, consider creating a script that wraps these commands and can be scheduled via cron:
Save this script, make it executable, and schedule it with cron to automate the cleanup.
Table: Key Docker Image Cleanup Commands
| Command | Action |
docker images | List all Docker images stored locally. |
docker ps -a | List all containers (running and stopped). |
docker images -f dangling=true | Show only dangling images (unused without tags). |
docker image prune | Remove all dangling images. |
docker system prune | Perform full cleanup of unused containers, images, networks, and volumes. |
docker rmi <image-id-or-tag> | Remove a specific image by ID or tag. |
docker image prune -a | Remove all unused images, irrespective of their status (dangling or not). |
Additional Considerations
- Backup Important Images: Before deletion, ensure you've backed up any images you may need later. Use
docker saveor private repositories for this purpose. - Automation: Regularly scheduled cleanups can prevent accumulation over time.
- Monitoring Disk Usage: Keep an eye on disk usage with
df -hor similar tools to ensure your Docker host runs optimally.
Proper maintenance of Docker images not only conserves resources but also helps you in managing your Docker environment more effectively. With the commands and practices outlined in this article, you can ensure your system remains both lean and organized.
Related reading
- How can I run bash in a docker container?
- How can I run containers detached and have them automatically removed when they exit?
- How can I run NodeJS in Docker with MongoDB and RabbitMQ?
- How can I run script automatically after Docker container startup
- How can I replace variables in annotation via Kustomize?
- How can I restart only one service by using skaffold?
- How can I start spring boot application in docker with profile?
- How can I use a local image as the base image with a dockerfile?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.