CodeBlanks Logo Icon CodeBlanks

docker rmi

Learn to remove images with docker rmi.

docker rmi

docker rmi (docker image rm) deletes an image from the local store. If a container (running or stopped) uses the image, removal is refused; the container must be removed first:

$ docker rmi nginx:alpine
Error response from daemon: conflict: unable to remove repository reference "nginx:alpine" (must force) - container 7c2f4a9e1b3d is using its referenced image
$ docker rm web && docker rmi nginx:alpine
Untagged: nginx:alpine
Deleted: sha256:a5b6c7d8…

Bulk cleanup

Instead of deleting one by one, prune commands are used:

docker image prune          # only dangling images
docker image prune -a       # all images no container uses
docker image prune -a --filter "until=168h"   # those older than a week
docker rmi $(docker images -q -f dangling=true)

-a should be used carefully; images you use often are deleted too and need to be pulled again.

Practice Image cleanup

Write three commands: force-remove the web container; delete the nginx:alpine image; then clean up the dangling images without asking for confirmation.

Starter code

terminal
# buraya yaz

Sample solution

terminal
docker rm -f web
docker rmi nginx:alpine
docker image prune -f

Expected output

web
Untagged: nginx:alpine
Deleted: sha256:a5b6c7d8e9f0
Total reclaimed space: 162MB

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 For an image with several tags, what does docker rmi api:1.0 do?

  • It deletes the containers
  • It removes only that tag; the image stays with the other tags Correct answer
  • It deletes all tags and the image
  • It errors

Explanation: When the last tag goes, the image is deleted too ("Untagged" and "Deleted").

2 Delete all unused images.

Question
docker image prune ____ -f
Answer
docker image prune -a -f

Explanation: -a also covers images that are unused but not dangling.

3 Match.

Term Meaning
docker rmi Deletes an image
Untagged Tag removed
image prune Cleans dangling ones
prune -a All unused

All lessons in Beginner