For a good reason, Docker is severely conservative in maintaining unsed objects (often referred to as “garbage collection”), such as images, containers, volumes and networks. Docker does not remove these objects unless you explicitly do so. These objects are saved on the local machine (i.e dev machine that was generated Docker objects from), and as you are creating these ones, this can cause Docker to use extra disk space. Fortunately, Docker provides a flexible command to get rid of each type of object and reclaim more disk space.
Docker named prune the act of getting rid of unsed objects and provies a prune command for each type of object.
Prune all objects at once
Docker provide the docker system prune command as a shortcut to prune images, containers and networks
By the fact that Docker Volumes can store sensitive data, these are not pruned by default, and you must explicitly specify — volumes flag for docker system prune to prune volumes
Delete unused docker images
docker image prune command allows to clean up images that are not tagged and are not referenced by any container, it’s called dangling images.
To remove all images although is tagged, but is not associated or is not used by at least one existing containers, use the -a flag:
By default, you are prompted to continue. To bypass the prompt, use the -f or — force flag.
You can limit which images are pruned using a filtering expression with the — filter flag, the format is of “key=value” and if the is more than one filter, just pass multiple flags (e.g. — filer “foo=bar” — filter “bif=baz”)
You can check currently supported filters here
Delete stopped docker containers
By default, stopped container is not automatically removed (unless started with the — rm). It’s very common in development environment keep some containers within the scope of tests. A stopped container’s writable layers still take up disc space, and it´s can be prunned to freeing some disk space.
To clean this up, you can use docker container prune.
Doing so, all stopped containers are removed. To limit the scope you can use -filter flag
Delete unused docker volumes
Docker does not remove volumes automatically, because to do so could destroy data
Doing so, all unused volumes are removed. To limit the scope you can use -filter flag.
For instance, the following command only removes volumes which are not labelled with the
Delete unused networks
Docker networks don’t take up much disk space, but they do create
iptables rules, bridge network devices, and routing table entries. You can use docker network prune to clean up networks which aren’t used by any containers.
Pretty straigthfaword isn’t it?