Introduction
Docker makes it easy to wrap your applications and services in containers so you can run them anywhere. However, as you work with Docker, it’s also easy to accumulate an excessive number of unused images, containers, and data volumes that clutter the output and consume disk space.
Docker gives you all the tools you need to clean up your system from the command line. This cheat sheet-style guide provides a quick reference to commands that are useful for freeing disk space and keeping your system organized by removing unused Docker images, containers, and volumes.
Matching infrastructure at centron
From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →
How to Use This Guide
- This guide is in cheat sheet format with self-contained command-line snippets.
- Jump to any section that is relevant to the task you are trying to complete.
- Note: The command substitution syntax,
$(command), is available in many popular shells such as bash, zsh, and Windows Powershell.
Purging All Unused or Dangling Images, Containers, Volumes, and Networks
Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not tagged or associated with a container):
docker system pruneTo additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag:
docker system prune -aRemoving Docker Images
Remove one or more specific images
Use the docker images command with the -a flag to locate the ID of the images you want to remove. Then use docker rmi to remove them:
docker images -adocker rmi Remove dangling Docker images
To locate dangling images:
docker images -f dangling=trueTo remove them:
docker image pruneRemoving Containers
Remove one or more specific containers
Use docker ps -a to locate the containers you want to remove:
docker ps -adocker rm Remove all exited containers
To locate exited containers:
docker ps -a -f status=exitedTo remove them:
docker rm $(docker ps -a -f status=exited -q)Removing Volumes
Remove one or more specific volumes
Use the docker volume ls command to locate volumes:
docker volume lsThen use docker volume rm to remove them:
docker volume rm Remove dangling volumes
To locate dangling volumes:
docker volume ls -f dangling=trueTo remove them:
docker volume pruneConclusion to Remove Docker Images
This guide covers some of the common commands used to remove images, containers, and volumes with Docker.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.