Technology Security Analyst
The 'docker framework prune' is a Docker order that is utilized to eliminate or erase unused articles or information, it very well may be pictures, holder, volume, or organization as these items are not taken out except if we unequivocally eliminate those objects, nonetheless, we want to indicate the '- volumes' choice to eliminate volumes in Docker 17.06.1 or above. In the Ifk about pictures, it eliminates both hanging and unreferenced pictures yet, of course, it eliminates just hanging pictures. The 'docker framework prune' is just upheld with API variant 1.25 or above of the client as well as the daemon.
At the point when we execute the 'docker framework prune' order, it settles on an API decision to the Docker daemon, and the daemon looks for all unused articles on that host and eliminates those objects from the framework. The '- volumes' choice was included in Docker 17.06.1 because, in prior forms, it eliminates all items including volumes, to erase volumes then we need to run the 'docker compartment prune', 'docker network prune', and so forth independently.
Let’s create a few images, networks, containers, volumes, etc. to understand all options with examples:
Step: 1. We can check all available objects one by one using the below commands:
docker image ls
docker volume ls
docker network ls
docker ps -f “status=exited”
In the above snapshot, we can see that there are lots of objects, however, all objects are not useful so let’s remove objects that are not referenced by any container and stopped the container as well.
Step: 2. Let’s run the ‘docker system prune to remove the unused objects as shown below:
docker system prune
In the above preview, we can see that main compartments and organizations are got erased, in any case, pictures are not taken out because there are unreferenced pictures yet no hanging picture exists and volumes are likewise not taken out because Docker form is above 17.06.1 so we want to expressly utilize the '- volumes' choice to eliminate the volumes.
Step: 1. As we can see in the below image, there are no images are deleted in the previous command.
docker image ls
Step 2. We have to use the ‘–all’ or in short ‘-a’ option to delete the unreferenced images as shown below:
docker system prune –a
In the above picture, we can see that the pictures are eliminated now and around 95 MB of the room has been recovered. Presently, assuming we check the rundown of accessible pictures on the host, we can see that only one Docker picture is remaining because one holder is running involving that picture in the underneath depiction:
docker image ls
Step: 1. Let’s list the available volumes as we can that volumes are still not removed by any of the above examples:
docker volumes ls
Step: 2. Now, to remove the volumes as well, we need to explicitly use the ‘–volumes’ option as already discussed so let go ahead and use this option to delete all unused volumes as shown below:
docker system prune --volumes
In the above preview, we can see that another line has been included the 'Cautioning' area that shows volumes are likewise taken out on the off chance that it isn't utilized by somewhere around one compartment and after affirmation, it eliminated the unused volumes and around 54 MB space has been recovered.
Step: 1. Let’s create a few containers and networks with labels ‘env=prod’ and a few with labels ‘env=dev’ as shown below:
docker network create --label env=prod test-net1
docker network create --label env=dev test-net3
docker run --label env=prod alpine
docker run --label env=dev alpine
Step: 2. Now, let’s remove the unused objects which have the label ‘env=dev’ by using the ‘–filter’ option as shown below: –
docker system prune --filter “label=env=dev”
In the above depiction, we can see that the daemon is giving WARNING with regards to the referenced mark too and it has eliminated 'test-net3', 'test-net4', and the compartments simply because these have name 'env=dev', different organizations are as yet present. Additionally, no space has been recovered because these items are not using any space on the Docker have.
Step: 3. We can use the label with no condition to remove all the objects that do not meet the mentioned condition. For example, we can remove the objects with the label ‘env=prod’ using the below command:
docker system prune --filter “label!=env=dev”
In the above snapshot, we can see that the remaining objects are also removed, however, this command also deletes the objects that do not have any labels.
Step: 4. We can use the ‘until’ keyword with the ‘–filter’ option to remove objects that are created before a given timestamp or duration as shown below: –
docker system prune -a --filter “until=2m”
In the above snapshot, we can see that it has removed all unused objects created 2 mins ago. We can also use Unix timestamps or date formatted timestamps.
Note: We cannot use the ‘–volumes’ option with the ‘until’ keyword while using the ‘filter’ option.
Step: 1. Let’s assume we have to run this command weekly and want to automate this task using a script, we have seen in the above examples that we have to confirm the action before deletion but we don’t want the daemon to ask for confirmation otherwise script will get stuck. We can use the ‘–force’ or in short ‘-of to remove objects without prompting for confirmation as shown below:
docker system prune -a -volumes -f
In the above snapshot, we can see that daemon deleted all objects including volumes as well as unreferenced images without asking for confirmation.
It is an extremely helpful and simple order to have plate cleanup, notwithstanding, be careful before running this order. We should be certain enough with regards to the items that will be erased.
Subscribe to get latest updates