Docker practical notes 2 - delete the local image

Please indicate the source of the reprint: http://blog.csdn.net/zhaoyanjun6/article/details/130239544
This article comes from [Zhao Yanjun's blog]

Delete local mirror - high-end operation

If you want to delete the local image, you can use docker image rmthe command , its format is:

docker image rm [选项] <镜像1> [<镜像2> ...]

Delete image with ID, image name, digest

Among them, <image> can be the image short ID, image long ID, image name or image summary.

For example, we have some images like this:

$ docker image ls
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
centos                      latest              0584b3d2cf6d        3 weeks ago         196.5 MB
redis                       alpine              501ad78535f0        3 weeks ago         21.03 MB
docker                      latest              cf693ec9b5c7        3 weeks ago         105.1 MB
nginx                       latest              e43d811ce2f4        5 weeks ago         181.5 MB

We can use the image's full ID, also known as 长 ID, to delete an image. Long IDs may be used when using scripts, but manual input is too tiring, so it is more often used 短 IDto delete images. docker image ls The one listed by default is already 短 ID, generally take the first 3 characters or more, as long as it is enough to distinguish it from other images.
For example, if we want to delete redis:alpine the image , we can execute:

$ docker image rm 501
Untagged: redis:alpine
Untagged: redis@sha256:f1ed3708f538b537eb9c2a7dd50dc90a706f7debd7e1196c9264edeea521a86d
Deleted: sha256:501ad78535f015d88872e13fa87a828425117e3d28075d0c117932b05bf189b7
Deleted: sha256:96167737e29ca8e9d74982ef2a0dda76ed7b430da55e321c071f0dbff8c2899b
Deleted: sha256:32770d1dcf835f192cafd6b9263b7b597a1778a403a109e2cc2ee866f74adf23
Deleted: sha256:127227698ad74a5846ff5153475e03439d96d4b1c7f2a449c7a826ef74a2d2fa
Deleted: sha256:1333ecc582459bac54e1437335c0816bc17634e131ea0cc48daa27d32c75eab3
Deleted: sha256:4fc455b921edf9c4aea207c51ab39b10b06540c8b4825ba57b3feed1668fa7c7

We can also use the image name, ie <仓库名>:<标签>, to delete an image.

$ docker image rm centos
Untagged: centos:latest
Untagged: centos@sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366c
Deleted: sha256:0584b3d2cf6d235ee310cf14b54667d889887b838d3f3d3033acd70fc3c48b8a
Deleted: sha256:97ca462ad9eeae25941546209454496e1d66749d53dfa2ee32bf1faabd239d38

Of course, it is more precise to use 镜像摘要delete image.

$ docker image ls --digests
REPOSITORY                  TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
node                        slim                sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228   6e0c4c8e3913        3 weeks ago         214 MB

$ docker image rm node@sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228
Untagged: node@sha256:b4f0e0bdeb578043c1ea6862f0d40cc4afe32a4a582f3be235a3b164422be228

Untagged 和 Deleted

If you observe the output information of the above commands, you will notice that the deletion behavior is divided into two categories, one is and the Untaggedother is Deleted. As we mentioned before, the unique identifier of an image is its ID and abstract, and an image can have multiple tags.

So when we use the above command to delete an image, we are actually asking to delete an image with a certain label. So the first thing to do is to cancel all the image tags that meet our requirements, and this is the Untaggedinformation . Because an image can correspond to multiple tags, when we delete the specified tag, there may be other tags pointing to this image. If this is the case, Deletethe behavior Therefore, not all docker image rmwill cause the behavior of deleting the image, it may just cancel a certain tag.

When all the tags of the image are cancelled, the image will probably lose its meaning of existence, so the deletion will be triggered. Mirroring is a multi-layer storage structure, so when deleting, it is judged and deleted sequentially from the upper layer to the base layer. The multi-layer structure of images makes image reuse very easy, so it is very likely that some other image is depending on a layer of the current image.

In this case, the behavior of deleting this layer will still not be triggered. The current layer will not be actually deleted until no layer depends on the current layer. This is why, sometimes it is strange, why there is no other tag pointing to this image, but it still exists, and why sometimes you find that the number of layers you have deleted is different from the number of layers you docker pullsee

In addition to image dependencies, you also need to pay attention to the container's dependency on images. If a container started with this image exists (even if the container is not running), then it is also not possible to delete this image. As mentioned before, the container is based on the image, plus a layer of container storage layer to form such a multi-layer storage structure to run. Therefore, if the image is depended on by the container, deleting it will inevitably cause a failure. If these containers are not needed, they should be deleted first, and then delete the image.

Guess you like

Origin blog.csdn.net/zhaoyanjun6/article/details/130239544