Is your Docker environment turning into a chaotic mess? Are unused containers and bloated disk space giving you headaches?
Managing a Docker environment efficiently is no easy task. That's why we're here to guide you through the process of Docker cleanup – the key to maintaining a streamlined and high-performing container setup.
In this comprehensive guide, we'll walk you through the common challenges faced in Docker management, provide practical solutions, and equip you with the necessary tools and techniques to efficiently clean up your Docker environment.
Get ready to regain control and achieve peak performance in your containerized world.
What is Docker Cleanup?
Docker Cleanup is a process of removing unwanted components of the Docker serving space and minimalism to the docker environment you manage.
Docker is today’s most commonly used product across software engineering. A typical day in the life of a DevOps Engineer involves working with multiple applications and containerizing them.
When you are involved in such a chaotic environment where you have to manage a lot of applications with different environments, it is important to understand to delete what is unnecessary.
This is where the concept of Docker Cleanup comes in. It is a process of removing unwanted components of the Docker serving space and minimalism to the Docker environment you manage.
Also Read: Docker Commands Cheat Sheet
Why is Docker Cleanup Important?
Now that we know what it is, let’s deep dive into why it is important that we learn about Docker cleanup.
Managing Memory Space
We can get so caught up with various projects that we often forget to clean up the resources that we no longer need.
Especially when you are running Docker applications on your PC. Docker Cleanup is necessary for disk management.
As the number of containers and images increases, the performance of docker reduces.
You may enhance Docker's overall performance by deleting unused resources like images and containers.
Security vulnerabilities might arise from obsolete or unused Docker images and containers.
If outdated software is still present in your Docker environment, vulnerabilities and defects found in those versions can be exploited.
When to Perform Docker Cleanup?
Images Not in Use
Docker images that are no longer required or have been replaced by more recent versions need to be removed as they might create ambiguity and occupy more space.
For example, consider you have several versions of an image called nginx pulled from Docker hub and you are trying to create a containerization environment for your production environment.
Isn’t it stressful to see all the different versions of images along with some different images you don’t require?
This is where the concept of “Docker Image Prune” comes into place. We will discuss more on that in the later sections.
If there are containers in a stopped state for more than a week that you might not need to use, Docker cleanup is the way to go.
For instance, let’s say you want to use nginx image from Docker hub for testing your application in a containerized environment.
In the system that you are testing, there may be many versions of nginx that you would have run and then eventually stopped. In such a situation, when you run your new version of nginx and it stops due to some failover.
When this happens, we tend to run “docker ps -a” command to list all the containers that are running and have been in a stopped state forever!
This will create ambiguity, confusion and consume unnecessary time.
In the software development process, it is important to maintain your environment clean and junk free. This is where we will use “Docker Container Prune”.
Unused Networks and Volumes
While working with docker, we often focus on images and containers. Rather, the focus has to be given to docker networks and volumes as it can create more confusion if there are too many of them.
The concept of networks and volumes in itself is a popular concept when you consider it anywhere.
Be it cloud or on-premises. If containers and images can create confusion, imagine how much inefficiencies unused networks and storage of docker can create.
We create docker networks to build different kinds of connections to the containers we create and deploy.
Docker networks become a huge concept when you see them in complex environments. You won't be able to identify which network is connected to which container if you don't maintain the environment well.
This is why cleanup is important. The same applies to volumes as well. Consider a scenario where you have a bunch of containers used for some application you had built in the past.
Then, you would have used volumes there to persist your container data as a backup. When you don’t clean up your environments often, the new containers that you provision can face confusion.
To maintain networks and volumes, we use the commands “docker network prune” and “docker volume prune” respectively.
Also Read: Kubernetes vs Docker Swarm
Docker Prune Command
So what is the methodology to do the docker cleanup and ensure the docker environment is maintained properly? Below are the details of everything you need to know about it.
It works as a strong tool for clearing out unwanted Docker resources, such as images, containers, volumes, and networks. There are different commands to remove each of the resources created in docker.
- docker image prune: Removes unused images.
- docker container prune: Deletes stopped containers.
- docker volume prune: Cleans up unused volumes.
- docker network prune: Removes unused networks.
- docker system prune: Performs a comprehensive cleanup by removing unused images, containers, volumes, and networks.
These individual prune commands can be used when you want to clean up specific types of resources selectively.
It's important to exercise caution when using the docker prune command, as it permanently removes Docker resources.
Always review the resources that will be deleted before confirming the cleanup to avoid deleting anything you may still need.
Pre-requisites of Docker Cleanup
- Docker has to be installed in your system.
- Understand the impact of each of the prune commands.
- Ensure to take a backup of the important images that you might need later.
If you have persistent data stored in Docker volumes, review the contents and back up any important data before cleaning up volumes. This ensures that you don't unintentionally delete valuable data.
Docker Cleanup: A Complete Guide to Cleanup Docker Resources
In order to perform Docker cleanup, we have a set of commands and operations to perform cleanup. Open your command prompt in any operating system you use and run the following commands as needed.
Removing Docker Containers
docker container prune: This command is used to delete all the stopped containers in Docker. Its syntax is shown below.
docker container prune [OPTIONS]
[OPTIONS] in the above command could be:
- - filter: Provide filter values (e.g. until=<timestamp>)
- - force: This will skip asking for confirmation of deletion when used.
Let's look at a few examples of Docker container prune.
docker container prune –all: This command will delete all the containers. It will also display the reclaimed space on your disk.
docker container prune –filter until=2023-01-02T15:04:05: This Docker cleanup command will basically delete all the containers that were created before the said time of 2nd January 2023.
Also Read: Docker Image vs Container
How to Remove Docker Images?
docker image prune: This command removes all the unused images in docker. The syntax of this Docker image cleanup command is:
docker image prune [OPTIONS]
[OPTIONS] in the above command could be:
- --all , -a: Removes all unused images, not just dangling ones
- --filter: Provides filter values (e.g. until=<timestamp>)
- --force , -f: This will skip asking for confirmation of docker image deletion when used.
Here is an example showcasing how to use the docker image prune command to delete all the images contained in the system.
docker image prune –all
How to Cleanup Docker Volumes?
docker volume prune: Use this command to remove unused volumes in docker.
Here is the syntax of the above docker volume prune command.
docker volume prune [OPTIONS]
[OPTIONS] in the above Docker cleanup command could be:
- --all , -a: Remove all unused images, not just dangling ones
- --filter: Provide filter values (e.g. "until=24hr")
- --force, -f: This will skip asking for confirmation of volume deletion when used.
Here are a few examples/scenarios of the Docker volume prune command.
docker volume prune --filter all=true: This command is helpful for anyone that needs to go back to the old behavior and prune named volumes too, you need to add a filter to include named volumes:
Example #2 of Docker Volume Prune
Let's say you want to retain only volumes that contain Postgres db related volumes and delete any other volumes contained in the system.
You can use the filter flag to delete everything else apart from Postgres.
docker volume prune --filter label!=postgresql_db
Docker Volume Cleanup Example #3
If you want to force delete some volumes, you can use –force flag.
How to Purge a Network in Docker?
docker network prune: This Docker cleanup command is used to eliminate unwanted networks created for different containers.
The syntax of this Docker cleanup command for networks is:
docker network prune [OPTIONS]
In the above command, [OPTIONS] could be:
- --filter: Provide filter values (e.g. until=<timestamp>) to perform conditional Docker colume cleanup.
- --force, -f: This command will basically request not to prompt for confirmation for Docker volume deletion.
For example, the following command will delete all networks created before 2nd February 2023.
docker network prune –filter until=2023-02-02T12:00:00
Also Read: Kubectl Cheat Sheet
Removing Docker Systems
docker system prune: This Docker cleanup command is the most important command to use.
It has to be used with caution as it is used to remove all the resources of Docker present in the system. This command used in the following syntax:
docker system prune [OPTIONS]
Here are a few values you can use instead of "[OPTIONS]"
--all , -a: Remove all unused images, not just dangling ones
--filter: Provide filter values (e.g. until=<timestamp>) and watch this Docker cleanup command function as per your filter values.
--force , -f: This will skip asking for confirmation of Docker System deletion when used.
--volumes: Prune volumes
Let's look at a few examples of Docker System Prune.
docker system prune -a -f
This Docker System Prune command should be used with caution as it deletes everything. All the images, containers, networks, and volumes that are running or stopped altogether.
Use this ONLY if you don’t need any data from your previous environments.
Docker system prune –filter until=2023-05-04
This Docker System Prune command is used to delete all the docker components that were created before 4th May 2023.