Docker

How System Prune Works in Docker?

Docker is an open-source project development, and shipment platform frequently used to deliver projects and software in containers. It works with Docker containers, images, networks, volumes, and many other components for developing and deploying applications. However, many unused containers and images take up much of your system space during development.

This post will describe how system prune works to free up space in Docker.

How Does System Prune Work in Docker?

In Docker, “system prune” is a basic command that is utilized to remove unused data and files. Docker users can remove dangling Docker containers, images, volume, and network with the help of this command. This command can work in multiple ways, such as removing Docker volume using the “system prune” command along with the “–volumes” option.

To understand how system prune works in Docker, look at the provided instructions.

Step 1: Open Visual Studio Code

First, launch the Visual Studio Code Editor from the Windows “Startup” menu:

Step 2: Open Terminal

Next, open the command line terminal from the Visual Studio “Terminal” menu:

Step 3: List All Containers, Images, and Volume

To list down Docker containers, utilize the “docker ps -a” command. Here, the “-a” option is used to display all containers:

> docker ps -a

To list Docker images, use the “docker images” command:

> docker images

The Docker volumes are listed with the help of provided command:

> docker volume ls

Step 4: Use System Prune to Remove Dangling Components

Next, prune all dangling or unused Docker components, such as Docker containers, images, and networks utilizing the “docker system prune” command:

> docker system prune

Step 5: Verification

Verify if the dangling containers are removed or not:

> docker ps -a

It can be observed that we have successfully pruned Docker containers:

Next, list down Docker images using the “docker images” command:

> docker images

You can see that all unused images are removed:

Step 6: Use System Prune to Remove Stopped Components

In order to remove all dangling as well as stopped components, such as images and containers, utilize the “docker system prune” command along with the “-a” option:

> docker system prune -a

Now, again list down Docker images for verification:

> docker images

You can see that all unused and stopped images have been removed successfully:

Step 7: Prune Docker Components Along With Volumes

Docker volume can not be pruned directly by the “system prune” command. Users are required to specify the “–volumes” option with the mentioned command. For this purpose, go through the provided command:

> docker system prune --volumes

Next, list down all volumes using the “docker volume ls” command:

> docker volume ls

It can be observed that we have removed the Docker volumes:

Alternatively, to prune the system without getting a confirmation alert, utilize the “-f” option:

> docker system prune -a --volumes -f

We have demonstrated how system prune works in Docker.

Conclusion

System prune is a basic command used to remove unused Docker containers, images, volume, and associated networks. To remove all unused and stopped Docker containers, images, and networks, utilize the “docker system prune -a” command. In order to remove volume as well, utilize the “–volumes” option along with the “docker system prune” command. This blog has demonstrated how system prune works in Docker.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.