In Docker practice, removing no longer needed images to free up disk space is a common administrative task. Here are a few different ways to delete Docker images:
1. Delete a single image
# Delete the image of the specified name and labeldocker rmi [image-name]:[tag] # If no tag is specified, the image of the latest tag in the repository is deleted by default.docker rmi [image-name] # Use image ID to delete a specific imagedocker rmi [image-id]
2. Forced delete the image being used (careful action)
If the image is referenced by a running container or has other dependencies, direct deletion will fail. At this time, you can first stop and delete all containers that use the image, and then force the image to be deleted:
# Find and stop/delete all containers associated with the imagedocker ps -a --filter "ancestor=image-name:tag" -q | xargs docker stop && docker rm # Then force delete the imagedocker rmi -f [image-id-or-image-name:tag]
3. Delete all images that are not referenced by any container
All unused images can be cleaned with the following command:
# Clean up all unused imagesdocker image prune # Or add `-a` parameter to delete images that are not referenced by the container but are markeddocker image prune -a # Add `-f` parameter for forced deletion without user confirmationdocker image prune -a -f
4. Delete all images
Extremely cautious: This will delete all local images, including the ones being used.
# List all image IDsdocker images -q # Delete all imagesdocker rmi $(docker images -q) # Do it with caution, please make sure it is correct, otherwise it may affect the running container
Notes:
- Before performing the deletion operation, make sure that important data has been backed up and confirm that no container is using the image you are about to be deleted.
- If the images are depended on by running containers, the containers must be stopped or deleted before the images can be deleted successfully.
Summarize
This is the end of this article about several different methods of Docker removing images. For more related content on Docker removing images, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!