If you are reading this guide, the chances are that you know what docker does, but a reminder couldn’t hurt. It enables building, testing, and deploying apps as self-sufficient and portable containers that you can run anywhere.
The thing is that you have to order Docker if you want it to remove images and other objects that are not used. That means Docker can easily take a huge amount of disk space with unused data.
In this article, we want to explain how to use Docker to remove all images and other unused objects. It is an excellent way to declutter your system and keep it organized, and it will also create extra disk space you can use more smartly.
How to Remove Unused Objects
A simple one-line command will tell Docker to get rid of all objects it does not use:
docker system prune
The system will ask you to confirm your decision. Type “y” and hit enter to execute the command.
Please note that this will remove all build cache, dangling images, stopped containers, and networks that are currently not used at all.
You can use this useful addition to the command to tell Docker also to remove volumes that it doesn’t use.
docker system prune --force --volumes
We used another useful flag in the above command. The “force” command will prevent the system from asking you to confirm the execution of the above line. Use it only when you are sure what you are erasing.
How to Remove Stopped Containers
Have you used the “–rm” when you started the container? If the answer is no, Docker will not automatically delete the container even if you stop it. In this section, we will find out how you can delete them yourself.
Having a complete list of containers may be useful. You can discover that by typing the following line:
docker container ls -a
The system will show you the ID of the container and its status, as well as the image, ports, and other useful information.
The crucial thing here is the ID of the container. You should type it in the command used to remove it:
docker container rm container_id
Change the “container_id” with the ID of the container you want to remove and the system will perform the removal. Please note that the ID should contain a combination of letters and numbers.
Keep in mind that you cannot delete an active container, so make sure that you stopped it before running the above command.
The above instructions are great when you want to remove a specific container. A far quicker way is to remove all the containers that are not active. Here is the command you can use:
docker container prune
The system will ask you to confirm the removal.
Make sure to use the above line only when you are sure that you are not removing a container you might need. To check what containers you are removing, execute this line before running the prune command:
docker container ls -a --filter status=created --filter status=exited
How to Use Filters to Remove Containers
You can also choose which containers you will remove by using specific filters. The available filters are “label” and “until.”
The Label Filter
If you want to remove containers with a particular label, use the following command:
docker container prune --filter “label=<key>”
You can also order the system to remove all containers that do not contain a particular label. To do that, use the following commands:
docker container prune --filter “label!=<key>”
The difference is in the exclamation mark which signals the system to remove containers without the specified label.
Please note that you can also use another format of the “label” filter”
docker container prune --filter “label=<key>=<value>”
The same applies to remove files with or without the specified label.
The Until Filter
The other filter that you can use is the “until” one. It tells the Docker to remove all images made before a particular time.
For example, if you want to remove all containers older than a day, use the following line:
docker container prune --filter “until=24h
Alternatively, you can use this filter to remove all containers created before a specific date and time:
docker container prune --filter “until=2019-03-05T16:30:00
The above line will remove all containers created before March 5th, 2019, at 4:30 PM.
Remove Active Containers
If you want to remove containers, you first need to stop them. Here is how to stop containers with a single line:
docker container stop $(docker container ls-aq)
The trick is in using the list of active containers as the parameter for stopping them.
You can now remove them with this line:
docker container rm $(docker container ls-aq)
How to Use Docker to Remove All Images
The process is similar to the one you used for the containers. You should start by checking out the list of available images so that you can identify the ID of the image you need to remove.
Here is the command you should use:
docker image ls
The output will show you repository, image ID, and even the size of every image. You surely want to remove an image that you haven’t used for a while, so once you have identified it, use the following line for removal:
docker image rm image_id1 image_id2
Keep in mind that an image ID contains a combination of letters and numbers. You can add as many IDs as you like or remove only a single image.
The system may respond with an error message. That will happen in case one of the containers still uses the image you are trying to remove. Return to the previous steps of this tutorial and perform container removal first. After that, rerun the command in this section and Docker will remove all images you specified.
Remove Unused and Dangled Images
Docker has a command that you can use to remove any images that are unused and dangled. When no containers use the image, and it is not tag, then it is considered dangled.
Use the pruning command to execute the removal of dangled images:
docker image prune
Unless you use the “force” function, the system will ask you to confirm the removal, so go ahead and type “Y” and enter once that happened.
The crucial thing to pinpoint is that this will remove all the images that are not tagged! If you have built an image and forgot the tag it, it will be removed by the above command.
You can expand the removal command to include images that are not used by any container:
docker image prune -a
Once again, the system will ask you to confirm the action.
How to Use Filters to Remove Images
The process is similar like when removing containers and you can use both “label” and until filters.
Use the following form to utilize the “label” filter:
docker image prune --filter “label=<key>”
The above command will remove all images that contain the specified label. The alternative format you can use is “label=<key>=<value>” to specify the label.
If you want Docker to remove images without a specified label, use this command:
docker image prune --filter “label!=<key>”
As for the “until” filter, you have two available options. The first one is to specify a particular date and time:
docker image prune --filter “until=2019-02-08T15:16:00
The above line will remove all images created before 8th February 2019, at 3:16 PM.
The easier way might be to remove all images created until a particular deadline with the following command:
docker image prune --filter “until=6h
That will remove images that you created more than six hours ago.
How to Remove Docker Volumes
We are certain you understood the basics now, but let’s get through them one more time, except we will use volumes now.
Start by listing the volumes:
docker volume ls
After identifying the name of the volume to remove, run the following command:
docker volume rm volume_name1 volume_name2
The volume name is a combination of letters in numbers that can be quite long, so make sure to enter everything correctly.
The system might display an error if any container uses the specified volume. In that case, you want to perform container removal first.
If you want to get rid of all unused volumes at once, you can use the below line:
docker volume prune
Do not forget that you will need to confirm the removal.
How to Remove Networks
Once again, we start by using the list command:
docker network ls
Docker will display the name and ID of each network, as well as their scope and drivers.
Next, run the removal command:
docker network rm network_ID1 network_ID2
The removal will be finalized if the network is not used by an existing container. If you get an error, execute the container removal command first and then repeat the above line.
If you want to remove all unused networks, tell Docker that with this command:
docker network prune
You can also use the label and until filters to specify what networks you want to remove.
docker network prune --filter “until=6h” docker network prune --filter “until=2019-03-01T20:20:00” docker network prune --filter “label=<key>” docker network prune --filter “label!=<key>”
The first two will tell the system to remove all networks before the specified time. The latter two will specify to remove networks that contain (3) or do not contain (4) the specified label.
That concludes our tutorial on how to tell Docker to remove all images that you do not need anymore. The process is similar for containers, volumes, and networks, which is why we included them in the guide, too.
We hope that you enjoyed the tutorial and that you are now familiar with how to use Docker removal commands. Go ahead and try them out, and do not hesitate to ask questions if you have them!
Best Linux Distros for Developers and Programmers as of 2020
Linux might not be the preferred operating system of most regular users, but it’s definitely the go-to choice for the majority of developers and programmers. While other operating systems can also get the job done pretty well, Linux is a more specialized OS that was…
How to Install IntelliJ IDEA on Ubuntu
If you are looking for a convenient way to manage Java applications on Ubuntu, the solution may lie in the IntelliJ IDEA development platform. An excellent advantage of this option is that it is relatively suitable for beginners, but experienced users will also find it…