Docker is a platform that simplifies the process of developing, shipping, and running applications in isolated environments called containers. Containers package everything an application needs to run, including code, runtime, libraries, and settings, ensuring consistency across different environments, whether it’s on a developer’s machine or in production. A Docker Image is a lightweight, standalone, and executable package that contains all the instructions to create a Docker container. It includes the application code, dependencies, and any necessary system tools or libraries. Once built, Docker Images can be shared, reused, and deployed, making them an essential component in ensuring application consistency and scalability across various systems.

Docker images can take up significant disk space over time, especially if you frequently build and pull images. This guide will walk you through the process of deleting Docker images that are no longer needed.

Step 1: List Docker Images

Before deleting any images, it’s a good idea to first list all the images currently available on your system.

Command:

docker images

Explanation: This command will display a list of all images, showing the repository, tag, image ID, creation date, and size.

Step 2: Identify the Image(s) to Delete

From the list generated by the previous command, identify the image(s) you want to delete. You can choose an image by its IMAGE ID, REPOSITORY, or TAG.

Hint:

  • IMAGE ID is a unique identifier for each image, which is the safest option to use.
  • REPOSITORY and TAG refer to the name and version of the image, respectively.

Step 3: Delete the Docker Image

To delete a specific Docker image, use the docker rmi command followed by the IMAGE ID, REPOSITORY:TAG, or IMAGE NAME.

Command:

docker rmi 

Or, if you prefer to use the repository and tag:

docker rmi :

Explanation:

  • This command will remove the image specified.
  • If the image is being used by a running container, Docker will prevent you from deleting it to avoid disruptions.

Example:

docker rmi 7d9495d03763

or

docker rmi ubuntu:latest

Step 4: Force Delete an Image (Optional)

If an image has multiple tags or is used by a stopped container, Docker may not delete it immediately. In such cases, you can forcefully remove the image.

Command:

docker rmi -f 

Explanation:

  • The -f or --force flag forces Docker to delete the image even if it is tagged multiple times or is associated with stopped containers.

Warning:

  • Use this command with caution as it can lead to unintended consequences if the image is still in use.

Step 5: Delete All Unused Images (Optional)

If you want to clean up all unused images (dangling images), you can use the docker image prune command.

Command:

docker image prune

Explanation:

  • This command deletes all dangling images—images that are not tagged and are not referenced by any container.
  • Docker will prompt you for confirmation before proceeding.

Hint: To remove all unused images, not just the dangling ones, use:

docker image prune -a

Step 6: Verify the Deletion

After deleting the image(s), you can verify that they have been removed by listing the images again.

Command:

docker images

Explanation:

  • If the image was successfully deleted, it will no longer appear in the list.

Additional Tips

  • Check Disk Space: After deleting images, it’s a good practice to check your disk space to ensure that the deletions have freed up space. You can use the docker system df command for this purpose.

  • Delete Unused Volumes and Networks: Occasionally, unused volumes and networks can also consume disk space. Consider running docker volume prune and docker network prune to clean these up.

  • Be Cautious: Always double-check before deleting images, especially in a production environment, as removing images in use can disrupt services.

By following these steps, you can effectively manage and delete Docker images, keeping your Docker environment clean and optimized.