Docker prune removes un­nec­es­sary objects like stopped con­tain­ers, unused volumes, and old networks or images. This helps optimize hard disk usage and frees up storage space.

What is Docker prune?

Docker prune or system prune is useful to sys­tem­at­i­cal­ly clean up Docker resources that are no longer needed, helping you free up memory. If Docker con­tain­ers, images, networks, and volumes remain unused for a long time, they can occupy a sig­nif­i­cant amount of disk space. With prune you can quickly eliminate these unused resources, keeping your system clean and per­form­ing to the best of its ability.

Es­pe­cial­ly in de­vel­op­ment en­vi­ron­ments, where new con­tain­ers are fre­quent­ly created and tested, a sig­nif­i­cant number of obsolete objects can ac­cu­mu­late. Docker prune ef­fec­tive­ly removes not only unused con­tain­ers and images but also networks and volumes that are no longer necessary. This stream­lines the man­age­ment of Docker resources and minimizes the risk of a cluttered, over­loaded system.

What is Docker prune used for?

The prune command is suitable for various sit­u­a­tions in daily work with Docker. Below we have listed four typical scenarios for its use.

Optimize work processes: Docker en­vi­ron­ments are fre­quent­ly utilized for de­vel­op­ment, testing, and pro­duc­tion. Without regular cleanup, this can lead to a rapid ac­cu­mu­la­tion of un­nec­es­sary resources. An overload like this not only increases memory usage but can also neg­a­tive­ly impact system per­for­mance. By removing unused resources, Docker prune enables a more efficient use of system resources. This is es­pe­cial­ly important in cloud-based en­vi­ron­ments, where costs rise with the demand for storage and computing resources.

Increase security: Redundant and obsolete Docker resources can present security risks. Docker prune removes unused con­tain­ers, images, networks, and volumes, reducing the chances of leaving outdated and po­ten­tial­ly vul­ner­a­ble com­po­nents un­pro­tect­ed. Main­tain­ing a clean Docker ecosystem minimizes the attack surface and enhances the overall security posture of your IT in­fra­struc­ture.

Increase per­for­mance: An over­loaded Docker en­vi­ron­ment can hinder system per­for­mance by consuming un­nec­es­sary resources like CPU and memory. Regular cleanup with Docker prune frees up disk space and enhances the ef­fi­cien­cy of container man­age­ment and execution. This results in sig­nif­i­cant­ly shorter loading times, faster ap­pli­ca­tion de­ploy­ments, and improved overall system re­spon­sive­ness.

Sim­pli­fied main­te­nance: Running Docker prune regularly sig­nif­i­cant­ly sim­pli­fies the main­te­nance of your Docker en­vi­ron­ment. By removing unused resources, it reduces the com­plex­i­ty of man­age­ment tasks for de­vel­op­ers. This makes it easier to keep track of active and relevant Docker com­po­nents, ac­cel­er­at­ing trou­bleshoot­ing. As a result, teams save time on ad­min­is­tra­tion and can focus more on de­vel­op­ing and enhancing their ap­pli­ca­tions rather than dealing with redundant resources.

What is the syntax of Docker prune?

Do you have a Docker en­vi­ron­ment and want to clean up resources to free up disk space and simplify man­age­ment? In this case, simply execute the following command to access prune services:

docker system prune
bash

By default, Docker iden­ti­fies stopped con­tain­ers, unused images, networks, and volumes that can be cleaned up and prompts you for con­fir­ma­tion before removal. Simply enter y and press the Enter key to proceed. Al­ter­na­tive­ly, you can use the -f option to skip the con­fir­ma­tion and run the cleanup im­me­di­ate­ly:

docker system prune -f
bash

Tip: It is useful to check which Docker resources are currently present on the system before using Docker prune. Execute the following commands to get a list of all con­tain­ers, images, networks, and volumes:

docker ps -a
docker images
docker volume ls
docker network ls
bash

Docker prune options

The following table gives you an overview of the different options of docker system prune.

Option De­scrip­tion
--all, -a Removes all unused Docker resources.
--force, -f Performs the cleanup without user con­fir­ma­tion.
--volumes Removes unused volumes.
--filter, -f Allows filtering the resources to be removed according to criteria.
--help Displays help on how to use the command.

How to apply the Docker prune command

Docker offers various commands that allow targeted cleanup of specific resource types. This gives you the flex­i­bil­i­ty to control exactly which resources should be removed.

Docker prune --filter

You can use Filter to control the selection of resources to be removed, such as con­tain­ers, images, networks, and volumes. This is ben­e­fi­cial in de­vel­op­ment en­vi­ron­ments where many temporary resources can be created and forgotten quickly.

The general syntax is:

docker <command> prune --filter "filter=<value>"
bash

The various filter options include:

  • until: Filters resources based on their creation date. Here, <duration> can be a numerical value with a unit such as h (hours), m (minutes) or s (seconds).
docker <command> prune --filter "until=<duration>"
bash

Example: Remove all stopped con­tain­ers that are older than 24 hours

docker container prune --filter "until=24h"
bash
  • label: Filters resources based on their Docker labels
docker <command> prune --filter "label=<label>"
bash

Example: Clean up all unused volumes that have the label mylabel

docker volume prune --filter "label=mylabel"
bash
  • dangling: Filters resources that are no longer used by a container
docker <command> prune --filter "dangling=true"
bash

Example: Remove all no longer used (dangling) images*

docker image prune --filter "dangling=true"
bash

In addition to the main filters mentioned above, there are also more specific filter options depending on the resource type:

  • before and since for images: Filters images based on their creation date, either before or since a certain point in time
  • exited for con­tain­ers: Filters con­tain­ers based on their exit status (for example exited=0 for suc­cess­ful­ly ter­mi­nat­ed con­tain­ers)

How to remove Docker resources

Step 1: Open a terminal or command line on your system.

Step 2: Run Docker prune to clean up the system:

docker {resource} prune
bash

Replace {resource} with the type of resources you want to clean up. Valid values are container, image, volume and network.

How to remove Docker con­tain­ers

By default, Docker does not au­to­mat­i­cal­ly release con­tain­ers after they have completed their lifecycle. It is important to regularly check the stopped con­tain­ers and clean them up as needed to free up blocked resources for running con­tain­ers. There are two methods for doing this.

  • docker rm: The docker rm command is intended to remove single or multiple Docker con­tain­ers manually. If you know exactly which con­tain­ers you want to delete, this is the ap­pro­pri­ate method. You must ex­plic­it­ly specify the IDs or names of the con­tain­ers you wish to remove, allowing you to delete only specific con­tain­ers from your en­vi­ron­ment without affecting others.

Here is an example to delete the con­tain­ers with the names container1 and container2:

docker rm container1 container2
bash
  • docker container prune: In contrast, docker container prune is a command that aims to remove several stopped con­tain­ers at once. This method is par­tic­u­lar­ly suitable for freeing up space quickly and ef­fi­cient­ly by deleting all stopped con­tain­ers that currently have no running processes.
docker container prune
bash

All stopped con­tain­ers are listed here, and Docker prompts you for con­fir­ma­tion before they are removed. This is very practical in en­vi­ron­ments where many temporary con­tain­ers are created and later become un­nec­es­sary.

For the daily man­age­ment and main­te­nance of your Docker en­vi­ron­ment, it is advisable to use docker container prune regularly to au­to­mat­i­cal­ly clean up stopped con­tain­ers. This approach saves time and ensures that no un­nec­es­sary resources impact system per­for­mance. In contrast, docker rm is ideal for deleting specific con­tain­ers in a targeted and con­trolled manner, whether for testing, de­vel­op­ment, or other specific needs.

How to remove Docker images

Docker images are the building blocks of con­tain­ers. They can have many different versions and tags over time. When you create or update new images, you are often left with old or unused images, known as dangling. These take up disk space on your system and can affect per­for­mance, es­pe­cial­ly if there are many such images.

With Docker prune you can identify and remove images that are no longer used:

docker image prune [Options]
bash

Possible options here are, for example:

  • -a, --all: This option not only removes dangling images, but also unused images. Unused images are those that no longer have tags and therefore cannot be ref­er­enced directly by tags.
docker image prune -a
bash
  • -f, --force: By default, Docker requests con­fir­ma­tion before images are actually deleted. With -f or --force you can skip this con­fir­ma­tion step and perform the cleanup im­me­di­ate­ly.
docker image prune -f
bash

How to remove all Docker images

You can also quickly and com­pre­hen­sive­ly clean up any Docker images on your system that are no longer required. To use Docker prune ef­fec­tive­ly, first open the terminal or command prompt and enter the following command:

docker image prune -a --force
bash

Ensure you review which images will be removed before you proceed with the cleanup. The -a option can delete unused images that you might still need.

How to remove Docker volumes

Volumes are per­sis­tent and retain data even when the as­so­ci­at­ed container is no longer running. Over time, these volumes can grow and consume resources un­nec­es­sar­i­ly.

docker volume prune
bash

This command searches for all volumes that have no active con­tain­ers. You will receive a list of volumes suggested for removal, and con­fir­ma­tion is required before the cleanup is executed.

In contrast, docker volume rm is a command that specif­i­cal­ly removes in­di­vid­ual Docker volumes. This can be useful if you know exactly which volume you want to delete, re­gard­less of whether it is currently used by a container. To remove a specific Docker volume, use the command docker volume rm followed by the name or ID of the volume:

docker volume rm volume_name
bash

Again, ensure you know the name or ID of the volume to be deleted, as this command per­ma­nent­ly removes the data and cannot be undone.

How to remove Docker networks

In Docker, networks are a fun­da­men­tal component that allows con­tain­ers to com­mu­ni­cate with each other, whether they are on the same host or spread across multiple hosts. Docker networks persist even after con­tain­ers are ter­mi­nat­ed or removed. Over time, this can result in an ac­cu­mu­la­tion of unused networks that consume resources un­nec­es­sar­i­ly. The docker prune network command is an easy way to identify and remove these unused networks.

The command has the following format:

docker network prune [OPTIONS]
bash

Docker generates a list of all networks that are no longer used by active con­tain­ers or are no longer ref­er­enced. Con­fir­ma­tion is then requested before the networks are actually removed.

Best practices for Docker prune

  • Automated cleanup: Use automated scripts or tools to regularly clean up Docker images, con­tain­ers and volumes that are no longer needed. This keeps the en­vi­ron­ment clean and optimizes per­for­mance.
  • Security patches and updates: Regularly check for security updates and patches for Docker engines, host operating systems, and all Docker images. Keeping these com­po­nents up to date helps minimize potential security vul­ner­a­bil­i­ties.
  • Mon­i­tor­ing and logging: Use mon­i­tor­ing and logging tools to monitor container per­for­mance, detect un­ex­pect­ed events and promote proactive main­te­nance.
  • External backup: Establish routines for external backups of important data from within Docker con­tain­ers, such as regularly backing up database contents or con­fig­u­ra­tion files.
  • Manual check before removal: Carefully check whether you no longer need certain resources before removing them. This will minimize the risk of ac­ci­den­tal data loss or in­ter­rup­tions to running processes.
Tip

If you’re just getting started with Docker, we recommend reading our com­pre­hen­sive Docker tutorial.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flex­i­bil­i­ty with no minimum contract
  • 24/7 expert support included
Go to Main Menu