Docker·57 questions

How to clean the system of unused Docker objects and free up disk space?

Answer

During the active development, building, and testing of applications in Docker, cumulative images, stopped containers, unused volumes, and networks gradually take up gigabytes of disk space on the host machine. Regular system cleanup helps maintain order and prevent a lack of disk resources on a continuous integration server or local computer.

The most versatile tool for deep cleaning is the built-in docker system prune command. It analyzes the state of the environment and removes all stopped containers, unused networks, and images that are not associated with any active or stopped container.

Before running the cleanup, it is important to understand the difference between dangling and unused objects. Dangling images are old versions of layers that lost their tags after the main image was rebuilt. By default, the system prune command removes only these, leaving images that are simply not running at the moment.

To remove absolutely all unused images and not just dangling ones, a special reset flag must be added to the cleanup command. This drastically reduces the volume of occupied space, but requires re-downloading base images during subsequent project builds.

In addition to the main objects, data volumes that often remain in the system after container deletion for security reasons require separate attention. Since such data is not automatically deleted by standard system cleanup, a separate volume management command with a flag for complete purging of unused entities is used to find and delete them.

Was this answer helpful?

More questions in this topic

Related questions from other topics