To delete a Docker image, you can use the docker rmi
command. Below are detailed instructions for different scenarios:
1. Delete a Docker Image by Name or ID
Command:
docker rmi <image-name-or-id>
Example:
If the image name is my-image
or the image ID is abc12345
, you can delete it with:
docker rmi my-image
# OR
docker rmi abc12345
2. Delete Multiple Docker Images
You can delete multiple images at once by specifying their names or IDs.
Command:
docker rmi <image1-name-or-id> <image2-name-or-id>
Example:
docker rmi my-image another-image abc12345
3. Force Delete an Image
If the image is currently in use by a stopped container, you may need to force delete it.
Command:
docker rmi -f <image-name-or-id>
Example:
docker rmi -f my-image
4. Delete All Unused Docker Images
You can delete all dangling images (images not associated with any container) using the following command:
Command:
docker image prune
To confirm deletion without being prompted:
docker image prune -f
Example:
docker image prune
- Explanation: This will delete images tagged as
<none>
or images not associated with a container.
5. Delete All Docker Images
To delete all images from your system, use the following command:
Command:
docker rmi $(docker images -q)
Example:
docker rmi $(docker images -q)
- Explanation:
docker images -q
lists the IDs of all images.- The output of
docker images -q
is passed todocker rmi
to remove all images.
6. Check Available Docker Images
Before deleting an image, you can list all images on your system to verify their names or IDs.
Command:
docker images
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
my-image latest abc12345 2 weeks ago 123MB
another-image v1.0 def67890 1 month ago 256MB
Important Notes:
- Dependencies:
- You cannot delete an image if it is being used by a running container. You must stop and remove the container first.
- Use the following commands to remove the container:
docker stop <container-id> docker rm <container-id>
- Prune Commands:
- To clean up not just unused images but also stopped containers, unused volumes, and networks, use:
docker system prune
- To clean up not just unused images but also stopped containers, unused volumes, and networks, use: