# 2021-01-18 #「Docker」- Delete image

The content of this note includes: how to delete a local Docker image; how to delete a remote Docker image; how to deal with common problems in the deletion process; it also involves some Docker image storage methods.

Delete remote mirror

Mirrors in the mirror warehouse (such as Docker Hub) cannot be deleted by commands at present, and must be deleted in the mirror warehouse. Complete the delete operation by clicking the button, which does not involve particularly complicated content, so simply skip it.

# 08/08/2019 Today I saw the docker registry rmi command in Docker v19.03 (current) , which can remove the image from the Registry. It is currently experimental.

Delete local mirror

Delete single or multiple mirrors

Use the docker rmi and docker image rm commands to delete the local mirror (you can only delete the local mirror, not the mirror in the mirror warehouse):

# Delete 
docker rmi fd484f19954f by Image ID 

# The following two are equivalent, because latest is the default label 
docker rmi test1:latest 
docker rmi test1 

# You can also delete it through DIGEST. Between REPOSITORY and DIGEST, use @ to separate. 
# Because the image pulled by DIGEST does not have a TAG, this method is used. 
docker rmi test/busybox@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf

1) If the mirror has multiple tags, and the tag is used as the delete parameter, the command will only delete the tag;
2) If the mirror has a unique tag, the command will delete the tag and then delete the mirror;

Delete the image used by the container

If the image has a container in use, it cannot be deleted. You need to use the -f option to delete the container and image:

docker image rm --force "REPOSITORY:TAG"

Delete all mirrors

If you want to delete all images, you can use the following command (you can use the --filter option of docker images for advanced filtering):

# The following two commands are equivalent 
docker image rm $(docker image ls -a -q) 
docker rmi $(docker images -a -q) 

# Delete the container image 
docker image rm -f $(docker image ls -a -q)

Delete all mirrors with the same IMAGE ID

When the image has multiple tags, if you use the IMAGE ID to delete ( docker rmi fd484f19954f ), the following error will be generated:

Error: Conflict, cannot delete image fd484f19954f because it is tagged in multiple repositories, use -f to force 2013/12/11 05:47:16 Error: failed to remove one or more images

Method 1: When the -f option can be used at this time, this will delete all tags and delete the mirror:

docker rmi --force fd484f19954f

Method 2: Use the command grep to filter, and use the result as the docker rmi parameter to delete.

Method 3: Use the docker inspect command, the RepoTags field of the command output will display the relevant tags for deletion.

Common error handling

# image has dependent child images

Find the dependent child images on Docker

Problem description:
When deleting a mirror ( docker rmi a990d6e2b083 ), the following error is prompted:

Error response from daemon: conflict: unable to delete d86795370e7b (cannot be forced) - image has dependent child images

Cause of the problem:
This error indicates that the deleted mirror has dependent submirrors.

Solution:

############################################## ############################## 
# A simple approach 
############## ############################################## ############### 
docker image inspect --format='{ 
    
    { 
    .Id}} {
     {.Parent}}' \ 
    $(docker image ls --all --filter since= a990d6e2b083 -q) | grep a990d6e2b083 

# The above command: 
#1 (docker image ls) Find the mirror 
#2 after a990d6e2b083 (docker image inspect) and then get the parent mirror of these mirrors 
#3 (grep) By filtering the parent mirror, what is found is Reference the submirror of this mirror. 

############################################## ############################## 
# However, the problem has never been so simple 
########## ############################################## ####################  
# The biggest problem is: there are still sub-mirrors, look for them slowly? ? ?
# Actually what you have to do is very simple. Usually this problem is due to a dangling mirror that references the mirror, so delete all "dangling" mirrors.
# " 
Dangling mirror" can safely delete docker rmi $(docker images --filter "dangling=true" -q --no-trunc) 

################# ############################################## ########### 
# However, the real correct approach is this 
# Find the sub-mirror of the sub-mirror of the sub-mirror that you want to delete... (no more deletions, no less deletions) 
# https://stackoverflow.com/questions/38118791/can-t-delete-docker-image-with-dependent-child-images/38119847#38119847#answer-53037893 
############# ############################################## ################# 
recursive_remove_image() { 
  for image in $(docker images --quiet --filter "since=${1}") # Find the mirror after this mirror . Because the submirror is after this. 
  do 
  done 
  # execute delete 
    if [$(docker history --quiet ${image} | grep ${1})] # Check the construction history of this sub-mirror and whether it contains this mirror. If it is, it should be deleted.
    then 
      recursive_remove_image "${image}" # Recursively, delete the sub-mirror to which this mirror is applied. 
    fi 
  echo "Removing: ${1}" 
  docker rmi -f ${1} 
} 
recursive_remove_image "<image-id>"

references

 

 

Guess you like

Origin blog.csdn.net/u013670453/article/details/112773561