Docker

How to remove a Docker Image

Hello. In this tutorial, we will talk about Docker and how to remove images from the docker.

1. What is Docker?

In the present world, Docker is an important term –

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 What is Docker used for?

It is used for –

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.2 Basic Docker terminology?

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates, and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.3 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

2. How to remove a Docker Image?

A docker image is a read-only template containing the list of instructions for creating a container that runs on the containerized environment such as Docker. It provides a convenient way to bundle the application and server environments that can be used privately or share it publically with other Docker users.

Now let us open the terminal to play around with the different docker commands that we will cover in this tutorial and I hope you’re aware of the basics of the docker such as docker pull etc.

2.1 List docker images

To remove the image we first need to list all the images available in the host environment and we can view the list by running the docker images command or the docker images -a command. The images were downloaded with the help of the docker pull IMAGE_NAME command. For example – docker pull mysql etc.

Fig. 1: Listing images

2.2 Remove a single docker image

To remove an existing docker image we can do it by executing the simple command – docker rmi IMAGE_ID_OR_NAME. To verify whether the image was removed successfully or not we will use the docker images command.

Removing a single image

docker rmi ubuntu
Fig. 2: Removing a single image

2.3 Remove multiple docker images

To remove multiple docker images we can do it by executing the same command as shown in section 2.2 but we need to pass the multiple ids or names i.e. docker rmi IMAGE_ID_OR_NAME_1 IMAGE_ID_OR_NAME_2 IMAGE_ID_OR_NAME_3 …. To verify whether the image was removed successfully or not we will use the docker images command.

Removing multiple images

docker rmi nginx mysql
Fig. 3: Removing multiple images

2.4 Remove dangling docker images

Dangling images in docker mean that we have created the new build of an image but it wasn’t given a new name. So the old images become the dangling images. These old images are the ones that are untagged and displays the <none> on its name when we run the docker images command. To display the list of dangling images we add the filter flag with the value of dangling=true to the docker images command.

List dangling images

docker images -f dangling=true

To remove the dangling images we need to execute the below command in the terminal.

Removing dangling images

docker image prune --filter="dangling=true"

2.5 Remove all docker images

Removing all images from docker is very. We simply need to run the prune and voila all images are removed from the environment.

Removing all images

docker image prune -a
Fig. 4: Remove all images

Once all the images are removed we can use the docker images command to verify. That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share! Happy learning and do not forget to share!

3. Summary

In this tutorial, we learned about Docker and different variations in the docker image command. You can also download the commands used in this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on learning Docker and playing around with the docker image command.

Download
You can download the full source code of this example here: How to remove a Docker Image

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button