Docker doesn’t make any configuration changes to your system, but it takes up a lot of disk space. Use it for a short time, and you might be surprised to see some scary usage statistics returned when you type the following command:
docker system df
Copy the code
Fortunately, Docker allows you to reclaim disk space in images, containers, and volumes that you never use.
Regular pruning
To safely remove stopped containers, unused networks, and dangling images, it is best to run the following commands at regular intervals:
docker system prune
Copy the code
The riskier options are:
docker system prune -a
Copy the code
This will erase any images that are not related to the running container. This may be an exaggeration, but Docker will re-download any images it needs. The first attempt is a bit slower, but the image is then cached for future use.
The following sections describe other ways to delete specific items.
Image recovery
A Docker image is a disk snapshot of an application (such as a Web server, language runtime, or database management system). You can view all active and dangling images (container-independent images) by entering:
docker image ls -a
Copy the code
You can delete a Docker image by typing:
docker image rm <name_or_id>
Copy the code
You can add any number of mirrors to this command, separating them with a space character.
The container cleaning
Docker containers are running instances of images, 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
Copy the code
You can only delete the container after it has stopped. Stop the container by typing:
docker container stop <name_or_id>
Copy the code
You can then delete the container by typing:
docker container rm <name_or_id>
Copy the code
Similarly, you can add any number of space-delimited container names/ids to this command.
There is rarely a need to keep a stopped container. The rm option can be added to any container that is automatically deleted once the Docker run command terminates.
Network to sort out
Containers can be connected to a Docker-managed network, so they can communicate with each other. These are configuration files that don’t take up much disk space. View all Docker networks by entering the following:
docker network ls
Copy the code
You can delete one or more unused networks by typing the following:
docker network rm <name_or_id>
Copy the code
Similarly, you can add any number of space-separated network names/ids to this command.
Data volume processing
Docker data volumes are virtual disk images. It must be attached to the running container so that it can save files or other state information between reboots. The size of the volume depends on the application that uses it, but a typical database will require hundreds of megabytes of space, even if it is nearly empty.
To view all docker-managed disk volumes, enter the following command:
docker volume ls
Copy the code
Deleting a Docker volume permanently erases its data! 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 or export capabilities, such as 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 commands can back up the mysql database
docker exec mysql /usr/bin/mysqldump -u root -pmysecret mydb \
> backup.sql
Copy the code
Windows PowerShell equivalent command:
docker exec mysql /usr/bin/mysqldump -u root -pmysecret -r mydb | \
Set-Content backup.sql
Copy the code
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 target path, where the container’s name/ID is followed by a colon and its path to reference the container, for example
docker cp mycontainer:/some/file ./host/directory
Copy the code
Assuming that your data is secure, you can delete any unused volumes by typing:
docker volume rm <name>
Copy the code
You can remove all unused Docker volumes – those that are not currently connected to a running container – using the following method:
docker volume prune
Copy the code
Or, docker Volume Prune -A removes it all.
Comprehensive cleaning
Each unused container, image, volume, and network can be erased using a single command:
docker system prune -a --volumes
Copy the code
To force erasure without a confirmation prompt, add -f. Your system will revert to its original state without any Docker data.
The original link (dockerwebdev.com/tutorials/c…