Is this a good thing to remove Docker images?
Absolutely, a good question, do this carefully!! Removing an image will erase all information about your container. So do this, if you want really need it.
Please go through the discussion here before starting the removal. Docker Removing Images – Linux Academy.
You’re safe. This command wont erase images which are used by your running and or stopped containers. You will get alert like:
Error response from daemon: conflict: unable to delete d5668b07b850 (cannot be forced) - image is being used by running container 247d66f3aa77
Error response from daemon: conflict: unable to delete 0458a4468cbc (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete 0458a4468cbc (must be forced) - image is referenced in multiple repositories
Error response from daemon: conflict: unable to delete aab791fdea6e (must be forced) - image is being used by stopped container 9e8df4ee108b
What is the command to remove an Docker image?
docker rmi image-name/image-id
See the example added below:
# docker rmi mirhosting/centos7-cpanel
Untagged: mirhosting/centos7-cpanel:latest
Untagged: mirhosting/centos7-cpanel@sha256:1de492e1ccb1cab42ef73dbd18b6a35646d5b19aebd6b0b8301245fcaef3b837
Deleted: sha256:4e4fe3940249060c9177b5c4a025b85b5fab4c77b0171ed286f13d0344c7927e
Deleted: sha256:d9a8fbedcc91b90e57b22ef84fc1d2072c26f5ec51560f835a4f867bc74bf562
Deleted: sha256:d6583503b314e14521dec49621cee52afe45be78ee311cdb2bb2684fb28196c1
Deleted: sha256:35e40b5b04b6911d0a78aff6fb8e7beca43d8cef4047e2b1e81d8207b632b6ce
Deleted: sha256:429fa0f2c8f17cc4f07b994c0dc68bad0cffa997f834511edf28e7be6214277e
Deleted: sha256:1d781640e737410c1943b3cd2a1f03ea7a0373982eaf43010ea0a6c78137a0b2
Deleted: sha256:6dcd5b482048f891a3e4c596c10d94c6baa27699d0cbd7cfe58ddbdd54ba5bc6
Deleted: sha256:896902e74958c5156522aa693b9bb5044367fe710bf7d6568cc79a3bbd5b803f
Deleted: sha256:72d2f3dbda13ef86ffdb89910f7237e38b057ef6a53defc508121d1075d56011
Deleted: sha256:d689117a523b6b79a319075619ce0b37c5e42a6ced71f395fd9f6e7711ddcf86
Deleted: sha256:38c2750ebbaf9c4b7bbf6329af3f6ea8fb6bce95767be8313b3bd68a21bc0a69
Deleted: sha256:ebc5c18085638a413cb5f06636f582c57d3c1a65f857f73d51fd1833a8df6457
Deleted: sha256:f2ad9f893382d2b8a124fae7753919627d0597fa091800a1ced8d63a58220725
Deleted: sha256:e7cc3ccad6eb927f5287cbe9273c2834c09264d9343b13b595d3483b9ebecdfb
It will remove all layers of that image.
Here I am explaining some tips to remove all images in a single shot.
Yes, we are sys admins, we can do this in many ways if we have the image ID. We can do this in help of for loop, passing the ID as a variable to docker rmi command etc.. You can decide the way which you want 🙂
So first we must know the commands to take the image ids.
How do I get the image ID of a Docker image?
The third field of docker images command is its ID. So first run the command:
docker images
Example
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 latest 67bab6537ca8 2 months ago 513MB
wordpress latest d5668b07b850 2 months ago 442MB
docker-redis latest 08a845c3d70f 2 months ago 154MB
redmine latest 887149aec1cd 2 months ago 663MB
tkd node 9a380e27d27f 2 months ago 676MB
To print Docker image ID only, use the following command:
docker images | awk {'print $3'}
See the sample output:
# docker images | awk {'print $3'}
IMAGE
67bab6537ca8
d5668b07b850
08a845c3d70f
887149aec1cd
9a380e27d27f
41a1f5b81103
2d194b392dd1
9dafae5efa4e
8c800b6e4b9f
d03d5f0ac183
Other option to list image ID using docker command:
docker images -q
Here we need to discuss about the filter switch along with docker command. The filter switch option is “-f”.
We can use this filter option in many use cases. In this post we only discussing about the dangling feature.
dangling (boolean – true or false)
if dangling = true
This will display untagged images that are the leaves of the images tree (not intermediary layers). These images occur when a new build of an image takes the repo:tag away from the image ID, leaving it as : or untagged. A warning will be issued if trying to remove an image when a container is presently using it. By having this flag it allows for batch cleanup.
if dangling = false
Prints all other images ID.
What’s the next option to print image ID only?
Yeah, the third option which we are discussing about the –format switch along with docker images command. This can also help us here. Using the format switch we can print the image IDs in a single short.
The command is added below:
docker images --format "{{.ID}}"
Okay back to the question. How to remove all Docker images in a single short?
You can use the following commands:
docker rmi $(docker images -q)
docker rmi $(docker images --format "{{.ID}}")
docker rmi $ (docker images | awk {'print $3'})
With dangling options..
docker rmi $(docker images -f "dangling=true" -q)
docker rmi $(docker images -f "dangling=false" -q)
That’s it!!
Go back to Docker tricks and tips