Docker: Clean up and free up disk space

In this tutorial, I’ll walk you through how to clean up Docker to free up disk space on your server or computer.

Even though Docker container images don’t necessarily take up a lot of space, Docker can take up a significant amount of space on a computer, especially when:

  • Images can be updated regularly
  • We compile our own images
  • When we stop services

After a while, you can have several dozen gigabytes of disk space used by files that are no longer used.

We will see how to recover this space.

Show space used by Docker

To begin, to get an idea of ​​the total space used, you can use the following command:

sudo docker system df

This command shows us the disk space used by Docker, whether it is the container images and volumes or the build cache.

The command also tells us how much disk space can be reclaimed in the RECLAIMABLE column.

To clean, you can go about it in the following ways:

  • Clean everything at once with one command
  • Place multiple orders to delete what you want

We’re going to see both.

Remove all unused items in Docker

The easiest and fastest way to clean up in one go is with the following command:

sudo docker system prune --all

The order requires confirmation

Wait while cleaning:

Once the cleaning is done, the command returns the space that was freed up to us.

In total 1.47GB of disk space was freed, which is more than what was given by the docker system df command.

This is explained by the cleaning of the build cache which had not been taken into account.

This solution is practical because it allows you to quickly clean everything except the volumes.

Supprimer les éléments inutilisés de Docker manuellement

On va voir comment supprimer les éléments suivants :

  • Images
  • Volumes
  • Réseaux
  • Cache des builds

A chaque fois la commande demande une confirmation, pour forcer l’exécution de la commande, il faut ajouter le paramètre -f.

Docker: Remove Unnecessary Images

Before deleting the images, you can use the following command to view them:

sudo docker image ls

Remove container images that are no longer useful:

sudo docker image prune --all

Docker: Delete volumes no longer in use

Deleting volumes may result in data loss, before deleting volumes, make sure you no longer need them.

To list volumes, enter the following command:

sudo docker volume ls

To view the details of a volume:

sudo docker volume inspect <volume-name>

Enter the following command to delete volumes that are no longer in use:

sudo docker volume prune --all

Delete unused networks

To delete networks:

sudo docker network prune

Clear build cache

To delete the cache of images you have built enter the following command:

sudo docker builder prune --all

Docker cleaning is now no longer a secret for you.




Leave a Comment