Docker Cleanup - A Guide for Clearing Images, Containers, and Volumes
Docker has revolutionized application deployment, offering flexibility and efficiency through its containerization technology.
As Docker environments evolve and grow, managing resources like containers, images, volumes, and networks becomes increasingly challenging.
Docker Cleanup addresses this challenge by providing a systematic approach to identify and remove unused or unnecessary resources.
In this blog, Let us explore the essential commands and techniques for cleaning up Docker resources effectively. Additionally, we will also see the significance of regular Docker cleanup routines in preventing resource clutter and mitigating security risks.
Lets get started!
Table of Contents
- Docker Cleanup: An Introduction
- Docker Essentials
- Docker Disk Space Management
- Docker Prune Command
- Removing Docker Images
- Removing Docker Containers
- Removing Docker Volumes
- Removing Docker Network
- Importance of Regular Docker Cleanup
- Docker Cleanup Command Cheatsheet
Docker Cleanup: An Introduction
Docker Cleanup is a process that involves managing resources used by Docker, a popular platform for developing, shipping, and running applications. When you work with Docker, you create containers, which are lightweight, portable environments that package an application and its dependencies.
Over time, these docker containers, along with other Docker resources like images, volumes, and networks, can accumulate and consume disk space. Thus, to prevent the accumulation of unused or unnecessary resources, Docker Cleanup helps to remove them. This process frees up disk space and improves system performance.
It works by identifying and deleting containers, images, volumes, and networks that are no longer in use or needed. Thus regularly using Docker Cleanup keeps systems organized, efficient, and clutter-free, ensuring applications run smoothly.
Docker Essentials
In Docker, an image is essentially a package containing all the necessary files and settings for an application to run. When you run an image, it creates a container, which is like a virtual environment where the application can operate independently.
Docker Containers can interact with each other through networks, which enable communication between them. Additionally, Docker volumes provide a way for containers to store and access data, even after the container is shut down.
Together, these components form the basis of Docker's containerization system, allowing for efficient and reliable application deployment and management.
For a deeper understanding of Docker containers, check out the link.
Docker Disk Space Management
It is necessary to regularly check the disk space occupied by Docker to ensure efficient resource management and prevent running out of disk space.
This helps to maintain system performance and avoid potential issues such as failed deployments or slowed container operations.
To check the disk space usage of Docker, you can use the following command,
docker system df
This command provides total disk space occupied by Docker components such as images, containers, volumes, and build cache. Here's a sample output.
○ → docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 4 0 2.863GB 2.863GB (100%)
Containers 0 0 0B 0B
Local Volumes 1 0 209.1MB 209.1MB (100%)
Build Cache 10 0 0B 0B
If you run docker system df -v
, Docker will provide more detailed information about the disk usage, including sizes of individual components, filesystem types, and mount points.
docker system df -v
Using the verbose option can be helpful when you need more detailed information for troubleshooting, analysis, or understanding the behaviour of Docker commands. Here's a sample output.
○ → docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
node latest 5199d6829c95 5 days ago 1.11GB 0B 1.108GB 0
mysql latest f3df03e3cfc9 7 days ago 585MB 0B 584.5MB 0
atatus-infra-agent 1.0.0 68547e56bd6c 2 weeks ago 347MB 0B 347.1MB 0
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
Local Volumes space usage:
VOLUME NAME LINKS SIZE
a5eb1d542fea4baf1e230d55eb57c6d1af4f26d2f122e1ce88ffa3315b5b4608 0 209.1MB
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
7wiqaoxjggbm regular 105MB 2 weeks ago 2 weeks ago 1 true
42jd60vtodvv regular 398B 2 weeks ago 2 weeks ago 1 true
ihxc4v6iaj3a regular 0B 2 weeks ago 2 weeks ago 1 true
cj1nnct3g02c regular 52.6MB 2 weeks ago 2 weeks ago 1 true
6jk6z52csgem regular 7MB 2 weeks ago 2 weeks ago 1 true
t0kjz0u3ot6b regular 0B 2 weeks ago 2 weeks ago 1 true
ryelnhbdosgn regular 0B 2 weeks ago 2 weeks ago 1 true
g6965smp8sv1 regular 0B 2 weeks ago 2 weeks ago 1 true
qjwfflqewqdw regular 105MB 2 weeks ago 2 weeks ago 1 true
y3525s5zwwbs regular 398B 2 weeks ago 2 weeks ago 2 true
Docker Prune Command
The Docker prune command is a clean up tool in your Docker environment. You can use Docker prune when your system is running low on disk space, or when you want to ensure that only necessary Docker components are occupying your system resources. The docker system prune
command removes unused data, including:
- All stopped Containers
- All networks not used by at least one container
- All dangling images
- All dangling build cache
It is particularly useful in development or testing environments where you are frequently creating and deleting containers or images.
docker system prune
○ → docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
aafb7f5c9e1e00e21e83e0c4d48e9bb02186ea9a3388afb7d607f052af43aa22
Total reclaimed space: 48B
By running Docker system prune, you can reclaim disk space by removing unnecessary resources.
If you want to force delete all the unused resources without confirmation, you can run this command,
docker system prune -f
This is useful when you want to automate Docker cleanup tasks or when you are confident about the resources you are deleting and don't need a confirmation step.
Just be cautious when using this option, as it will delete all unused resources without further verification.
Removing Docker Images
(i). Remove one or more specific image
docker images -a
This command provides a list of Docker images, listing both base images and intermediate layers. It displays information such as image IDs, tags, and additional details for each entry.
○ → docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
node latest 5199d6829c95 5 days ago 1.11GB
mysql latest f3df03e3cfc9 7 days ago 585MB
atatus-infra-agent 1.0.9 68547e56bd6c 2 weeks ago 347MB
Once you have identified the image/ images you want to remove, use the docker rmi
command followed by the image ID or tag.
- To remove a docker image by its ID,
docker rmi image_id
○ → docker rmi 5199d6829c95
Untagged: node:latest
Untagged: node@sha256:64c46a664eccedec63941dab4027c178a36debe08a232d4f9d7da5aca91cff3d
Deleted: sha256:5199d6829c9501975002f30375c1b1ff47c5a71b8bfa07d9696c33fa6fb42c7b
Deleted: sha256:500f6fb1ae68c70173ac5676635fd142add25dcdf53e5325d1210745526aba71
Deleted: sha256:cb9ae0245c58ae6f85c5fe5886bca018086a447180b8647a29f1ca87554fdc2c
Deleted: sha256:10e725eccfc92734937b04b3dfd60f6e7d4c5740c24d5d1c4215829ceceefc4d
Deleted: sha256:d3fb96d9acb6e6d933e3728b4ad5b940bb46bf1d94944ee3424f735c4b3a64c6
Deleted: sha256:41a13d456d84469e658d82f10bd734f302daab6f65c4cae441299b4a03d3a123
Note: The command docker rmi 5199d6829c95
was executed to remove the specified Docker image node:latest
. Following the command, Docker successfully untagged and deleted the specified image. Additionally, Docker identified and deleted other associated layers that were shared among multiple images, ensuring efficient disk space management. Docker employs similar behaviour for other commands that involve the manipulation of images and containers.
- To remove a docker image by its tag,
docker rmi image_tag
○ → docker rmi golang:latest
Untagged: golang:latest
Untagged: golang@sha256:b1e05e2c918f52c59d39ce7d5844f73b2f4511f7734add8bb98c9ecdd4443365
Deleted: sha256:e0aa2034f411a9f1a2480237a67461716a74dc096a5e74f07e17d30d3021aa8b
Deleted: sha256:0e966fff3a6ee66b5d6df4e515295b76ecb7a46cb9115d81226c8f4a1211994c
Deleted: sha256:92de0673715e39e1f2f9e02b74b2ee9d0a20edff0ab1e2edf890765b00b58ee4
Deleted: sha256:b7bd751ca66c5cfe91cb0325a8186b507188a9b5edb4709947e919c3861ad1b5
Deleted: sha256:6a2b24ee9cdc8a5fedbd84de9302a4c5357b37f3623ba2d1933582cf7e255dd0
- To remove multiple docker images at once,
docker rmi image1 image2 image3
- To remove all docker images at once,
docker rmi $(docker images -a -q)
(ii). Remove Docker Images with Filters
To remove Docker images with filters, you can use the docker image prune
command along with filtering options.
docker image prune --filter "your_filter"
By using filters with the docker image prune
command, you can specifically target and remove only the unused (dangling) images.
- To remove docker images that were created more than 24 hours ago
docker image prune --filter "until=24h"
○ → docker image prune --filter "until=24h"
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:8ff5c7cc34074ab24778653f4f6632b05440b97afe30a7c94f3e744750b4b03c
Total reclaimed space: 10B
- To remove docker images with a specific label
docker image prune --filter "label=unused"
- To remove dangling docker images
docker image prune --filter "dangling=true"
○ → docker image prune --filter "dangling=true"
untagged:mysql@sha256:091fe36a5591449a69d3011bf05cf083201e4c031101f071447bd985f5
deleted: sha256:6f2df62f26cc90e1fa08d55f31d4b13f009d3849b1aff23b77797e284336acca
Total reclaimed space: 743.9MB
(iii). Remove Images based on a Pattern
This is a specific approach to manage Docker images, focusing on filtering and removing images based on a pattern.
- First use the following command to list all the docker images,
docker images -a
○ → docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
golang latest e0aa2034f411 18 hours ago 823MB
mysql latest f3df03e3cfc9 7 days ago 585MB
atatus-infra-agent 1.0.9 68547e56bd6c 2 weeks ago 347MB
- Then use
grep "pattern"
to filter this list, showing only the images that match the specified pattern.
docker images -a | grep "node"
○ → docker images -a | grep "mysql"
mysql latest f3df03e3cfc9 7 days ago 585MB
docker images -a | grep "pattern" | awk '{print $1":"$2}' | xargs docker rmi
○ → docker images -a | grep "golang" | awk '{print $1":"$2}' | xargs docker rmi
Untagged: golang:latest
Untagged: golang@sha256:b1e05e2c918f52c59d39ce7d5844f73b2f4511f7734add8bb98c9ecdd4443365
Deleted: sha256:e0aa2034f411a9f1a2480237a67461716a74dc096a5e74f07e17d30d3021aa8b
Deleted: sha256:0e966fff3a6ee66b5d6df4e515295b76ecb7a46cb9115d81226c8f4a1211994c
Deleted: sha256:92de0673715e39e1f2f9e02b74b2ee9d0a20edff0ab1e2edf890765b00b58ee4
Deleted: sha256:b7bd751ca66c5cfe91cb0325a8186b507188a9b5edb4709947e919c3861ad1b5
Deleted: sha256:6a2b24ee9cdc8a5fedbd84de9302a4c5357b37f3623ba2d1933582cf7e255dd0
Deleted: sha256:b970d87779b54a60898c739c0545a878abca1dc0baabbdc74e5645782b20ef5d
Deleted: sha256:609a42498e5cd50cecf991708b09a6c29f321d00eccb994167d804b16af8a3dd
Deleted: sha256:072686bcd3db19834cd1e0b1e18acf50b7876043f9c38d5308e5e579cbefa6be
This command removes the specified Docker images.
Removing Docker Containers
(i). Remove One or More Containers
To remove one or more containers using Docker, you can use the docker rm
command followed by the the container ID or name of the containers you want to remove.
docker rm ID_or_Name ID_or_Name
○ → docker rm ac144553cdf7
ac144553cdf7
(ii). Remove a Container upon Exiting
To automatically remove a container upon exiting, you can use the --rm
option.
docker run --rm image_name
You can replace image_name
with the desired option you want to remove.
(iii). Remove all Exited Containers
If you specifically want to see containers that have stopped running, you can use the -f
flag with the status exited
. This helps you identify containers that are no longer active.
docker ps -a -f status=exited
○ → docker ps -a -f status=exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cb69341b9ad mysql "docker-entrypoint.s…" 2 minutes ago Exited (1) 2 minutes ago adoring_austin
After identifying you can pass their IDs to the docker rm
command using the -q
flag. The -q
flag simplifies the output of the docker rm
command by showing only the IDs of the containers being removed, making it easier and quicker to remove them.
docker rm $(docker ps -a -f status=exited -q)
○ → docker rm $(docker ps -a -f status=exited -q)
1cb69341b9ad
(iv). Remove all Docker Containers
To remove all Docker containers from your system, you can use the following command. So, when you execute docker rm $(docker ps -aq)
, it will remove all containers, regardless of whether they are currently running or have already exited.
docker rm $(docker ps -aq)
○ → docker rm $(docker ps -aq)
8a2a2984c9b9
3882e4efac11
cd0f135e9e38
Please note that this action cannot be undone. Make sure you want to proceed with this operation before executing the command.
(v). Remove Docker Containers by Pattern
docker ps -a | grep "pattern"
This command helps to find containers whose names or other details match a specific pattern.
○ → docker ps -a | grep "node"
f7ea490859b7 node "docker-entrypoint.s…" 30 seconds ago Up 30 seconds condescending_bardeen
After identifying the containers you want to remove, you can use the following command to remove them.
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm
Here awk '{print $1}'
part extracts the container IDs from the output, and xargs docker rm
passes the IDs to the docker rm
command to remove the containers.
○ → docker ps -a | grep "node" | awk '{print $1}' | xargs docker rm
f7ea490859b7
(vi). Docker Container Prune
The docker container prune
command is used to remove all stopped containers. Executing this command will permanently remove all stopped containers. Before running it, ensure you no longer need any data from the stopped containers.
docker container prune
○ → docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
e5d57327181cf9f07435c8831ae4d8aace0735ae0739a292b8c6fd77fc607492
aeb630c17970fcadf78d5a40998d01824967ead9bae89cff4c8b90a7f4862e27
1e0cbfa96ec7e5d378b4ade539610d91e4a5ee9366e2b614bafd35f475a58af4
e705a877b83da9cc9553e446db042772231bb189dc198149dd0535fc81b5370e
66a49786540fbbc8d53808d9f44ed6bc678d28e21368a75f4e15a681b37702bd
67acac9abd34adecbbd691fb456d44bbee3558d288b96b7aee30b4333971b60d
37c4386bd1bad435624d16ab2a0af0ccf9ab3fa52ee304463fa0f373b86c4410
a434c900f1e5eb49efdba22bddb3432a287cab04d3f5f4a9c61adaf9be595432
430aec9dc867068024c4d70d2d369e4987e2a9ab77c5fa23050b9a07b0727c35
72780d3943a645820ebd0d22773012288b59052506da665c0b6adf30a3028ae0
2f5b0cb9e382f4d50eb34daf0fa29f566d3f8d2680c17ab005a93555f47d6a0d
Total reclaimed space: 10B
Removing Docker Volumes
(i). Remove One or More Specific Volumes
To list the existing Docker volumes to identify the ones you want to remove, you can use the following command,
docker volume ls
○ → docker volume ls
DRIVER VOLUME NAME
local 0dbd5e14f4424f0b38a208a8c0d2ad2c04c26b4f6f616ce26df833c275d5f3b8
local 6e157b921a0856acf3e73f26f96facfab9eab8d3cb135bacf641be949ca30014
local 52d42a7154fdf6d4e912ef960761f1318e5b814d61016954adc96ade223c23ad
local 86a06bf73fb2cd691c683b87fbb52f2051f22e006d0b23a7524f373966372af3
local 6680b5bdfe64a4cc9b938c7188016944db7ea6206c26da78832ba71eaf01adf4
local a5eb1d542fea4baf1e230d55eb57c6d1af4f26d2f122e1ce88ffa3315b5b4608
local aafb7f5c9e1e00e21e83e0c4d48e9bb02186ea9a3388afb7d607f052af43aa22
local c45a478cda53af0825fc4d9331a281a225e0df5e8cae3b54ac4787590c522b3e
local ff0a25dd0e4820fd748aa71fb8b646b3bb6fd9d5cab25cf5efc9ad69f346c3ca
From the list generated, note down the names or ID's you want to remove. Then use the docker volume rm
command to remove the identified volumes.
docker volume rm volume_name1 volume_name2
○ → docker volume rm 0dbd5e14f4424f0b38a208a8c0d2ad2c04c26b4f6f616ce26df833c275d5f3b8
0dbd5e14f4424f0b38a208a8c0d2ad2c04c26b4f6f616ce26df833c275d5f3b8
(ii). Remove Dangling Volumes
docker volume ls -f dangling=true
docker volume ls
in the above command lists the Docker volumes. The -f
flag with dangling=true
filters out the dangling volumes.
○ → docker volume ls -f dangling=true
DRIVER VOLUME NAME
local 6e157b921a0856acf3e73f26f96facfab9eab8d3cb135bacf641be949ca30014
local 52d42a7154fdf6d4e912ef960761f1318e5b814d61016954adc96ade223c23ad
local 86a06bf73fb2cd691c683b87fbb52f2051f22e006d0b23a7524f373966372af3
local 6680b5bdfe64a4cc9b938c7188016944db7ea6206c26da78832ba71eaf01adf4
local a5eb1d542fea4baf1e230d55eb57c6d1af4f26d2f122e1ce88ffa3315b5b4608
local aafb7f5c9e1e00e21e83e0c4d48e9bb02186ea9a3388afb7d607f052af43aa22
local c45a478cda53af0825fc4d9331a281a225e0df5e8cae3b54ac4787590c522b3e
local ff0a25dd0e4820fd748aa71fb8b646b3bb6fd9d5cab25cf5efc9ad69f346c3ca
Now identify the volumes you want to remove from the list and remove the filtered volumes.
(iii). Docker Volume Prune
The docker volume prune
command is an easy way to clean up the unused volumes in one single go. When you run this command, Docker identifies volumes that are not currently in use by any container and deletes them.
docker volume prune
○ → docker volume prune
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
a5eb1d542fea4baf1e230d55eb57c6d1af4f26d2f122e1ce88ffa3315b5b4608
c45a478cda53af0825fc4d9331a281a225e0df5e8cae3b54ac4787590c522b3e
6680b5bdfe64a4cc9b938c7188016944db7ea6206c26da78832ba71eaf01adf4
52d42a7154fdf6d4e912ef960761f1318e5b814d61016954adc96ade223c23ad
86a06bf73fb2cd691c683b87fbb52f2051f22e006d0b23a7524f373966372af3
aafb7f5c9e1e00e21e83e0c4d48e9bb02186ea9a3388afb7d607f052af43aa22
6e157b921a0856acf3e73f26f96facfab9eab8d3cb135bacf641be949ca30014
ff0a25dd0e4820fd748aa71fb8b646b3bb6fd9d5cab25cf5efc9ad69f346c3ca
Total reclaimed space: 209.1MB
When you run docker volume prune
, it only deletes volumes that are not being used by any running container. If a volume is currently being used by a container, it won't be removed. This helps prevent accidental deletion of volumes that are still needed by your active services.
Removing Docker Network
(i). Remove a Network
Use the command given below to remove a network from Docker
docker network rm name
○ → docker network rm my-bridge-network
my-bridge-network
(ii). Remove Multiple Networks
To remove multiple networks, you can use docker network rm
command followed by the id/ names of the network you wish to remove.
docker network rm id name
○ → docker network rm d82c6deafecf my-bridge-network
d82c6deafecf
my-bridge-network
When you provide multiple networks, the command tries to delete each one sequentially. If it fails to delete one network, it moves on to the next. The command then reports whether each deletion was successful or not.
Importance of Regular Docker Cleanup
Lets explore the key points illustrating the importance of Docker cleanup.
- Regular docker cleanup helps you save valuable disk space by removing unused files.
- Keeping Docker clean speeds up tasks like creating containers and managing images.
- Cleaning up removes old files, including unnecessary Docker logs, that could be exploited by hackers, making your system safer.
- By Docker cleanup, you prevent your system from getting bogged down with unnecessary containers and images.
- With regular Docker cleanup, you spend less time managing Docker and more time developing.
- Cleaning up reduces the chances of running into errors caused by cluttered resources.
- A clean Docker environment makes it easier for teams to collaborate, as everyone works with the same, organized setup.
- By optimizing resource usage, you may avoid unnecessary costs associated with storage and computing resources.
- Efficient resource management in Docker, including proper docker logging practices, contributes to reducing energy consumption.
- A well-maintained Docker environment provides a smoother and more enjoyable experience for developers and users alike.
Docker Cleanup Command Cheatsheet
Here is a Cheatsheet for your quick reference,
Command | Description |
---|---|
docker system df | Display disk usage of Docker |
docker system df -v | Display verbose disk usage details |
docker system prune -f | Remove all unused containers, networks, images (both dangling and unused), and optionally, volumes. |
docker images -a | List all Docker images. |
docker rmi image_id | Delete an image by ID. |
docker rmi image_tag | Delete an image by tag. |
docker rmi image1 image2 image3 | Delete multiple images. |
docker rmi $(docker images -a -q) | Delete all images. |
docker image prune --filter "your_filter" | Prune images with a filter. |
docker image prune --filter "until=24h" | Prune images older than 24 hours. |
docker image prune --filter "label=unused" | Prune images with unused labels. |
docker image prune --filter "dangling=true" | Prune dangling images. |
docker images -a | List all images. |
docker images -a | grep "pattern" | Filter images by pattern. |
docker images -a | grep "pattern" | awk '{print $1":"$2}' | xargs docker rmi | Delete images matching a pattern. |
docker rm ID_or_Name ID_or_Name | Remove containers by ID or name. |
docker run --rm image_name | Automatically remove a container upon exiting. |
docker ps -a -f status=exited | List exited containers. |
docker rm $(docker ps -a -f status=exited -q) | Remove all exited containers. |
docker rm $(docker ps -aq) | Remove all containers. |
docker ps -a | grep "pattern" | Filter containers by pattern. |
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm | Remove containers matching a pattern. |
docker container prune | Remove stopped containers. |
docker volume ls | List volumes. |
docker volume rm volume_name1 volume_name2 | Remove volumes. |
docker volume ls -f dangling=true | List dangling volumes. |
docker volume prune | Remove unused volumes. |
docker network rm name | Remove a network by name. |
docker network rm id name | Remove a network by ID and name. |
Conclusion
Docker Cleanup is essential for managing resources effectively in Docker environments. By systematically removing unused containers, images, volumes, and networks, developers can optimize disk space, improve system performance, and mitigate security risks.
Essential commands and techniques discussed in this blog, helps developers to streamline maintenance tasks and ensure consistent adherence to best practices.
Regular Docker Cleanup help teams to maximise the benefits of containerization technology and maintain a clean, efficient Docker environment.
Atatus Docker Monitoring
Docker Monitoring with Atatus offers extensive visibility and management capabilities for containerized environments, providing detailed insights into workload activities.
Atatus automatically discovers and monitors Docker containers to provide you with realtime insights into traces, metrics, logs and image details within each container.
It automatically identifies Docker containers across hosts and delivers real-time monitoring of performance metrics, dependencies, and interactions for each container.
Key Features of Atatus's Docker Monitoring Solution includes,
1. Resource utilization tracking for optimized performance.
2. Real-time visibility into container health and status.
3. Automated alerting for proactive issue resolution.
4. Seamless scalability management for dynamic workloads.
5. Container-level monitoring for pinpointing bottlenecks.