Clean Up Docker Resources featured image

Clean Up Docker Resources – Images, Containers, and Volumes

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:
This will remove:
– all stopped containers
– all networks not used by at least one container
– all dangling images
– all dangling build cache

Option 2:
This will remove:
– 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:
This will remove:
– 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

docker prune options

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:
Remove image:
docker rmi

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:

You can remove these images by running the following command:

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:
Remove:
Remove all images

You can list all the docker images by using the command:

Once you’ve decided to remove them all, you can use this command to delete them all:

 

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:

To remove the containers, use the command:

docker rm container
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:

Removing exited containers

You can filter the exited containers using the “-f” argument.  List the exited containers using the command:

Now that you’ve filtered them, remove them using this command:

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:
Remove:
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:

Stop and Remove:

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:
Remove:
docker volume rm
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:

To remove all such dangling volumes, use the command:

 

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.

 

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!

About Akshay Nagpal

Big Data Analytics and ML enthusiast.