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
-for--forceto 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 imagesOutput
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
| Command | Description |
|---|---|
| docker rmi IMAGE_NAME_OR_ID | Remove one or more Docker images by name or ID |
| docker rmi -f IMAGE_NAME_OR_ID | Force remove image even if used by containers |
| docker images | List all Docker images on the system |
| docker ps -a | List all containers, running or stopped |
| docker rm CONTAINER_ID | Remove 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.