Docker
Published in Docker
avatar
4 minutes read

Removing Old and Unused Docker Images

To keep your Docker environment clean and save disk space, it's essential to regularly remove old and unused Docker images.

Listing Docker Images

Before removing any Docker images, let's first list all the existing images on your system to identify the ones you want to remove. Open your terminal or command prompt and run the following command:

docker images

This command will display a list of all Docker images along with their repository, tag, image ID, creation date, and size.

Removing Single Docker Images

To remove a specific Docker image, you need to know its Image ID or Repository and Tag combination. Replace <IMAGE_ID> with the actual Image ID or <REPOSITORY>:<TAG> with the desired repository and tag. Run the following command to remove the specified image:

docker rmi <IMAGE_ID>

or

docker rmi <REPOSITORY>:<TAG>

Removing Dangling (Unused) Docker Images

Dangling images are those that have no tag and are not associated with any container. They are essentially unused and can be safely removed to free up disk space. To remove dangling images, use the following command:

docker image prune

Confirm the action by typing y when prompted.

Removing All Unused Docker Images

If you want to remove all unused images, including dangling images, you can use the following command:

docker image prune -a

Confirm the action by typing y when prompted.

Removing Images Based on Filters

You can remove Docker images based on various filters such as image name, label, before a specific date, or by size. To do this, use the docker image prune command with appropriate filters. Here are some examples:

  • To remove all images with a specific name:

    docker image prune -a --filter "label=<LABEL>"
    
  • To remove images created before a certain date:

    docker image prune -a --filter "until=<YYYY-MM-DD>"
    
  • To remove images larger than a specific size:

    docker image prune -a --filter "size=<SIZE>"
    

Remember to replace <LABEL>, <YYYY-MM-DD>, and <SIZE> with the actual values you want to use for filtering.

Removing All Unused Images and Volumes

If you want to go even further and remove all unused images as well as unused volumes, use the following command:

docker system prune -a --volumes

Confirm the action by typing y when prompted.

Please be cautious while running the above commands, as they will permanently delete Docker images and volumes. Make sure to double-check before confirming the removal.

With these commands, you can efficiently manage your Docker images and keep your system clean and organized. Happy Dockerizing!

0 Comment