Nov 30, 2022
How to Remove All Docker Images, Containers, Volumes, Networks and Unused Resources
Docker is taking the world by storm because it is a valuable open-source platform that allows applications to run without using an actual virtual machine of any kind. This gives users the flexibility to run and test applications in software containers without the need to worry about compatibility, turning your virtual private server into a great development platform.
In this tutorial, we’ll teach you the basic commands every user should know, such as how to remove unused Docker images, containers, volumes, and networks.
Removing All Unused Docker Resources
Docker doesn’t remove any unused data by itself, which can result in cluttering your disk space.
If you’d like to remove all images, containers, and networks that are not associated with a container
docker system prune
This command will remove everything, including dangling images and stopped containers, as the following message will tell you:
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all dangling images - all dangling build cache Are you sure you want to continue? [y/N]
Notice that this will remove dangling Docker images only.
In order to include all unused images, execute the following command:
docker system prune -a
Here’s the warning message that you’ll see before continuing:
WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache Are you sure you want to continue? [y/N]
However, the previous two commands will not touch unused volumes – this is done deliberately for safety reasons.
If you’d like to remove that as well, use the following command:
docker system prune --volumes
Important! Take note that using both rm and prune commands is not reversible.
How to Remove Docker Images?
In order to view all of your Docker images, you can use:
docker images -a
If you want to remove Docker image or a few, you can use this command and list the IMAGE ID as shown here:
docker image rm IMAGE_ID
To remove dangling images that are most recent and untagged, we will use the “docker remove all images” command as shown here:
docker image prune
However, to remove Docker images that are present in existent containers that are also tagged, we can use this:
docker image prune –a
If you wish to remove all images, for example, that may fall under a specific time frame, use the command:
docker image prune -a --filter "until=24h"
If you want to know more about –filter flags, check out the official documentation page.
In order to force executing any “remove” command at any given time, use the -f or –force flag.
How to Remove Docker Containers?
Similarly to before, in order to see the list of all Docker containers, you will need to run this command:
docker container ls -a
If you want to remove a specific container, enter the CONTAINER ID as shown in this example:
docker container rm CONTAINER_ID
To remove all the containers that stopped in the application, follow this command:
docker container prune
Take note that this will remove all stopped containers. In order to view the list of what containers will be deleted using the beforementioned command, use the –filter flag:
docker container ls -a --filter status=created --filter status=exited
To limit the removal of containers that stopped, for example, according to the time frame, you can use the following command:
docker container prune --filter "until=24h"
If you want to remove a container once you’re done working with it, you start one by adding a –rm flag. Here’s an example of how to remove such container:
docker run --rm CONTAINER_ID
When you’re done, the container will be deleted automatically.
How to Remove Docker Volumes?
Volumes are used for multiple containers, and it is very likely there will be a number of either unused or stopped volume files.
These files are not removed automatically, neither is there a setting in Docker to do so, as that can cause significant loss or damage to data.
First, to get all Docker volume IDs, use the following command:
docker volume ls
If you want to remove a certain volume, use this command followed by the VOLUME NAME:
docker volume rm VOLUME_NAME
To remove all unused volumes using a single command, you can use the following:
docker volume prune
If Docker volumes have labels attached to them, you can also use this:
docker volume prune --filter "label!=keep"
In this example, the command will only remove those volume files which are not labeled and assigned with the “keep” label.
How to Remove Docker Networks?
Though Docker networks don’t take much disk space, it can cause some problems if unnecessary files are not cleared from the disk.
One problem is that it creates rules for iptables and bridge networks with routing table entries, which can cause some issues in the long run.
For a full list of NETWORK IDs, use the following command:
docker network ls
In order to remove a specific network, you can use:
docker network rm NETWORK_ID
If you wish to remove all unused networks, use the following command:
docker network prune
For a filter based on the time frame, 24 hours in this example, you can simply enter instead:
docker network prune --filter "until=24h"
If you get an error when trying to remove a certain network, it means that an existing container uses that specific network and will have to be removed before continuing.
Why Use Docker?
Working with Docker allows the app to run more smoothly, no matter the system, screen size, or web browser it is being used on. Docker makes it easy to deploy, launch, and then test applications without much difficulty. In short, it is quick, saves time, and is easy to learn.
Here are some more examples of why Docker is such a popular container platform.
- Cost-effective and cost-efficient. Unlike more traditional container platforms, Docker’s infrastructure is minimal, which helps to save the organization a lot of money on virtual machines and related equipment. Most importantly, Docker can be handled with fewer employees, which also helps save money if your budget’s too tight.
- Great overall compatibility. Docker allows the developers to create, run, and test applications on the container, ensuring a smooth operation on any system. It provides plenty of room for experimentation that enables software developers to make applications with fewer bugs. It also allows it to be more portable with easy to set up code bases.
- Faster productivity. One of Docker’s more prominent features is that it allows for a single command to take care of further steps. This adds more efficiency to your workflow and faster configuration of an application, increasing productivity.
- Multi-cloud platform compatibility. Docker allows multi-cloud computing, a method many systems all over the world have fully embraced it. Amazon Web Services (AWS) and the Google Compute Platform (GCP) are just some of the examples where Docker is highly favored. Many applications that, include OpenStack, Microsoft Azure, Chef, Puppet, and more, are highly compatible with Docker containers.
- Increased security. Docker is secure and fast, which means that each application running on Docker containers stays isolated from each other, causing fewer security issues in return. It allows the developer to take charge of the full management of the application and the traffic flow without causing server or security issues.
Nowadays, Docker is gaining considerable momentum and is used by many famous companies worldwide. It is one of the best platforms to date that allows more convenient development solutions, allows unlimited testing and experimentation to create applications quickly and efficiently.
In this tutorial, you’ve learned how to clear unused Docker images, containers, volumes, and networks. If you have any more questions, feel free to leave a comment down below.