How does one remove a Docker image?

DockerOperating SystemOpen Source

If you have been using Docker for quite a long time now, you might have several unused images already existing in your local machine. These images might be previously downloaded older versions, or simply an image that you downloaded for testing. These images take up a lot of space unnecessarily and reduce the overall performance and experience. Moreover, they are several unused, dangling images as well.

It’s always better to remove these older images which will help you to keep track of all your useful images in a better way. Docker allows you to remove images quite easily and through many different commands. You can use the Docker rmi command, Docker images rm command, or even Docker image prune commands to do so. Additionally, Docker allows you to use several options along with these commands to remove images strategically.

In this article, we will discuss all the popular and most frequently used commands that will allow you to delete images quite easily.

Delete Docker Images

As already discussed, there are several commands that can be used interchangeably to remove one or more Docker images from your system. Let’s start from the very basic one.

$ docker image rm [OPTIONS] IMAGE [IMAGE...]

You can also use several options along with this command. Below are the most useful ones.

  • --force - You can use this option to forcefully remove images.
  • --no-prune - If you don’t want to delete untagged images, you can use this option.

Please note that you can only remove those images that don’t have a container associated with it. If you try to remove such an image, it will throw an error. To override this default behaviour, you can use the --force option to delete images forcefully.

For example, if you want to delete a fedora image with tag 24, you can do so using the following command.

$ docker image rm fedora:24

To check whether an image has a container associated with it, you can list all the containers using any one of the following two commands.

$ docker ps -a


$ docker container ls -a

If you find any container associated with that image, you can either stop and remove the container or remove it forcefully.

$ docker stop <container-name>


$ docker rm <container-name>

Or

$ docker rm -f <container-name>

After removing the container, you can now proceed with deleting the image.

Delete Images using a shorter command

You can also use the other shorter command mentioned below to delete an image.

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

If you don’t want to check for containers before removing an image, you can use the force option to remove it.

$ docker rmi -f <image-name>

Also, if you want to remove more than one image together, you can directly mention the image IDs or image names of all these images separated by spaces.

$ docker rmi -f myimage1 myimage2 myimage2

Delete Images by Pruning them

You can also use the Docker image prune command to delete all the dangling images.

$ docker image prune [OPTIONS]

You can use several options such as -

  • --all - To delete all the unused and dangling images as well.
  • --filter - To provide filters to remove only certain specific images.
  • --force - To prune images forcefully.

For example, if you want to remove all the unused images from your system, you can use the following command.

$ docker image prune --all

If you want to delete all the Docker images together, this is a great way to do it. There is also another way to delete all the images simultaneously. You can use a sub-command along with the Docker rmi command. Consider the command below.

$ docker rmi -f $(docker images -aq)

Here, we have used the force option along with the Docker rmi command as a parent command. Instead of mentioning the image IDs or names, we have used a subcommand that lists the image IDs of all the images using the all and quiet options.

To sum up, in this article, we have discussed why it is a good practice to remove unused or dangling images periodically. We discussed how to delete images using three different commands along with several options. We also discussed how to remove more than one or all the images simultaneously. We hope that you will now be able to remove Docker images quite easily.

raja
Updated on 06-Aug-2021 11:39:49

Advertisements