Docker is one of the most sought-after tools in development and DevOps. However, sometimes, there are a lot of images, containers, and volumes that are no longer needed. Their overabundance can lead to application slowdowns and disk clutter, so you need to get rid of this "garbage" occasionally.
Docker has enough features to clean the system periodically, and you can do it directly from the command line. Once you memorize the commands below, the cleaning process will not be much more complicated than using the "Recycle Bin" in the OS.
First, here is a command that removes everything, including so-called "dangling" objects:
docker system prune
To delete stopped objects, you will need the -a
flag, like this:
docker system prune -a
Images that are no longer used are destroyed like this:
docker image prune -a
This command also includes child images and metadata.
Now get rid of all containers:
docker container prune
And finally, get rid of the volumes:
docker volume prune
To ensure you are removing the correct objects, use the --dry-run
or -n
flags to view the objects without destroying them.
In addition, these commands delete objects without the ability to restore them. Therefore, before following the instructions above (and below), make sure that you understand what objects will be deleted and that you are not deleting something important.
Here, we will look at basic instructions for deleting a certain number of images with or without specified parameters.
To do this, first type:
docker images -a
The command allows you to display a list of all Docker images on the local machine, including those not currently in use. The -a
flag tells Docker to display all available images, not just recently created.
Next, enter:
docker rmi Img Img
Instead of Img
, substitute the image names to be deleted. Multiple names should be separated by a space.
To get a list of unused images (such objects are also called "dangling"), type:
docker images -f dangling=true
Such images can occur when creating new images based on old ones with the same name but without the tag. In this case, the old image becomes dangling and is not used. The -f
option filters objects by different criteria; in this case, we filter by the dangling=true
setting.
Now, to delete unused images, enter:
docker image prune
This command affects dangling images and those that are no longer in use. When it is executed, it will delete the images with no possibility of recovery.
To search for images that contain some pattern in their names, type:
docker images -a | grep "pattern"
-a
displays all images, including inactive and intermediate images. grep
filters the output to display only those images whose names contain the specified pattern (type it instead of "pattern"
, keeping the quotation marks).
Now, to remove them, type:
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi
awk '{print $3}'
only displays the third output column containing the identifiers. xargs docker rmi
is needed to pass identifiers to docker rmi
, which removes the given images. xargs
breaks the list of image identifiers into separate arguments. If you try to delete multiple images by listing their identifiers manually, you may run into a problem if the identifiers contain spaces or other special characters. In this case, using xargs
is safer and more convenient.
To delete images in use, type:
docker rm -f $(docker ps -a -q)
This will stop and destroy the containers before deleting the images.
To display a list of all images on the local machine, including those not currently in use, type:
docker images -a
Next, to delete all inactive images on the local machine, type:
docker rmi $(docker images -a -q)
This command outputs a list of IDs of all images on the local machine and passes it as an argument to docker rmi
, which deletes all Docker images without the possibility of recovery. To delete the active ones used by containers as well, enter:
docker rm -f $(docker ps -a -q)
In this chapter, we will cover all the basic instructions on how to delete a certain number of containers with or without specified parameters.
To get started, type:
docker ps -a
Now enter this command to delete:
docker rm Name_or_ID
To use it correctly, replace Name_or_ID
with the appropriate IDs or names (you can enter multiple objects separating them with spaces).
Also, to delete a container, you should stop it first. If the container is running, type:
docker stop Name_or_ID
To destroy all objects of this type on the local machine, enter:
docker rm $(docker ps -a -q)
To start a new container based on the specified image, enter, replacing image_name
:
docker run --rm image_name
The --rm
flag tells Docker to delete it after it stops.
Now, a little clarification. By default, the created container stays on the local machine after stopping. And if you don't want to keep the container, then use the --rm
flag. This is very convenient when launching temporary containers to perform some task, for example, running tests, when you don't want to leave "garbage" behind. Flags such as -v
for mounting volumes or -e
for passing environment variables will help to save data.
First, type:
docker ps -a -f status=exited
The -f status=exited
flag here filters the output.
Now, to delete, type:
docker rm $(docker ps -a -f status=exited -q)
To output containers with exited and created statuses, i.e. not running or currently running, type:
docker ps -a -f status=exited -f status=created
The -a
and -f
flags here tell Docker to display all containers and filter them.
Now, to delete all objects that are in exited or created states, type:
docker rm $(docker ps -a -f status=exited -f status=created -q)
To get a list of containers on the local machine, and then filter the list to display only those objects whose names have a particular pattern in them, enter by replacing "pattern"
with the desired pattern, keeping the quotation marks:
docker ps -a | grep "pattern"
Now, to delete objects whose names have the string "pattern"
in them, type:
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
This command uses grep
to filter the list, awk '{print $1}'
to extract the first column (identifier), and then passes the identifiers using xargs
. Let's add that docker ps -a | grep "pattern"
can output not only names but also other information: status, ID, etc. Therefore, use the commands above to delete objects with the string "pattern"
in their specified parameters.
To get a list of containers on the local machine, including those not currently running, enter the familiar command:
docker ps -a
It will show information about each object: identifier, name, status, utilized resources, etc.
To stop all containers on the local machine, enter:
docker stop $(docker ps -a -q)
And now delete them, like this:
docker rm $(docker ps -a -q)
In the final chapter, let's look at the basic commands for deleting a certain number of volumes with or without specified parameters.
To get a list of volumes on the local machine, type:
docker volume ls
The command outputs a list of available volumes, including information about them (IDs, names).
To delete a specified number of volumes with specified names, type, adding the volume name instead of vlm_name
:
docker volume rm vlm_name
This command takes a varying number of arguments. To delete multiple objects, list their names separated by a space:
docker volume rm my_volume1 my_volume2
If volumes are used by containers in a running state, they cannot be deleted unless those containers are stopped or deleted. Attempts to destroy such objects will result in an error output.
To get a list of dangling volumes on the local machine, type:
docker volume ls -f dangling=true
The command outputs a list of them, including their IDs and names. You can now get rid of them by typing:
docker volume prune
All objects of this type that are not associated with any container and are not currently in use will be deleted.
To delete volumes that were created automatically, type:
docker rm -v container_name
The -v
flag here tells Docker to delete all linked volumes. The point is that such volumes persist on the local machine unless deleted explicitly. Therefore, such an operation can be useful to free up the space occupied by unused volumes on the local machine.
In conclusion, before you perform the commands for deleting images, containers, and volumes, you should make sure that you or your team members will no longer need the objects you are deleting. After all, all of these operations are irreversible. That's all for now; we wish you success with Docker!