Prune unused Docker objects (original) (raw)

Docker takes a conservative approach to cleaning up unused objects (often referred to as "garbage collection"), such as images, containers, volumes, and networks. These objects are generally not removed unless you explicitly ask Docker to do so. This can cause Docker to use extra disk space. For each type of object, Docker provides a prune command. In addition, you can use docker system prune to clean up multiple types of objects at once. This topic shows how to use these prune commands.

The docker image prune command allows you to clean up unused images. By default, docker image prune only cleans up dangling images. A dangling image is one that isn't tagged, and isn't referenced by any container. To remove dangling images:

To remove all images which aren't used by existing containers, use the -aflag:

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 filtering expressions with the--filter flag. For example, to only consider images created more than 24 hours ago:

Other filtering expressions are available. See thedocker image prune referencefor more examples.

When you stop a container, it isn't automatically removed unless you started it with the --rm flag. To see all containers on the Docker host, including stopped containers, use docker ps -a. You may be surprised how many containers exist, especially on a development system! A stopped container's writable layers still take up disk space. To clean this up, you can use the docker container prune command.

By default, you're prompted to continue. To bypass the prompt, use the -f or--force flag.

By default, all stopped containers are removed. You can limit the scope using the --filter flag. For instance, the following command only removes stopped containers older than 24 hours:

Other filtering expressions are available. See thedocker container prune referencefor more examples.

Volumes can be used by one or more containers, and take up space on the Docker host. Volumes are never removed automatically, because to do so could destroy data.

By default, you are prompted to continue. To bypass the prompt, use the -f or--force flag.

By default, all unused volumes are removed. You can limit the scope using the --filter flag. For instance, the following command only removes volumes which aren't labelled with the keep label:

Other filtering expressions are available. See thedocker volume prune referencefor more examples.

Docker networks don't take up much disk space, but they do create iptablesrules, bridge network devices, and routing table entries. To clean these things up, you can use docker network prune to clean up networks which aren't used by any containers.

By default, you're prompted to continue. To bypass the prompt, use the -f or--force flag.

By default, all unused networks are removed. You can limit the scope using the --filter flag. For instance, the following command only removes networks older than 24 hours:

Other filtering expressions are available. See thedocker network prune referencefor more examples.

The docker system prune command is a shortcut that prunes images, containers, and networks. Volumes aren't pruned by default, and you must specify the--volumes flag for docker system prune to prune volumes.

To also prune volumes, add the --volumes flag:

By default, you're prompted to continue. To bypass the prompt, use the -f or--force flag.

By default, all unused containers, networks, and images are removed. You can limit the scope using the --filter flag. For instance, the following command removes items older than 24 hours:

Other filtering expressions are available. See thedocker system prune referencefor more examples.