Docker

How to Delete Docker Images

Docker images are templates from which Docker containers are created. There are a lot of Docker images on Docker Hub which you can use to create Docker containers for your desired application.

Docker keeps a local copy of every Docker image you download from Docker Hub which of course requires additional disk space. If you’re using different types of Docker images, then over time your Docker host may have lots of unnecessary Docker images taking up valuable disk spaces. You can of course delete the Docker images that you don’t use or have any need to free up disk spaces.

In this article, I am going to show you how to delete Docker images stored locally on your Docker host. So, let’s get started.

Requirements:

You must have Docker installed on your computer if you want to try out the examples in this article.

If you don’t have Docker installed, then you may check out one of the articles below (depending on your operating system) to get Docker installed on your desired Linux distribution.

If you still have any problem installing Docker, you may contact me through https://support.linuxhint.com. I will be more than happy to help.

Deleting Docker Images Using Repository and Tag:

You can remove Docker images stored locally on your Docker host using the repository and tag of the image.

When you download a Docker image using the docker pull command, you have to specify the image that you want to download.

The format of the image identifier of the Docker images are repository:tag. For example, alpine:latest, ubuntu:18.04, mysql:latest, mhart/alpine-node:latest etc.

You can list all the available Docker images stored locally on your Docker host with the following command:

$ docker images list

As you can see, all the locally stored Docker images are listed. The first 2 columns contain the repository and tag of the Docker image.

Let’s say, you want to delete the Docker image echoserver:latest. To do that, run the following command:

$ docker image rm  echoserver:latest

The Docker image echoserver:latest should be removed.

Deleting Docker Images Using Image ID:

The image ID of a Docker image is automatically calculated depending on the contents of the image. So, every Docker image has a unique image ID.

You can use the Docker image ID to delete a Docker image from your Docker host.

You can use the following command to list all the locally stored Docker images on you Docker host.

$ docker image list

As you can see, all the locally stored Docker images are listed. The image ID is in the third column.

Let’s say, you want to remove the Docker image elasticsearch:2. If you look closely, you can see that it has the image ID 5e9d896dc62c.

Now, to remove the Docker image 5e9d896dc62c, run the following command:

$ docker image rm  5e9d896dc62c

The Docker image 5e9d896dc62c should be removed.

You may see the following error while trying to remove a Docker image using the image ID. It just means there are many images with the same image ID (alias images), so Docker don’t know which one to remove. The solution to this is simple. Just remove the image using the repository:tag of the image.

So, that’s how you delete Docker images from your Docker host. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.