September 2, 2020
September 2, 2020
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 Docker unused images, containers, volumes, and networks.
Fast and reliable performance – get cloud VPS hosting with Hostinger.
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]
NOTE: 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.
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 to know more about –filter flags, check out the official documentation page.
PRO TIP: In order to force executing any “remove” command at any given time, use the -f or –force flag.
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 on how to remove such container:
docker run --rm CONTAINER_ID
When you’re done, the container will be deleted automatically.
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 of 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.
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 unsued 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"
NOTE: 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.
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 easy to learn.
Here are some more examples of why Docker is such a popular container platform.
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.