Docker old images cleanup? - docker

The output of sudo docker images shows images that were created ages ago, I don't see any use for them, sudo docker system prune does not remove them either.
How can I easily delete old images? Do I have to write a script to conditionally delete based on date created within the output of docker images?

I don't think you can filter images by created date, but you can use another image as a reference of time.
The flag (-f or--filter) with before shows only images created before the image with given id or reference.
For example, having these images:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
image1 latest eeae25ada2aa 4 minutes ago 188.3 MB
image2 latest dea752e4e117 9 minutes ago 188.3 MB
image3 latest 511136ea3c5a 25 minutes ago 188.3 MB
Filtering with before (image1) would give:
$ docker images --filter "before=eeae25ada2aa"
REPOSITORY TAG IMAGE ID CREATED SIZE
image2 latest dea752e4e117 9 minutes ago 188.3 MB
image3 latest 511136ea3c5a 25 minutes ago 188.3 MB
To Remove images before image1(eeae25ada2aa)
$ docker rmi $(docker images --filter "since=511136ea3c5a" -q)
dea752e4e117
511136ea3c5a

Related

Deleting the Docker disk image size

I'm using docker and I got an error "No space on the drive" when I tried to use the docker. So, I searched online and found that I had to remove the the unused images from the docker. So, I used the command
docker system prune --all
It deleted about ~30GB of the images. But When I run the docker again I got the same "No space" issue and when I checked the disk image size it showed me that 64 GB used even though I deleted the unused containers.
I had to increase the size of the drive every time when I use docker.
docker images output
REPOSITOY TAG IMAGE ID CREATED SIZE
postgres latest aal783da97 2 days ago 6.32GB
redis latest aal783da98 2 days ago 6.32GB
mysql latest aal783da99 2 days ago 6.32GB
airflow_worker latest aal783da91 2 days ago 6.32GB
airflow_scheduler latest aal783da92 2 days ago 6.32GB
How do I delete all the 64.1 GB?

What is docker image reference?

Docker documentation mentions image reference in many places. However, running docker images command gives the list of images with the following properties: REPOSITORY, TAG, IMAGE ID, CREATED, SIZE - no reference. Is 'reference' a synonym for ID or digest, or something else?
The docker image reference is the combination of the REPOSITORY and TAG in this format REPOSITORY:TAG where they are both separated by :. So if you have an image with a REPOSITORY of IMAGE1 and a tag of latest the image reference would be IMAGE1:latest. The knowledge of an image reference would help you to filter by docker image list by reference by running:
docker images --filter=reference='myDocker*:*dev'
The above command will return all docker images that the repository name starts with myDocker and the tag name ends with dev.
To add on to Kelvin's answer, Reference is the Repository which you will use with the tag. Have a look the below example.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest e02e811dd08f 5 weeks ago 1.09 MB
busybox uclibc e02e811dd08f 5 weeks ago 1.09 MB
busybox musl 733eb3059dce 5 weeks ago 1.21 MB
busybox glibc 21c16b6787c6 5 weeks ago 4.19 MB
As you can see above, my reference would be respectively
busybox:latest
busybox:uclibc
busybox:musl
busybox:glibc
If you only use the reference as busybox, by default it will use the latest tag.
You can filter the images on the reference filter as well.
docker images --filter=reference='busy*:*libc'
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox uclibc e02e811dd08f 5 weeks ago 1.09 MB
busybox glibc 21c16b6787c6 5 weeks ago 4.19 MB
You use the image reference in Dockerfile as well when you build the image by using the FROM directive.
FROM busybox #Reference is used as you can see and automatically, `latest` tag will be pulled
...
Ref:- https://docs.docker.com/engine/reference/commandline/images/
Ref:- https://docs.docker.com/engine/reference/builder/#from

How do I delete local Docker images by repository?

I have a bunch of docker images I want to get rid of and it would be convenient if I could remove them by specifying the repository name as it appears when I run docker images. For example if docker images returns:
REPOSITORY TAG IMAGE ID CREATED SIZE
ui_test 191127_manual 41a7ca9824d6 24 hours ago 1.42GB
ui git-24fa8d1a cdd254eff918 24 hours ago 1.44GB
ui git-31a4b052 9b4740060a62 25 hours ago 1.45GB
ui_test 191122_manual ba9cb04ce2d8 6 days ago 1.39GB
ui git-68110e426 f26ef80abc25 6 days ago 1.38GB
what command would I use to remove all of the ui_test images?
You can pass image IDs you want to delete to docker rmi:
docker rmi $(docker images -q 'ui_test')
From the docs:
The docker images command takes an optional [REPOSITORY[:TAG]] argument that restricts the list to images that match the argument. If you specify REPOSITORYbut no TAG, the docker images command lists all images in the given repository.

How to map docker images to a dockerfile?

Asking this question out of curiosity:
Is it possible to map the docker images and Dockerfiles if they are available in the same machine/server ?
If not is there any way to tag an image which can refer to the Dockerfile ...something of this sort ?
My requirement is know how the images are formed from the docker files if I have images of following sort:
root#labadmin-VirtualBox:~/RAGHU/DOCKER# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
env_path 1.1 01502209b2fc 13 minutes ago 187.9 MB
env_path 1.0 3610243625cd 14 minutes ago 187.9 MB
env_path 1.2 3610243625cd 14 minutes ago 187.9 MB
env_path 2.0 c6d1eb2cff1b 14 minutes ago 187.9 MB
An approach to achieve what you requested, is using the LABEL field in your Dockerfile to save the path or an id of your dockerfiles. Then, you could use the docker inspect command in order to retrieve this information.
You can find more information about labels and how to manage them in this link.

If images in Docker are untagged, are they used at all or just orphans?

Running the commands docker images and docker images -a results in the following outputs:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 9cbaf023786c 2 days ago 192.8 MB
$ docker images -a
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 9cbaf023786c 2 days ago 192.8 MB
<none> <none> 03db2b23cf03 2 days ago 192.8 MB
<none> <none> 8f321fc43180 2 days ago 192.8 MB
<none> <none> 6a459d727ebb 2 days ago 192.8 MB
<none> <none> 2dcbbf65536c 2 days ago 192.8 MB
<none> <none> 97fd97495e49 2 days ago 192.6 MB
Are the images tagged <none> of any importance? If not: why do they come with the tagged images I pulled? Do the sizes add up or are they just a repition? If so, can I delete them without any effect on my work?
The image files are independent, and combine via unionfs magic to form a running container.
The images you care about are often tagged with memorable names.
You can delete the unused images, i.e. those not contributing to any image you care about. I do it this way in bash:
function docker_rm_unnamed_images {
sudo docker rmi $(sudo docker images | grep '^<none>' | awk '{print $3}')
}
You can think of a docker image as a stack of 'layers'. Each Dockerfile command adds an additional layer to the image. It's important to realize that each of those commands creates a separate image file. So the Dockerfile
FROM foo
RUN a
RUN b
RUN c
would be a stack of
image=1 (possibly pulled from the foo registry)
image=2 (after applying a to image 1)
image=3 (after applying b to image 2)
image=4 (after applying c to image 3)
It is likely that the foo image was composed of multiple other layers, so your final image is a stack of 4 or more images. Each of those image files lives in your docker image registry. Most of them are unnamed, because they correspond to a RUN command, for example. Each of the image files 1-4 are probably fairly small (unless they correspond to a yum install p1 .. p100 for example). Together they make up the file system of the container that you ultimately run.
To remove all untagged images, the additional options is needed:
docker rmi $(sudo docker images -a | grep '^<none>' | awk '{print $3}')
option '-a': show all images, including untagged images

Resources