Docker allows you to create, manage, and remove containers and images easily. This guide will explain how to delete Docker images on a Linux system.
Step 1: List Docker Images
- Before deleting a Docker image, it's useful to list all available images. Run the following command to display all images on your system:
docker images
This command will display a table with the image repository, tags, image ID, creation date, and size.
Step 2: Remove a Specific Docker Image
- To delete a Docker image, you need to know its IMAGE ID or REPOSITORY:TAG. Run the following command to remove a specific image by its ID:
docker rmi
You can also delete an image using its repository name and tag:
docker rmi :
Example: To delete an image named myapp
with the tag latest
, run:
docker rmi myapp:latest
Step 3: Force Delete a Docker Image
- If the image is being used by any running or stopped containers, Docker will prevent you from deleting it. To force delete an image, use the
--force
or-f
flag:
docker rmi -f
Be careful when using the force option, as it will remove the image even if it's in use by containers.
Step 4: Delete All Unused Docker Images
- To delete all images that are no longer being used by containers (also known as "dangling" images), use the following command:
docker image prune
If you want to remove all unused images, even those that are not dangling, use the -a
flag:
docker image prune -a
Step 5: Verify Image Removal
- After removing images, you can verify that they have been deleted by listing the images again:
docker images
This will show the remaining images on your system, and the deleted images will no longer appear.
Conclusion
By following this guide, you now know how to delete Docker images on a Linux system, including removing specific images, force deleting, and cleaning up unused images.