How to completely clean up your docker data


Docker will not make any configuration changes to your system… But it will take up a lot of disk space( After a while, when you enter the following command, some terrible usage statistics will be returned:

docker system df

Fortunately, docker allows you to reclaim disk space from unused images, containers, and volumes.

Prune regularly

In order to safely delete stopped containers, unused networks and suspended images, it is best to run the following commands at regular intervals:

docker system prune

The more risky options are:

docker system prune -a

This will also erase any mirrors that are not related to the running container. This may be a bit extreme, but docker will download the required images again. The first download will be slightly slower, but then the image will be cached for future use.

The following sections describe other ways to delete specific items.

Mirror expulsion

Docker images are disk snapshots of applications such as web services, language runtime, or data management systems. You can view all images, whether running or suspended (container independent) by entering the following:

docker image ls -a

A docker image can be deleted by entering the following command:

docker image rm <name_or_id>

You can add any number of mirrors to this command — separate them with a space character.

Container cleaning

Docker container is an instance of mirror operation, and any number of containers can be started from the same container. Containers are usually small because they are stateless and reference mirrored file systems. View all running and stopped containers by entering the following command:

docker container ls -a

Once a container is stopped, you can delete it. The command to stop the container is as follows:

docker container stop <name_or_id>

The command to delete a container is as follows:

docker container rm <name_or_id>

Similarly, you can add any number of space delimited container names or IDs to this command.

There is little need to keep stopped containers. You can add the — RM option to the docker run command to automatically delete a container after it terminates.

Network arrangement

Containers can connect to the network managed by docker, so they can communicate with each other. These are configuration files that do not take up too much disk space. View all docker networks by entering:

docker network ls

You can delete one or more useless networks by entering the following command:

docker network rm <name_or_id>

Similarly, you can add any number of space separated network names or IDs to this command.

Evaporation of roll

Docker volumes are virtual disk images. It must be attached to the running container so that it can save files or other status information between restarts. The size of the volume depends on the application that uses it, but a typical database requires hundreds of megabytes of space even if it is empty in most cases.

You can view all the disk volumes managed by docker through the following command:

docker volume ls

Removing a docker volume will erase its data forever! There is no turning back!

If you are developing database driven applications, you can usually keep one or more data dumps that can be used to recreate a specific set of records. Most database client tools provide dump functions, such as the export link in administrator.

Most database systems will provide backup tools, such as mysqldump in MySQL   Utilities. You can use the docker exec command to perform these operations on a running container.

The following Linux / MacOS command backs up the MySQL database named mydb running on the container named Mysql to a file named backup.sql. MySQL root user with password mysecret:

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

Equivalent commands for windows PowerShell:

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 to or from a running container. This is passed through the source path and the target path. The container is distinguished by its name / ID, followed by a colon and its path, for example,

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

Assuming your data is secure, you can delete any unused volumes by entering the following:

docker volume rm <name>

You can delete all unused docker volumes – those volumes that are not currently connected to a running container using the following methods:

docker volume prune

Alternatively, docker volume prune – a deletes all volumes. After all, you have a backup, haven’t you?

Completely clean start

You can use a single command to clean up each unused container, mirror, volume, and network:

docker system prune -a --volumes

If you want to force cleanup without a confirmation prompt, you can add – F. Your system will be restored to the original state without any docker data.

This is the end of this article on how to completely clean up your docker data. For more information about docker data cleaning, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!