0
0
Dockerdevops~5 mins

Removing images in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your computer fills up with old or unused Docker images. Removing these images frees up space and keeps your system clean.
When you have images that you no longer use and want to free disk space
When you want to remove a broken or corrupted image to fix Docker issues
When cleaning up after testing multiple versions of an app image
When you want to remove dangling images that have no tags
When preparing your system before pulling fresh images
Commands
List all Docker images on your system so you can see their IDs and names before removing any.
Terminal
docker images
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 4bb46517cac3 2 weeks ago 133MB hello-world latest fce289e99eb9 3 months ago 13.3kB
Remove the Docker image with the ID 4bb46517cac3 to free up space and clean unused images.
Terminal
docker rmi 4bb46517cac3
Expected OutputExpected
Untagged: nginx:latest Deleted: sha256:4bb46517cac3a1b2c3d4e5f67890123456789abcdef0123456789abcdef012345
Remove all dangling images (images not tagged or used by any container) to clean up space quickly.
Terminal
docker image prune -f
Expected OutputExpected
Deleted Images: untagged: <none>:<none> Total reclaimed space: 50MB
-f - Force removal without asking for confirmation
Key Concept

If you remember nothing else, remember: use 'docker rmi' with the image ID or name to remove specific images and 'docker image prune' to clean up unused dangling images.

Common Mistakes
Trying to remove an image that is still used by a running container
Docker will refuse to remove images that are in use, causing an error.
Stop and remove containers using the image before removing the image.
Using 'docker rmi' without specifying the correct image ID or name
The command will fail because Docker cannot find the image to remove.
Run 'docker images' first to get the exact image ID or name.
Not using the '-f' flag with 'docker image prune' and expecting it to run without confirmation
The command will prompt for confirmation and wait, which can interrupt scripts.
Add '-f' to force prune without confirmation in automated scripts.
Summary
Use 'docker images' to list all images and find the image ID or name.
Use 'docker rmi IMAGE_ID' to remove a specific image by its ID.
Use 'docker image prune -f' to remove all dangling images quickly.