docker rmi
- Docker & Kubernetes course
- Beginner
- Lesson 38/82
- 4 min
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
# buraya yazSample solution
docker rm -f web
docker rmi nginx:alpine
docker image prune -fExpected 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.
docker image prune ____ -f
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 |