Log in

Video Lesson: Remove an image in Docker

Remove an image in Docker

Learn how to remove Docker images using docker rmi, prune unused images, and efficiently reclaim disk space.


Lesson Content

Docker images eat up disk space quickly. Between old versions, failed builds, and experiments you forgot about, ...you'll need to clean house regularly.

The docker rmi command removes images from your system:

docker rmi <image-name or image-id>

Let's say you want to remove that nginx image:

docker rmi nginx

You can also use the image ID instead:

docker rmi 35f3cbee4fb7

Remove Docker images used by containers

But you need to know that Docker won't let you remove an image if a container is using it, even if that container is stopped. You'll get an error like this:

Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 2f3d4e5a is using its referenced image 605c77e624dd

You've got two options. Either remove the container first, then you'll be able to remove the image:

docker rm e6519a292cfd  # Container ID
docker rmi 35f3cbee4fb7  # Image ID

Or force the image removal with the -f flag:

docker rmi -f nginx

But be careful with force, as it leaves containers in a weird state where they reference an image that no longer exists.

Remove multiple Docker images at once

If you want to remove multiple images at once, you can just list them all out:

docker rmi redis:8.0-alpine redis:8.2-alpine

For surgical precision, you can also combine docker images with docker rmi:

# Remove the oldest two images
docker rmi $(docker images nginx -q | tail -n 2)

Prune Docker images to reclaim disk space

We may also want to remove any old images, such as dangling ones. They're perfect candidates for cleanup.

Here's how to wipe them all out in one go:

docker image prune

Docker asks for confirmation, then removes all dangling images.

You can also use the -a flag instead to remove all unused images, not just the dangling ones:

docker image prune -a

This removes any image not currently used by a container. It's the nuclear option for reclaiming disk space in a hurry.