Sign in

Getting rid of Docker Unused Objects

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 keep

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?


Hey, I'm passionate about solving problems in simple and elegant approaches, always open to learning more and make some good friends...

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store