Pages

Sunday, October 7, 2018

Prune in Docker


Once we start playing with containers , it is also necessary to clean things. There are many cases where none images will be created, containers that are stopped and not removed , networks created but not used, unused volumes etc that needs to be deleted..

The Prune command exist for every resource from images, containers, volumes and networks in docker. In this article we will see how we can purne each of these resources

Pruning Containers
Run the command “docker container prune” to remove all containers from the system that are not in running status. Docker will ask for a confirmation before deleting the containers. The containers that are in exited or created status will be deleted

jagadishwork$Sat Oct 06@ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
449d23d915cd3470804607985525ea217fe3caf16dbc48c58414f5ca9b7aa7f2

Total reclaimed space: 0B

Use the -f (force ) flag to skip the confirmation.
If we want to remove the running container even, we can use “docker container rm -f $(docker container ls -aq)

Pruning Images
If we want to free all space occupied by unused images layers we can use the command “docker image prune”.  Now what are  these unused image layers?

When ever we make a change , we are re-creating layers which means the previous version of the same layer is orphaned and modified with the changes that we do. On a system where we build images, the number of orphaned images layers can be large.  All these orphaned layers are removed using the above prune command

We can use the -a ( all ) flag to remove orphaned image layers as well as images that are not currently in use on the system using the “docker image prune --force --all

Pruning Volumes
Volumes are used for having a persistent access to data by containers. If we want to reclaim space occupied by volumes destroy the volumes using “docker volume prune

This command will remove all volumes that are not currently in use by at least one container.

Docker will not allow to remove volumes that are currently in use. This will be same if the volume is being used a stopped container. We need to first remove the container and then the volume. We can use a filter ( -f  ) flag which allows us to specify a set of volumes which we want to prune as “docker volume prune --filter ‘label=java’

Pruning Network
The last resource in docker that we want to prune is network. We can use the “docker network prune” to delete all unused network.

Prune Everything
Now if we want to remove everything we can use the “docker system prune” to remove unused containers, images, volumes and networks once at a time.

More to come, Happy Learning

No comments :

Post a Comment