Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete and clean up images by Docker

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to delete and clean up images in Docker. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Delete the image through the tag

You can delete a mirror by either of the following:

Docker rmi [image]

Or:

Docker image rm [image]

The supported subcommands are as follows:

-f,-force: force the deletion of an image, even if a container references the image

-no-prune: do not delete the untagged parent image

For example, we want to delete the allen_mysql:5.7 image created in the previous section with the following command:

Docker rmi allen_mysql:5.7

From the above section, we know that allen_mysql:5.7 and docker.io/mysql:5.7 actually point to the same image, so you may have doubts, if I deleted the allen_mysql:5.7, will I also delete the docker.io/mysql:5.7 image?

In fact, when the same image has multiple tags, executing the docker rmi command will only delete the tags you specified among the many tags in the image, and will not affect the original image file.

If you don't believe me, we can execute the docker images command to see if the docker.io/mysql:5.7 image is still there:

As you can see, the docker.io/mysql:5.7 image still exists!

So, if there are no multiple tags for a mirror, you should be careful when you execute the delete command when there is only one tag, which removes the image completely.

For example, at this point, we execute the docker rmi docker.io/mysql:5.7 command:

As you can see from the image above, we have deleted all the file layers of the docker.io/mysql:5.7 image. The mirror no longer exists locally!

2. Delete image via ID

In addition to deleting the image through the tag name, we can also delete the image by setting the image ID, such as:

Docker rmi ee7cbd482336

Once it is established to delete the image through ID, it will first try to delete all tags that point to the image, and then delete the image itself.

III. Restrictions on deleting mirrors

Deleting a mirror is easy, but it can't be deleted anytime and anywhere. It has some restrictions.

When the container created by the image is not destroyed, the image cannot be deleted. To verify this, let's do an experiment. First, we pull the latest alpine image through the docker pull alpine command, and then start the image to output hello, dockerboards:

Next, let's try to delete the image:

You can see the prompt, and the image cannot be deleted because a container is referencing him! At the same time, this message also tells us that the mirror can be removed only by adding the-f subcommand, that is, by forcing deletion!

Docker rmi-f docker.io/alpine

However, we generally do not recommend such a violent practice, and the correct approach should be:

Delete the container that references the image first

Then delete the mirror image.

That is, execute the delete command by referencing the container ID (9d59e2278553) of the image, as indicated in the figure above:

Docker rm 9d59e2278553

Then execute the command to delete the mirror:

Docker rmi 5cb3aa00f899

At this time, it can be deleted normally!

4. Clean up the image

After using Docker for a period of time, the system usually leaves some temporary unused image files, which can be cleaned by the following command:

Docker image prune

The subcommands it supports are:

-a,-- all: delete all useless images, not just temporary files

-f,-- force: forcibly delete the image file without pop-up prompt for confirmation

In addition, after executing the docker image prune command, tell us how much storage space has been freed!

This is the end of the article on "how to delete and clean images in Docker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report