0
0
DockerHow-ToBeginner · 3 min read

How to Remove Docker Image: Simple Commands Explained

To remove a Docker image, use the docker rmi IMAGE_NAME_OR_ID command. This deletes the specified image from your local system, freeing up space.
📐

Syntax

The basic command to remove a Docker image is docker rmi followed by the image name or ID. You can specify one or more images separated by spaces.

  • docker rmi IMAGE_NAME_OR_ID: Removes the specified image.
  • If the image is used by any container, the command will fail unless you force removal.
  • Use -f or --force to force removal even if the image is in use.
bash
docker rmi IMAGE_NAME_OR_ID
💻

Example

This example shows how to list Docker images, remove one by its image ID, and verify it is deleted.

bash
docker images
# Lists all images

# Remove image by ID (replace IMAGE_ID with actual ID)
docker rmi IMAGE_ID

# Verify removal
docker images
Output
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 2ca708c1c9cc 2 weeks ago 72.9MB hello-world latest fce289e99eb9 3 months ago 13.3kB Untagged: ubuntu:latest Deleted: sha256:2ca708c1c9cc... REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 3 months ago 13.3kB
⚠️

Common Pitfalls

Common mistakes when removing Docker images include:

  • Trying to remove an image that is still used by a running or stopped container, which causes an error.
  • Not using the correct image name or ID, leading to "No such image" errors.
  • Forcing removal without understanding dependencies can break containers relying on that image.

Always check running containers with docker ps -a before removing images.

bash
docker rmi IMAGE_ID
# Error: conflict: unable to delete IMAGE_ID (must be forced) - image is being used by running container

docker rm CONTAINER_ID
# Remove container first

docker rmi IMAGE_ID
# Now image removal succeeds
Output
Error response from daemon: conflict: unable to delete IMAGE_ID (must be forced) - image is being used by running container CONTAINER CONTAINER_ID Untagged: IMAGE_ID Deleted: sha256:IMAGE_ID
📊

Quick Reference

CommandDescription
docker rmi IMAGE_NAME_OR_IDRemove one or more Docker images by name or ID
docker rmi -f IMAGE_NAME_OR_IDForce remove image even if used by containers
docker imagesList all Docker images on the system
docker ps -aList all containers, running or stopped
docker rm CONTAINER_IDRemove a container to free image usage

Key Takeaways

Use docker rmi IMAGE_NAME_OR_ID to remove Docker images safely.
Check for containers using the image before removal to avoid errors.
Use -f flag to force removal but be cautious of dependencies.
List images with docker images and containers with docker ps -a.
Remove containers first if they block image deletion.