Friday, January 10, 2025
HomeProgrammingHow to delete a docker image?

How to delete a docker image?

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.

See also  Go Programming Language (Introduction)

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:

See also  What is the Difference between Public, Protected, Package Private, and Private in Java

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 to docker 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:

  1. 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>
      
  2. Prune Commands:
    • To clean up not just unused images but also stopped containers, unused volumes, and networks, use:
      docker system prune
      

 

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x