How to Remove Docker Images, Containers, and Volumes?

Photo of author

By admin

Docker is one of the leading technologies for benefitting the DevOps process. It does so in several ways. With Docker, you can run the applications seamlessly, irrespective of the system, underlying architecture, and business size.

Docker provides an efficient way to create, deploy, and test applications in the working environment. One of the significant critical factors of using Docker is its cross-platform ability. You can run Docker containers on any platform without worrying about the underlying operating system.

What is Docker?

You might understand Docker as a virtual machine, but it works entirely differently from a VM, as it is much more efficient in resource utilization.

Docker works on the concept of containerization. A container wraps up the application and its dependencies in a bundle, which can be deployed in production as an isolated application.

The Cluttering Problem in Docker

Docker has a repository where you can store different versions of containers and images for rollback purposes. Over time, it might fill up with several unused images, containers, networks, and volumes consuming disk space while serving no real purpose.

With Docker, you can use many tools to clear up space directly from the command line. Nonetheless, you must manually run the commands as Docker does not automatically take care of unused resources.

So, we will discuss some CLI commands that will help you reclaim disk space by clearing unused containers, images, volumes, and networks. This article will cover the following topics:

  1. How to remove all unused Docker resources at once?
  2. Steps to remove Docker images.
  3. Steps to remove Docker Containers.
  4. Steps to remove Docker Volumes.
  5. Steps to remove Docker Networks.

Before diving into the topics above, let’s first discuss what you need to declutter your Docker repository successfully.

Prerequisites

Prerequisites for running Docker and its commands are:

  • You should have a Linux system to install and run Docker.
  • You should have access to the terminal or the command-line interface (Ctrl+Alt+T on Ubuntu and Alt+F2 on CentOS).
  • You should have a sudo privilege to run Docker commands.

Decluttering the Docker Repository

1. How to remove all unused Docker resources at once?

First, you should have your system’s latest version of Docker. You also require SSH to access your VPS. Docker does not facilitate any service or ways to remove unused resources, leading to cluttered disk space or an unorganized server.

Many commands will help you eliminate these resources and make your server free to work efficiently. You can remove all the resources using the following command if they are not associated with any container:

Docker system prune

Once you run the above command, all the resources, dangling images, and stopped containers will be removed. You will get the following message to confirm the removal of resources.

docker system prune

It will remove the dangling images only. If you want to delete all the unused images, you can execute the following command from the terminal.

docker system prune -a

Again, you will be asked for confirmation with a warning message like this:

warning message

The above two commands will not include the volumes until we specify the volumes. For safety concerns, you have to mention volumes explicitly. You can run the below command to remove the volumes:

Docker system prune –volumes

Note: You cannot reverse the prune and rm commands, and they will obliterate the resources without any backup to restore.

2. Steps to remove Docker images

Using the -a flag and the docker images command will list all the images with their ids and intermediate image layers. You can select the id from there to remove any particular image. Run the below command to list all the Docker images:

docker images -a

Once you get the id for the particular Docker image that you want to remove, you can pass that id (IMAGE_ID) to the below command to execute deletion:

docker image rm IMAGE_ID

2.1 Removing dangling Docker images

Docker images come with multiple layers, whereas dangling images are the layers that do not relate to the tagged images or any container. Dangling images are not in use and consume disk space.

Dangling images are images that are created without being tagged. You can locate the dangling image with the -f flag and dangling=true. This command will list all the dangling images within the Docker:

Docker images -f dangling=true

Once you get the output of all dangling images in the Docker, you can execute the below command to remove all the dangling images.

docker image prune

If you want to delete the image that is currently tagged to a container, you can execute the following command:

Docker image prune –a

You can even select the images that fall under a particular time frame and delete them. You can consider the below example:

docker image prune -a –filter “until=24h”

2.2 Removing images based on a pattern

You can use the grep parameter with the docker images command. Once you get the desired list of the images, you can use the awk parameter to pass to the remove command. Run the below set of commands to delete images based on a pattern:

docker images -a | grep “pattern_p”

docker images -a | grep “pattern_p” | awk ‘{print $2}’ | xargs docker rmi

2.3 Removing all Docker images

You can use the -a flag to list all the available Docker images. If you want to delete all the images listed, add the -q flag for passing the image id to the remove command, like this:

docker images -a

docker rmi $(docker images -a -q)

3. Steps to remove Docker Containers

3.1 Removing a specific container based on container id

You can run the below command to list all the containers present within the Docker with their id:

docker ps -a

The above command will get the id of the container you want to delete, and you can pass the container id to this command:

docker rm Cont_ID_or_Name ID_or_Name

3.2 Removing a container automatically upon exit

You can delete a container whenever it exits or has completed its working using this command:

docker run –rm image_name

3.3 Removing all the exited containers at once

You can use the docker command to list all the containers with -a flag sorted with their status as exited, created, recreating, running, or paused. Once you know which containers to delete, you can pass those container ids using the -q flag.

You can run the below set of commands to delete a particular set of containers:

docker ps -a -f status=exited

docker rm $(docker ps -a -f status=exited -q)

You can specify more than one filter to sort out the containers based on their statuses. The below set of commands will allow you to delete the container either as exited or running status:

docker ps -a -f status=exited -f status=running

docker rm $(docker ps -a -f status=exited -f status=running -q)

3.4 Removing container based on a specific pattern

You can use the grep command and the docker command to list all the containers matching a specific pattern. Once you get the list, you can select the containers to delete and pass those ids to the remove command using the awk parameter.

You can run the below set of commands to delete the container:

docker ps -a | grep “pattern_p”

docker ps -a | grep “pattern_p” | awk ‘{print $3}’ | xargs docker rm

3.5 Stopping and removing all containers

Use the docker command to list all the containers using the -a flag. Once you decide which container to delete, you can pass those container ids to the remove command with the -q flag. You can run the following set of commands for stopping and deleting the containers:

docker ps -a

docker stop $(docker ps -a -q)

docker rm $(docker ps -a -q)

4. Steps to remove Docker Volumes

You can use volumes for multiple containers. There may be chances that Docker has several unused or stopped volumes taking up the disk space. Docker does not facilitate automatic removal of the docker volumes as it may lead to data loss.

Run the docker command to get a list of all the available volumes. Once you get the list, you can delete one or more volumes by specifying their names with the rm docker command.

docker volume ls

docker volume rm volume_name volume_name

4.1 Removing the dangling Docker volumes

Well, a volume cannot be automatically deleted when you delete a container. But when you have a volume lest that is not connected to any container, it is called dangling volume.

You can list all the volumes along with -f filter to find out the dangling volumes and delete them accordingly using the below set of commands:

docker volume ls -f dangling=true

docker volume prune

4.2 Removing a container with the associated volume

You can delete an unnamed volume with the unnamed container by using the -v flag. This, however, is the case only with unnamed volumes. The container id is displayed whenever you delete a container, but there are no volume-related details.

If the related volume is unnamed, it will get deleted automatically with the container; otherwise, it will stay on the system. Use the following command to remove a container with the associated volume:

Docker rm -v container_name

Conclusion

Docker provides you a better development solution. There is no doubt that it has changed the working of every business by providing an efficient way to create and deploy applications.

Docker works on containers, images, volumes, and networks. These tend to consume some of the disk space even when not required within the projects. To maintain the server and disk space, they must be removed regularly to free the resources. This practice will also boost the system’s performance.

This article teaches you how to run different Docker commands to free up resources. You can deal with it with good command-line knowledge if you have proper access and a system.

Frequently Asked Questions

1. How do I remove a docker container?

Ans: The following command can be used to remove Docker container: docker stop <Container_ID> docker rm <Container_ID>

2. What command will remove a docker image?

Ans: docker rmi <your-image-id> command will remove a docker image

3. Where are docker images stored in Linux?

Ans: Under /var/lib/docker, docker stores images, containers, volumes, etc.

4. How do I edit docker images?

Ans: Choose to Build Containers from the Build page in the Advanced Designer. …

  • Select the configuration you want to configure under Docker Configuration, and Choose Container Configuration.

5. Where are docker images stored in Windows?

Ans: A default installation stores layers in C:/ProgramData/docker, divided between the “image” and “windowsfilter” directories.

People are also reading:

Leave a Comment