How to Docker: Remove Docker, Containers, Images, and Volumes

Docker has gained popularity enough to become a tool and an asset to create and store applications nearly flawlessly. With Docker, you don’t need to worry about compatibilities and environments. In this article, we’ll show you some basics of Docker: remove a container, images, and more!

How to Remove Images in Docker

Remember, before using Docker, you need to have it installed on your VPS. You can access your server with PuTTY! If you’re having trouble, check out our tutorial!

Docker: Remove Images

Apply the –a option to view a single or multiple images with an Image ID of every single image within the system.

From this rundown, pick the IDs of the image that you need to dispose of, afterwards using the rmi command, as displayed in the example below.

You can select multiple images by choosing their Image IDs. The following commands help remove one, specific or different files:

Command to view the list:

$ docker images –a

Command to remove an image:

$ docker image rmi <image_id>

Command to remove multiple images:

$ docker images rmi <image_id> <image_id>

Docker: Remove Dangling Images

Dangling images are the ones that have no relation to tagged images. Normally these images are unused and only use up disk space. We can find them by using the -f filter option with the string dangling=true. They can be purged afterwards.

To view dangling images use the below command:

$ docker images –f dangling=true

To delete or remove dangling images use:

$ docker images purge

Docker: Remove Images by Pattern

Removing certain images as indicated by our decision is easy and fast in Docker. We have to run specific commands inside the terminal to see the list of many pictures along with the matched pattern.

Then we will use the awk command with the Image ID to erase the specific picture.

If we want to remove images according to a specific pattern, we can do it in just a few commands! First, we’ll need to use Docker along with a grep command to list the images, then delete them with the awk command.

To specify your pattern in the terminal use:

$ docker images –a | grep "pattern"

To remove the image use:

$ docker images –a | grep "<ImageId>" | awk '{<Print $1>}' | xargs docker rm

Docker: Remove Container

To remove a container in Docker you have to follow steps similar to when removing an image. First, use the -a option to list the containers, then use the rm command to delete a single, or multiple containers:

To view containers use:

$ docker container -a

To remove a single container use the below command:

$ docker container rm <container ID>

For deleting multiple containers, type:

$ docker container rm <container ID> <container ID>

Docker: Remove Container When Exiting an Application

If you don’t require a container after creating it, then simply use the –rm command to delete it.

$ docker run –-rm <container_name>

Docker: Remove Container by Pattern

To view all containers that matches your pattern use the below command. The process is nearly identical to what you would use to remove images by pattern:

$ docker ps -a |  grep "pattern”

Next, just use the rep command along with awk to remove the container:

$ docker ps -a | grep "pattern" | awk '{print $1}' | xargs docker rmi

Docker: Remove Volumes

Use the ls command to look at the list volumes, and check the volume names of the ones you want to remove. Afterwards, use the rm command as show in the example:

List volumes:

$ docker volume ls

Remove single or multiple volumes:

$ docker volume rm <volume_name> <volume_name>

Docker: Remove Dangling Volumes

Volumes don’t exist without containers. If you remove containers, and leave volumes empty, they won’t be automatically removed. To remove these volumes we can follow a process similar to removing dangling images:

First, we can list all the dangling volumes with the -f filter option, then use the prune command to remove the dangling ones.

List dangling volumes:

$ docker volume ls –f dangling=true

Remove dangling volumes:

$ docker volume prune

Docker: Remove Container and Its Volume

Nameless volumes can be deleted with their containers by using the  –v option:

$ docker rm -v <container_name>

Wrap Up

Docker allows you to work in a chaos free environment. But after using Docker for a longer period of time, you might have leftover images, volumes and containers that are only taking up valuable disk space. In this tutorial, we showed you some basics of Docker: remove containers, images, volumes according to different specifications.

We hope this guide helps you keep a tidy development workflow! See you in the next one!

The author

Edward S.

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.