How to use Docker prune – tutorial with examples

Docker prune removes unnecessary objects like stopped containers, unused volumes, and old networks or images. This helps optimise hard disk usage and frees up storage space.

What is Docker prune?

Docker prune or system prune is useful to systematically clean up Docker resources that are no longer needed, helping you free up memory. If Docker containers, images, networks, and volumes remain unused for a long time, they can occupy a significant amount of disk space. With prune you can quickly eliminate these unused resources, keeping your system clean and performing to the best of its ability.

Especially in development environments, where new containers are frequently created and tested, a significant number of obsolete objects can accumulate. Docker prune effectively removes not only unused containers and images but also networks and volumes that are no longer necessary. This streamlines the management of Docker resources and minimises the risk of a cluttered, overloaded system.

What is Docker prune used for?

The prune command is suitable for various situations in daily work with Docker. Below we have listed four typical scenarios for its use.

Optimise work processes: Docker environments are frequently utilised for development, testing, and production. Without regular cleanup, this can lead to a rapid accumulation of unnecessary resources. An overload like this not only increases memory usage but can also negatively impact system performance. By removing unused resources, Docker prune enables a more efficient use of system resources. This is especially important in cloud-based environments, 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 containers, images, networks, and volumes, reducing the chances of leaving outdated and potentially vulnerable components unprotected. Maintaining a clean Docker ecosystem minimises the attack surface and enhances the overall security posture of your IT infrastructure.

Increase performance: An overloaded Docker environment can hinder system performance by consuming unnecessary resources like CPU and memory. Regular cleanup with Docker prune frees up disk space and enhances the efficiency of container management and execution. This results in significantly shorter loading times, faster application deployments, and improved overall system responsiveness.

Simplified maintenance: Running Docker prune regularly significantly simplifies the maintenance of your Docker environment. By removing unused resources, it reduces the complexity of management tasks for developers. This makes it easier to keep track of active and relevant Docker components, accelerating troubleshooting. As a result, teams save time on administration and can focus more on developing and enhancing their applications rather than dealing with redundant resources.

What is the syntax of Docker prune?

Do you have a Docker environment and want to clean up resources to free up disk space and simplify management? In this case, simply execute the following command to access prune services:

docker system prune
bash

By default, Docker identifies stopped containers, unused images, networks, and volumes that can be cleaned up and prompts you for confirmation before removal. Simply enter y and press the Enter key to proceed. Alternatively, you can use the -f option to skip the confirmation and run the cleanup immediately:

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 containers, 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 Description
--all, -a Removes all unused Docker resources.
--force, -f Performs the cleanup without user confirmation.
--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 flexibility 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 containers, images, networks, and volumes. This is beneficial in development environments 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 containers 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 containers: Filters containers based on their exit status (for example exited=0 for successfully terminated containers)

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 containers

By default, Docker does not automatically release containers after they have completed their lifecycle. It is important to regularly check the stopped containers and clean them up as needed to free up blocked resources for running containers. There are two methods for doing this.

  • docker rm: The docker rm command is intended to remove single or multiple Docker containers manually. If you know exactly which containers you want to delete, this is the appropriate method. You must explicitly specify the IDs or names of the containers you wish to remove, allowing you to delete only specific containers from your environment without affecting others.

Here is an example to delete the containers 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 containers at once. This method is particularly suitable for freeing up space quickly and efficiently by deleting all stopped containers that currently have no running processes.
docker container prune
bash

All stopped containers are listed here, and Docker prompts you for confirmation before they are removed. This is very practical in environments where many temporary containers are created and later become unnecessary.

For the daily management and maintenance of your Docker environment, it is advisable to use docker container prune regularly to automatically clean up stopped containers. This approach saves time and ensures that no unnecessary resources impact system performance. In contrast, docker rm is ideal for deleting specific containers in a targeted and controlled manner, whether for testing, development, or other specific needs.

How to remove Docker images

Docker images are the building blocks of containers. 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 performance, especially 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 referenced directly by tags.
docker image prune -a
bash
  • -f, --force: By default, Docker requests confirmation before images are actually deleted. With -f or --force you can skip this confirmation step and perform the cleanup immediately.
docker image prune -f
bash

How to remove all Docker images

You can also quickly and comprehensively clean up any Docker images on your system that are no longer required. To use Docker prune effectively, 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 persistent and retain data even when the associated container is no longer running. Over time, these volumes can grow and consume resources unnecessarily.

docker volume prune
bash

This command searches for all volumes that have no active containers. You will receive a list of volumes suggested for removal, and confirmation is required before the cleanup is executed.

In contrast, docker volume rm is a command that specifically removes individual Docker volumes. This can be useful if you know exactly which volume you want to delete, regardless 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 permanently removes the data and cannot be undone.

How to remove Docker networks

In Docker, networks are a fundamental component that allows containers to communicate with each other, whether they are on the same host or spread across multiple hosts. Docker networks persist even after containers are terminated or removed. Over time, this can result in an accumulation of unused networks that consume resources unnecessarily. 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 containers or are no longer referenced. Confirmation 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, containers and volumes that are no longer needed. This keeps the environment clean and optimises performance.
  • Security patches and updates: Regularly check for security updates and patches for Docker engines, host operating systems, and all Docker images. Keeping these components up to date helps minimise potential security vulnerabilities.
  • Monitoring and logging: Use monitoring and logging tools to monitor container performance, detect unexpected events and promote proactive maintenance.
  • External backup: Establish routines for external backups of important data from within Docker containers, such as regularly backing up database contents or configuration files.
  • Manual check before removal: Carefully check whether you no longer need certain resources before removing them. This will minimise the risk of accidental data loss or interruptions to running processes.
Tip

If you’re just getting started with Docker, we recommend reading our comprehensive Docker tutorial.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included
Was this article helpful?
Page top