Docker simplifies application deployment using containers, but over time, unused images, containers, and volumes can accumulate and consume disk space. Fortunately, Docker offers powerful tools for cleaning up these resources via the command line. This guide provides a quick reference to essential commands for removing Docker images, containers, and volumes to free up disk space and keep your system organized.
How to Use This Guide
This guide is designed in a cheat-sheet format with command snippets for quick reference. You can jump to any section that relates to your cleanup needs.
Note: The syntax $(command)
used in the examples is compatible with popular shells like bash, zsh, and Windows PowerShell.
For easy Docker deployment on a live server, check out DigitalOcean App Platform.
Purging Unused Resources (Images, Containers, Volumes, Networks)
Docker provides a single command to clean up unused resources, including images, containers, volumes, and networks:
- Remove unused resources:
docker system prune
- Remove all unused resources, including stopped containers and unused images:
docker system prune -a
Removing Docker Images
1. Remove Specific Images:
List all images, including intermediate layers, using docker images -a
, then remove the ones you don’t need:
- List Images:
docker images -a
- Remove Images:
docker rmi image1 image2
Note: Use the -a
flag to list all images, including those without tags. Use -f
with docker rmi
to force removal of untagged images.
2. Remove Dangling Images:
Dangling images are unused image layers. To remove them:
- List Dangling Images:
docker images -f dangling=true
- Remove Dangling Images:
docker image prune
3. Remove Images Based on Pattern:
Find images matching a pattern with grep
, then delete them using awk
and xargs
:
- List Images by Pattern:
docker images -a | grep "pattern"
- Remove Images by Pattern:
docker images -a | grep "pattern" | awk '{print $1":"$2}' | xargs docker rmi
4. Remove All Images:
To delete all images from your system:
- List All Images:
docker images -a
- Remove All Images:
docker rmi $(docker images -a -q)
Removing Docker Containers
1. Remove Specific Containers:
To remove a container by ID or name:
- List Containers:
docker ps -a
- Remove Containers:
docker rm container_id_or_name
2. Automatically Remove Containers on Exit:
To remove a container when it exits, use the --rm
flag:
- Run and Remove on Exit:
docker run --rm image_name
3. Remove All Exited Containers:
Find and remove containers that are in the “exited” state:
- List Exited Containers:
docker ps -a -f status=exited
- Remove Exited Containers:
docker rm $(docker ps -a -f status=exited -q)
4. Remove Containers Based on Multiple Filters:
Combine multiple filters to remove containers that meet different criteria:
- List Containers by Status:
docker ps -a -f status=exited -f status=created
- Remove Containers by Status:
docker rm $(docker ps -a -f status=exited -f status=created -q)
5. Remove Containers by Pattern:
Use grep
to filter containers by a pattern and delete them:
- List Containers by Pattern:
docker ps -a | grep "pattern"
- Remove Containers by Pattern:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
6. Stop and Remove All Containers:
To stop and remove all containers:
- Stop All Containers:
docker stop $(docker ps -a -q)
- Remove All Containers:
docker rm $(docker ps -a -q)
Removing Docker Volumes
1. Remove Specific Volumes:
To remove specific volumes:
- List Volumes:
docker volume ls
- Remove Volumes:
docker volume rm volume_name
2. Remove Dangling Volumes:
Dangling volumes are no longer connected to any containers. To remove them:
- List Dangling Volumes:
docker volume ls -f dangling=true
- Remove Dangling Volumes:
docker volume prune
3. Remove a Container and Its Volume:
To remove a container and its associated unnamed volume:
- Remove Container and Volume:
docker rm -v container_name
Conclusion
This guide covers the key commands for cleaning up Docker images, containers, and volumes. For more advanced options, check out the Docker documentation for detailed usage of docker system prune
, docker rmi
, docker rm
, and docker volume rm
. If you have any cleanup tasks you’d like to see covered, feel free to share your suggestions in the comments!
Leave a Reply