Remove unused Docker containers and images to reclaim disk space

nas

When using Docker to build containers that change often you may find your available disk space decreases quickly. This is often the case when developing applications where the dependencies keep changing. Find out how to reclaim space without risking data loss.

Reading time:
1 min

Removing unused containers, networks and images

To remove all unused containers, networks and images you can use docker system prune. I recently ran this on a server that had no disk space left and managed to recover over 30GB.

Example usage and output:

docker system prune

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Deleted Containers:
...

Deleted Networks:
...

Deleted Images:
...

Total reclaimed space: 30.57 GB

Removing unused volumes

Optionally you can also remove unused volumes, however this must be considered carefully due to the possibility of data loss. Any volume that is not currently being used by a container will be removed!

docker system prune --volumes
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all volumes not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y

...

Total reclaimed space: 13.5 MB

Other options

To see all the available options you can run docker system prune --help which will print out the following:

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. "label=<key>=<value>")
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

For further information and examples refer to the Docker docs: https://docs.docker.com/engine/reference/commandline/system_prune/

Thank you for reading this article.
Please share if you liked it.