Docker rmi command
docker rmiThe command is used to delete one or more Docker images.
docker rmiThe command is very useful for managing images stored locally, helping users clean up images that are no longer needed and free up storage space.
Syntax
docker rmi [OPTIONS] IMAGE [IMAGE...]
Parameter description:
IMAGE: The name or ID of the image to delete. It can be an image name, image ID, or image digest.OPTIONS: Optional parameters used to control the behavior of the command.
Common options:
-a,--all-tags: When specifying a repository name, delete all images under that repository.-f,--force: Force delete the image, even if it is being used by a container.--helpPrint help information and exit.--no-prune: Do not delete dangling parent images.-q,--quiet: Quiet mode, do not display detailed information about deleted images.
Delete a single image
docker rmi ubuntu:latest
Delete the image with the latest tag named ubuntu.
Delete multiple images
docker rmi ubuntu:latest nginx:latest
Delete the images tagged latest for ubuntu and nginx.
Delete multiple tags of an image
docker rmi ubuntu:latest ubuntu:18.04
Delete the latest and 18.04 tags of the ubuntu image.
Delete an image along with all its tags
docker rmi -a ubuntu
Delete all images under the ubuntu repository.
Force delete image
docker rmi -f ubuntu:latest
Force delete the image with the latest tag of ubuntu, even if it is in use.
Delete dangling images
docker rmi -d
Delete all dangling images that have no tags.
Quiet mode: delete all images
docker rmi -q $(docker images -q)
Delete all images using quiet mode, without displaying deleted image information.
Delete all images in the specified repository
docker rmi -a myrepo
Delete all images under the myrepo repository.
Delete the image while preserving its child images
docker rmi --no-prune ubuntu:latest
Delete the ubuntu:latest image, but keep its child images.
Example
View existing images:
docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE my_image latest d1e1b5a3a8a9 3 days ago 128MB my_image1 latest c3a4f5a3a8b8 4 days ago 256MB my_image2 latest a9e1d3a7c9b9 5 days ago 512MB
Delete an image:
docker rmi my_image
Output:
Untagged: my_image:latest Deleted: sha256:d1e1b5a3a8a9...
Delete multiple images:
docker rmi my_image1 my_image2
Output:
Untagged: my_image1:latest Deleted: sha256:c3a4f5a3a8b8... Untagged: my_image2:latest Deleted: sha256:a9e1d3a7c9b9...
Common Scenarios
- Clean up unused images: Delete images that are no longer needed to free up storage space.
- Image management: Manage and maintain the local image repository, ensuring that only needed images are kept.
- Force delete: Force delete images when they are in use by containers (use with caution).
Notes
- Force-delete an image (using the
-foption) may cause containers that depend on the image to fail to run, so use with caution. - By default,
docker rmiwill also delete unused parent images; if you do not want this, you can use--no-pruneOptions.
docker rmiThe command is a very important tool in Docker image management. With this command, users can delete images that are no longer needed, free up storage space, and keep the local image repository clean. By using appropriate options, the image deletion process can be managed flexibly.
other extensions