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.
- How to Install and Use Docker on Ubuntu 18.04 LTS (https://linuxhint.com/install_docker_ubuntu_1804/)
- Install Docker on Debian 9 (https://linuxhint.com/install_docker_debian_9/)
- Install Docker on CentOS 7 (https://linuxhint.com/install-docker-centos7/)
- Install Docker on Raspberry Pi (https://linuxhint.com/install_docker_raspberry_pi/)
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:
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:
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.
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:
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.