Docker takes up a lot of disk space over time. Enter the following command and you may see some scary usage statistics:

docker system df

This article will introduce some methods for cleaning up docker space, including how to clean up unused images, containers, and volumes.

In fact, the focus of all “cleaning” lies in the danshari mentality, which is similar to the dilemma of family organizing. If you’re a heavy Docker user, this article might help.

Regular cleaning

It is best to run the following command at regular intervals to safely remove stopped volumes, unused networks, and images:

docker system prune

To be bold, add the ‘-a’ option:

docker system prune -a

This clears any images that are not relevant to the running container. Generally there is no loss and Docker will re-download the image it needs if necessary. The only downside is that the first download will be slower. If you’re running out of disk space, this is a great way to help you trade time for space.

The following describes specific cleanup methods for specific items.

Image clearing

A Docker image is a disk snapshot of an application such as a Web Server, Runtime, or DBMS. You can view all active and pending images (that is, images not associated with the container) by typing the following:

docker image ls -a

Docker images can be deleted by typing:

docker image rm <name_or_id>

  • Tip: You can delete multiple mirrors at once by adding multiple mirror ids separated by Spaces

Container cleaning

A container is a running instance of an image, and the same image can start any number of containers. Containers are usually small because they are stateless and reference mirrored file systems. Enter the following command to view all running and stopped containers:

docker container ls -a

To delete a container, stop it:

docker container stop <name_or_id>

Then type the following to delete the container:

docker container rm <name_or_id>

  • Tip: It is also possible to delete multiple mirrors simultaneously with one command (multiple mirror ids separated by Spaces).
  • Tip: If the docker run command uses the “–rm” option, it will automatically delete the container once the container is stopped.

The Network is cleared

Communication between containers depends on the Network managed by Docker. A Network is a configuration file that does not take up much disk space. To view all network information, run the following command:

docker network ls

To delete unused networks, enter the following command:

docker network rm <name_or_id>

  • Tip: You can also delete multiple networks at the same time (multiple network ids separated by Spaces).

Clear Volume (Volume)

Volume is a virtual disk image. It is attached to a running container so that state information (such as files) can be saved after the container is restarted. The size of a Volume depends on its purpose. It is usually a big user of disk space, such as a typical database, which takes up hundreds of megabytes of space even when there is no data yet.

Run the following command to view information about all volumes:

docker volume ls

Note that Volume stores persistent state (files, data, etc.).

For example, database applications typically require a dedicated data export backup operation. Most database systems provide backup tools such as mysqldump in MySQL. You can use Docker Exec to perform backup operations inside the container.

For example, the following command can be used to backup a database named ‘mydb’ running in a container named ‘mysql’ to a file named ‘backup.sql’ in the current directory of the host machine with the mysql username ‘root’ and password ‘mysecret’ :

docker exec mysql /usr/bin/mysqldump -u root -pmysecret mydb > backup.sql

The equivalent command in Windows PowerShell is:

docker exec mysql /usr/bin/mysqldump -u root -pmysecret -r mydb | Set-Content backup.sql

You can also use the docker cp command to copy data files in and out of a running container:

docker cp mycontainer:/some/file ./host/directory

After ensuring a reliable backup of important data, specific volumes can be deleted with the following command:

docker volume rm <name>

Alternatively, you can delete all unused volumes using the following methods:

docker volume prune

Or, docker Volume Prune -A removes it all. It’s all backed up, isn’t it? 😀

Comprehensive cleaning

The last big move! Delete all unused images, containers, volumes, and networks:

docker system prune -a --volumes

  • The -f option indicates forcible clearing.

When the execution is complete, the Docker will revert to its original state without any data.