HowtoForge

How to Delete a Docker Image on Linux

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:

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 <image_id>

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

docker rmi <repository>:<tag>

Explanation:

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 <image_id>

Explanation:

Warning:

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:

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:

Additional Tips

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

How to Delete a Docker Image on Linux