Docker is one of the most popular products in organizations these days. It makes the process of managing applications in containers very easy. Docker provides portability, performance, agility, scalability, and isolation to the applications since it uses containers. Containers are more portable and require fewer resources than virtual machines. As you work with Docker, you tend to accumulate an excessive number of unused images, volumes, and containers. These resources will clutter the output and consume a lot of disk space. In this tutorial, you will learn how to clean up Docker resources and organize your server.
Purge all
You can clean all the Docker resources including images, stopped containers, volumes, and networks with a single command. You can choose one of the options below:
Reference: Dangling resources are the ones that aren’t related to any running container.
Option 1:
1 |
docker system prune |
– all stopped containers
– all networks not used by at least one container
– all dangling images
– all dangling build cache
Option 2:
1 |
docker system prune -a |
– all stopped containers
– all networks not used by at least one container
– all images without at least one container associated with them
– all build cache
Option 3:
1 |
docker system prune -a --volumes |
– all stopped containers
– all networks not used by at least one container
– all volumes not used by at least one container
– all images without at least one container associated with them
– all build cache
Removing Docker Images
To remove a specific image, you need to know its image ID. You can find the image ID of a Docker container using the “docker images” command, as explained in our tutorial on how to install & operate Docker on Ubuntu.
List images:
1 |
docker images |
Remove image:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
docker rmi <Image ID> Example: docker rmi 1d622ef86b13 Output: Untagged: ubuntu:latest Untagged: ubuntu@sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7 Deleted: sha256:1d622ef86b138c7e96d4f797bf5e4baca3249f030c575b9337638594f2b63f01 Deleted: sha256:279e836b58d9996b5715e82a97b024563f2b175e86a53176846684f0717661c3 Deleted: sha256:39865913f677c50ea236b68d81560d8fefe491661ce6e668fd331b4b680b1d47 Deleted: sha256:cac81188485e011e56459f1d9fc9936625a1b62cacdb4fcd3526e5f32e280387 Deleted: sha256:7789f1a3d4e9258fbe5469a8d657deb6aba168d86967063e9b80ac3e1154333f |
Removing Dangling Images:
When you build a Docker image, it generally has several layers of images. Dangling images are the layers that do not have any relation with any tagged image. Dangling images consume disk space but serve no purpose. They can be listed using the command:
1 |
docker images -f dangling=true |
You can remove these images by running the following command:
1 |
docker image prune |
Removing images based on pattern
You can easily find images based on a particular pattern using the “grep” command and then remove them by passing it in the “docker rmi” command using “awk”. You can use the following commands and replace the “pattern” in each of them:
List:
1 |
docker images -a | grep "pattern" |
Remove:
1 |
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi |
Remove all images
You can list all the docker images by using the command:
1 |
docker images -a |
Once you’ve decided to remove them all, you can use this command to delete them all:
1 |
docker images -a -q | xargs docker rmi |
Removing Containers:
Now that you have cleared all the unnecessary images, it’s time to delete some of the containers which aren’t required.
To list the containers, you can use the command:
1 |
docker ps -a |
To remove the containers, use the command:
1 |
docker rm <<Container Name/Container ID>> |
Running Containers Temporarily
If you want to run the container only once, you can choose to delete the container automatically once it exits. You can do so using the command:
1 |
docker run --rm <<ImageName>> |
Removing exited containers
You can filter the exited containers using the “-f” argument. List the exited containers using the command:
1 |
docker ps -a -f status=exited |
Now that you’ve filtered them, remove them using this command:
1 |
docker rm $(docker ps -a -f status=exited -q) |
Removing containers based on pattern
You can easily find containers based on a particular pattern using the “grep” command and then remove them by passing it in “docker rm” command using “awk”. You can use the following commands and replace the “pattern” in each of them.
List:
1 |
docker ps -a | grep "pattern" |
Remove:
1 |
docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rm |
Stop and Remove All Containers
Before doing so, review all the containers on your server by listing them. Only once you’re sure that you want to delete them, run the following commands:
List the containers to review:
1 |
docker ps -a |
Stop and Remove:
1 2 |
docker stop $(docker ps -a -q) docker rm $(docker ps -a -q) |
Removing Volumes
Remove a specific volume
To remove a specific volume, you need to know the volume name. To find that out, you can list the volumes.
List:
1 |
docker volume ls |
Remove:
1 |
docker volume rm <<VolumeName>> |
Remove Dangling Volumes
When you remove a container, the volume attached to it doesn’t get removed automatically. Such a volume is called dangling volume. To locate such volumes, use the filter argument in the command:
1 |
docker volume ls -f dangling=true |
To remove all such dangling volumes, use the command:
1 |
docker volume prune |
Remove a container and its unnamed volume
If you created an anonymous or unnamed volume while running the container, you can remove it along with the container using a single command. However, if the volume is named then only the container would get deleted.
1 |
docker rm -v <<ContainerName>> |
Now that you have got this cheat sheet, you should be ready to clean up Docker resources that aren’t required on your server.
Happy Computing!
- Removing Spaces in Python - March 24, 2023
- Is Kubernetes Right for Me? Choosing the Best Deployment Platform for your Business - March 10, 2023
- Cloud Provider of tomorrow - March 6, 2023
- SOLID: The First 5 Principles of Object-Oriented Design? - March 3, 2023
- Setting Up CSS and HTML for Your Website: A Tutorial - October 28, 2022