How to map docker images to a dockerfile? - docker

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.

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.

Docker old images cleanup?

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

Why doesn't my newly-created docker have a digest?

I have been following the Docker tutorial here, and built a test image on my local OSX machine by committing changes to an existing image and tagging it with three different labels:
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
adamatan/sinatra devel fccb6b4d21b4 8 minutes ago 469.5 MB
adamatan/sinatra junk fccb6b4d21b4 8 minutes ago 469.5 MB
adamatan/sinatra latest fccb6b4d21b4 8 minutes ago 469.5 MB
However, none of these images has a digest:
# docker images --digests adamatan/sinatra
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
adamatan/sinatra devel <none> fccb6b4d21b4 9 minutes ago 469.5 MB
adamatan/sinatra junk <none> fccb6b4d21b4 9 minutes ago 469.5 MB
adamatan/sinatra latest <none> fccb6b4d21b4 9 minutes ago 469.5 MB
Other test images I have created with a Dockerfile do have a digest.
Why do some images have a digest and some don't? Is it related to the way the images were created (Dockerfile or not)?
Firstly, Please keep in mind that a digest could represent a manifest, a layer or a combination of them (we normally called that combination an image).
Manifest is a new term that introduced with Docker registry V2. Here is a short description fetched from Docker Registry V2 slides page21 ~ page23:
[Manifest] describes the components of an image in a single object
Layers can be fetched immediately, in parallel.
When you get the digests with command docker images --digests, here the digest is the SHA256 hash of image manifest, but image ID is the hash code of the local image JSON configuration (this configuration is different from manifest). In this case, if an image doesn't have an associated manifest, the digest of that image will be "none".
Normally, two scenarios could make an image doesn't have associated manifest:
This image has not been pushed to or pulled from a V2 registry.
This image has been pulled from a V1 registry.
To generate a manifest, the easiest way is to push the image to a V2 registry (V1 registry will not works). Docker client will generate a manifest locally, then push it with image layers to registry. When you pull the image back, the image will has a manifest.
Once the manifest existing, your image digest should not be "none".
Yes it is related to how the images were created. Docker can be a real stinker at times.
This may be helpful for you in this case.
I was also facing this issue (digest was none).
Reason was when I can the docker image and image was listed in docker images.
At this point, I was checking for the digest value using
docker images --digest
and the digest value comes as <none>.
Resolution:
Push the image in your docker repository, then image will show the digest value.

Resources