'docker image ls' gives huge <none> image list [duplicate] - docker

This question already has answers here:
Docker remove <none> TAG images
(37 answers)
Closed 3 years ago.
I'm kinda new to docker but has some fair knowledge about it. Recently I wanted to list down the my docker images, so I ran docker image ls, which gives me a bit of large list of pulled images..
Above list shows the part of the image list in the machine. I have some knowledge about this <none> images which are known as intermediate images. But the problem is they are huge some images are almost 1GB. To my knowledge I never ran a single huge container other than gitlab. When I was done with it I removed the image. Can someone explain why these intermediate images are large and way to get rid of it without damaging other image layers?

docker system prune
Is your friend to clean up so called "dangling images" (among other things), for more background information please refer to the following thread:
What is a dangling image and what is an unused image?

docker container prune # Remove all stopped containers
docker volume prune # Remove all unused volumes
docker image prune # Remove unused images
docker system prune # All of the above, in this order: containers, volumes, images
docker system df # Show docker disk usage, including space reclaimable by pruning

Related

How do I clean docker?

An error occurred because there is not enough disk space
I decided to check how much is free and came across this miracle
Cleaned up via docker system prune -a and
docker container prune -f
docker image prune -f
docker system prune -f
But only 9GB was cleared
Prune removes containers/images that have not been used for a while/stopped. I would suggest you do a docker ps -a and then remove/stop all the containers that you don't want with docker stop <container-id>, and then move on to remove docker images by docker images ps and then remove them docker rmi <image-name>
Once you have stooped/removed all the unwanted containers run docker system prune --volumes to remove all the volumes/cache and dangling images.
Don't forget to prune the volumes! Those almost always take up way more space than images and containers. docker volume prune. Be careful if you have anything of value stored in them though.
It could be your logging of the running containers. I've seen Docker logging writing disks full with logging. By default Docker containers can write unlimited logs.
I always add logging configuration to my docker-compose restrict total size. Docker Logging Configuration.
From the screenshot I think there's some confusion on what's taking up how much space. Overlay filesystems are assembled from directories on the parent filesystem. In this case, that parent filesystem is within /var/lib/docker which is part of / in your example. So the df output for each overlay filesystem is showing how much disk space is used/available within /dev/vda2. Each container isn't using 84G, that's just how much is used in your entire / filesystem.
To see how much space is being used by docker containers, images, volumes, etc, you can use docker system df. If you have running containers (likely from the above screenshot), docker will not clean those up, you need to stop them before they are eligible for pruning. Once containers have been deleted, you can then prune images and volumes. Note that deleting volumes deletes any data you were storing in that volume, so be sure it's not data you wanted to save.
It's not uncommon for docker to use a lot of disk space from downloading lots of images (docker makes it easy to try new things) and those are the easiest to prune when the containers are stopped. However what's harder to see are the logs that containers are outputting which will slowly fill the drive within the containers directory. For more details on how to clean the logs automatically, see this answer.
If you want, you could dig deep at granular level and pinpoint the file(s) which are the cause of this much disk usage.
du -sh /var/lib/docker/overley2/<container hash>/merged/* | sort -h
this would help you coming to a conclusion much easily.

Eliminate docker <none> images when building using docker-compose

I am working on a microservice with several docker containers. Using docker-compose creates the images I want but also several other copies of images which fill up my disk space. What are the use of this other child images and can I stop them from being created since they are using up my memory. Please note they are not really "dangling" they just appear on build.
The images are the intermediate layers resulted from docker build. They are the parent layers for your final image and cannot be removed as your latest image actually refers to them.
Only those images which are not referenced by any other layers can be removed. These images are called dangling. You can use the following command to remove the dangling images:
docker rmi $(docker images -f "dangling=true" -q)

Many <none> images created after build a docker image

FROM scratch
MAINTAINER Aario <AarioAi#gmail.com>
ENV SHARED_GROUP docker
I build a docker image with above dockerfile.
After runing docker build -t "aario/centos" .
It creates this image aario/centos and the <none> image:
Is it ok? And how to solve it?
When I run docker rmi 2f??????? to remove the aario/centeros image. The <none> image will be removed on the same time.
Docker image is composed of layers:
docker history aario/centos
Each row you see using command above is a separate layer. Also, with time, a number of "orphaned" layers will fill up your disk space. You can safely remove them with:
docker image prune
Those are intermediate image layers. I.e, all of the steps of your Dockerfile.
You can check what these images are made of with inspect command:
docker image inspect 2f???????
They do not use additional disk space, since they are part of your named image. They can save space when you have multiple Dockerfiles with the same steps (in the same order) because they work as cache.
A longer and more complete explanation can be found here.
Eventually, if you delete your image or change build steps, you could have dangling none images. Those can be deleted, as #Mike mentioned, with:
docker image prune

What is a dangling image and what is an unused image?

In the docker documentation of docker image prune it is possible to use the -a flag to
Remove all unused images, not just dangling ones
and later
Remove all dangling images. If -a is specified, will also remove all images not referenced by any container.
Can someone explain to me what dangling images are and what's the difference between dangling and unused images?
An unused image means that it has not been assigned or used in a container. For example, when running docker ps -a - it will list all of your exited and currently running containers. Any images shown being used inside any of containers are a "used image".
On the other hand, a dangling image just means that you've created the new build of the image, but it wasn't given a new name. So the old images you have becomes the "dangling image". Those old image are the ones that are untagged and displays "<none>" on its name when you run docker images.
When running docker system prune -a, it will remove both unused and dangling images. Therefore any images being used in a container, whether they have been exited or currently running, will NOT be affected.
Safest and Easiest way to cleanup Dangling Images
docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
Docker images consist of multiple layers. Dangling images, are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space.
Note: I recommend not to use prune in production, because docker system prune -a will remove all the images which are not referenced by the container, by which we can't roll back to the previous release.
To list dangling images by adding the filter flag, -f with a value of dangling=true to the docker images.
List Dangling images
docker images -f dangling=true
Remove Dangling Images
docker rmi $(docker images -f dangling=true -q)
OR
docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi
When we run any cron jobs to delete tha dangling stuff use the above to make sure the job runs successfully. Like in Jenkins if we run a free style job with beloow commad job will never fail even if no dangling stuff exists in the machine.
This is the safest and easiest way to cleanup dangling images and get back our disk space back for use.
Images in docker are referenced by a sha256 digest, often referred to as the image id. That digest is all you need for the image to exist on the docker host. Typically, you will have tags that point to these digests, e.g. the tag 'busybox:latest' currently points to image id c30178c523... on my system.
Multiple tags can point to the same image, and any tag can be changed to point to a different id, e.g. when you pull a new copy of busybox:latest or build a new version of your application image.
Dangling images are images which do not have a tag, and do not have a child image (e.g. an old image that used a different version of FROM busybox:latest), pointing to them. They may have had a tag pointing to them before and that tag later changed. Or they may have never had a tag (e.g. the output of a docker build without including the tag option).
These are typically safe to remove as long as no containers are still running that reference the old image id. The main reason to keep them around is for build caching purposes.
In addition, you may have downloaded images that you are not currently used by containers (including stopped containers). These are entirely different from dangling images and may be safe to remove as long as you don't plan to use them in the future or don't mind downloading another copy when you do need them.
Dangling images are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space.
An unused image is an image that has not been assigned or used in a container.
To list dangling images:
docker images -f dangling=true
Dangling images are untagged images. The following command gives a list of dangling images.
docker images --filter "dangling=true"
docker image prune deletes all dangling images.
Unused images are images that have tags but currently not being used as a container. You may or may not need it in future.
docker image prune -a delete all dangling as well as unused images.
You generally don't want to remove all unused images until some time. Hence it is better to remove with a filter.
docker image prune -a -f --filter "until=6h"
In the images screenshot, "none" name are dangling images.
A dangling image just means that you've created the new build of the image, but it wasn't given a new name. So the old images you have becomes the "dangling image". Those old image are the ones that are untagged and displays "" on its name when you run docker images.
docker system prune -a, it will remove both unused and dangling images. Therefore, any images being used in a container, whether they have been exited or currently running, will NOT be affected.
I saw useful commands (aliases) for removing dangling images, courtesy of andyneff here: https://forums.docker.com/t/how-to-delete-cache/5753:
alias docker_clean_images='docker rmi $(docker images -a --filter=dangling=true -q)'
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'
The first one
cleans all dangling images. This is useful for removing intermediate
images left over from multiple builds. The second one is for removing
stopped containers. These are aliases I use for routine maintenance
If you want to remove ALL of your cache, you first have to make sure
all containers are stopped and removed, since you cannot remove an
image in use by a container. So something similar
docker kill $(docker ps -q) docker_clean_ps docker rmi $(docker images
-a -q)
This would kill and remove all images in your cache.

docker build creates another set of docker image even the build command is the same?

Assume that you can create a 500MB docker image with following command
docker build -t demo:1.0 .
It seems to me that the docker image (demo:1.0) is overwritten when I re-run the same command. However, I noticed that free disk space decreases as I re-run the command. Why does this happen? Is docker creating another set of the image somewhere in the file system? I'd like to know how I can find old-generations of the docker images and delete them.
It's standard docker behavior, if you run
docker images
you may see a dangling image like this
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
demo 1.0 a1b38444260e 6 days ago 500 MB
<none> <none> 46250b7172c5 6 days ago 500 MB
You can remove the dangling images with the command
docker rmi $(docker images -qa -f "dangling=true")
Docker doesn’t delete such images automatically, despite, it's a logical feature to have. For now this command can be used to do a manual deletion.
While the images are stored in /var/lib/docker/{driver-name}, you wouldn't remove images manually from that path.

Resources