How to completely clean up your Docker data

  • 2021-11-02 03:36:43
  • OfStack

Periodic catalog trimming (prune)
Mirror eviction
Container cleaning
Network arrangement
Evaporation of rolls
A completely clean start

Docker won't make any configuration changes to your system... but it will take up a lot of disk space. (Using Docker) After a while, when you type the following command, you will return some terrible usage statistics:


docker system df

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

Periodic trimming (prune)

To safely remove stopped containers, unused networks, and hanging images, it is best to run the following commands at 1-time intervals:


docker system prune

The riskier options are:


docker system prune -a

This will also erase any images unrelated to the running container. There may be one extreme to this, but Docker will re-download the image it needs. The first download will be a little slower, but then the mirror will be cached for future use.

The following sections describe other ways to delete specific items.

Mirror eviction

An Docker image is a disk snapshot of an application, such as an Web service, a language runtime, or a data management system. You can view all images, whether running or suspended (container-independent), by entering the following:


docker image ls -a

1 Docker images 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 space characters.

Container cleaning

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


docker container ls -a

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


docker container stop <name_or_id>

The command to delete the container is as follows:


docker container rm <name_or_id>

Again, you can add any number of space-delimited container names or ID to this command.

It is almost unnecessary to keep stopped containers. You can add the--rm option to the docker run command to automatically delete the container after it terminates.

Network arrangement

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


docker network ls

Enter the following command to delete one or more useless networks:


docker network rm <name_or_id>

Again, you can add any number of space-separated network names or ID to this command.

Evaporation of rolls

The Docker volume is a virtual disk image. It must be attached to a 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.

All Docker managed disk volumes can be viewed by the following command:


docker system prune
0

Removing an Docker volume will erase its data forever! There is no turning back!

If you are developing a database-driven application, 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 functionality, such as the Export link in Adminer.

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

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


docker system prune
1

Equivalent command for Windows PowerShell:


docker system prune
2

You can also copy data files to or from a running container using the docker cp command. This is passed through the source path and the destination path, and 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 system prune
4

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


docker system prune
5

Alternatively, docker volume prune-a removes all volumes. After all, you have already backed up, haven't you?

A completely clean start

You can use a single command to clear each unused container, image, volume, and network:


docker system prune
6

If you want to force cleanup without a confirmation prompt, you can add-f. Your system will be restored to its original state without any Docker data.


Related articles: