How can I find the images being pulled from the SHA1s? - docker

When we run a docker container if the relevant image is not in the local repo it is being downloaded but in a specific sequence i.e parent images etc.
If I don’t know anything about the image how could I find from which images is being based on based on the layers pulled as displayed in a docker run?
The output only shows the SHA1s on any docker run etc

AFAIK, you can't, there is no reverse function for a hash.
Docker just tries to get the image from local, when its not available tries to fetch it from the registry. The default registry is DockerHub.
When you don't specify any tag when running the container ie: docker run ubuntu instead of docker run ubuntu:16.04 the default latest is used. You'll have to visit the registry and search which version the latest tag is pointing to.
Usually in DockerHub there is a link pointing the GitHub repo where you can find the Dockerfile, in the Dockerfile you can find how its built, including the root image.
You also can get some extra info with docker image inspect image:tag, but you'll find more hashes in the layers.

Take a look to dockerfile-from-image
"Similar to how the docker history command works, the dockerfile-from-image script is able to re-create the Dockerfile (approximately) that was used to generate an image using the metadata that Docker stores alongside each image layer."
With this, maybe you can get the source of the image.

Related

Looking for an easy way to get the Dockerfile of an image hosted in Docker hub

I know that I can pull a docker image locally and use docker image inspect to get details about the image details in the ContainerConfig section.
I am looking for a way to get the Dockerfile of an image hosted in Docker Hub without pulling the image locally.
For example, consider this image:
https://hub.docker.com/r/kodekloud/throw-dice
How can I see its Dockerfile without pulling its image locally?
On Docker Hub, if you click on "Tags" and then a specific tag (for this particular image, only "latest" exists) then you'll get an automated dump of the layer metadata, which is sort of like a Dockerfile. Some details, like the original file names from the build context, are lost.
In general you can only approximately reconstruct the Dockerfile given an image. I've had reasonably good luck finding corresponding GitHub repositories, and a Dockerfile is usually in the repository root or in a directory with an obvious name like docker.

Dockerfile FROM command - Does it always download from Docker Hub?

I just started working with docker this week and came across a 'dockerfile'. I was reading up on what this file does, and the official documentation basically mentions that the FROM keyword is needed to build a "base image". These base images are pulled from Docker hub, or downloaded from there.
Silly question - Are base images always pulled from docker hub?
If so and if I understand correctly I am assuming that running the dockerfile to create an image is not done very often (only when needing to create an image) and once the image is created then the image is whats run all the time?
So the dockerfile then can be migrated to which ever enviroment and things can be set up all over again quickly?
Pardon the silly question I am just trying to understand the over all flow and how dockerfile fits into things.
If the local (on your host) Docker daemon (already) has a copy of the container image (i.e. it's been docker pull'd) specified by FROM in a Dockerfile then it's cached and won't be repulled.
Container images include a tag (be wary of ever using latest) and the image name e.g. foo combined with the tag (which defaults to latest if not specified) is the full name of the image that's checked i.e. if you have foo:v0.0.1 locally and FROM:v0.0.1 then the local copy is used but FROM foo:v0.0.2 will pull foo:v0.0.2.
There's an implicit docker.io prefix i.e. docker.io/foo:v0.0.1 that references the Docker registry that's being used.
You could repeatedly docker build container images on the machines where the container is run but this is inefficient and the more common mechanism is that, once a container image is built, it is pushed to a registry (e.g. DockerHub) and then pulled from there by whatever machines need it.
There are many container registries: DockerHub, Google Artifact Registry, Quay etc.
There are tools other than docker that can be used to interact with containers e.g. (Red Hat's) Podman.

docker pull wouldn't pull latest image from remote

I ran this:
docker pull 91xxxxx371.dkr.ecr.us-west-2.amazonaws.com/main_api
and nothing new was pulled, but I knew there were new images on AWS/ECR.
So I removed the existing images:
docker rmi 91xxxxx371.dkr.ecr.us-west-2.amazonaws.com/main_api
and then pulled again and of course it says it retrieved new images, but that's probably just because I deleted the local tags/images or whatever.
Why didn't the first pull command get the latest? It defaults to the latest tag.
Update:
I have to correct my answer, #David Maze (comment) is right: I described the docker run behaviour.
From the Docker documentation:
When using tags, you can docker pull an image again to make sure you have the most up-to-date version of that image
So your command should work, I don't know why it's not working, sorry.
But nevertheless you can use as a workaround tags to enforce to pull the image with the specified tag.
docker run (not docker pull) search first in your local registry on your machine. If there is the image with the tag latest, the search is satisfied and terminated. If the image with the given tag is not available in your local registry, then docker will search in a remote registry like docker hub or your own.
So the tag latest should be used with care. If you have an Image with the tag latest in your local registry then you have to delete it first, so docker get nothing and search in remote registry.

Is there any way to pull an image from private registry and cut URL?

I have some private Docker registry: http://some-registry-somewhere.com:5000.
When I need to run my compose configuration, I need to pull a target image.
$ docker pull some-registry-somewhere.com:5000/target/image:tag1
In docker-compose.yml file, I have to set the same full URL-path because there is pulled image some-registry-somewhere.com:5000/target/image:tag1.
To have an image with image name only we may tag it:
$ docker tag some-registry-somewhere.com:5000/target/image:tag1 target/image:tag1
But is there any way to automatically cut Docker registry URL through Docker?
There is no such way, because of API specification. The image name is not just the tag, it also identifies for docker engine, which registry should be used for pushes and pulls of this image.
While the first some-registry-somewhere.com:5000/target/image:tag1 is image target/image:tag1 which is located in some-registry-somewhere.com:5000.
The second one target/image:tag1 is, in other words, image, docker.io/target/image:tag1, which is located in official repository.
In fact, they can be different in most of the cases.
The one way, which is not good, actually, because can be confusing (see again about repositories), is to use &&:
docker pull some-registry-somewhere.com:5000/target/image:tag1 && docker tag some-registry-somewhere.com:5000/target/image:tag1 target/image:tag1

What are the different ways of implementing Docker FROM?

Inheritance of an image is normally done using docker's from command, for example.
from centos7:centos7
In my case, I have a Dockerfile which I want to use as a base image builder, and I have two sub dockerfiles which customize that file.
I do not want to commit the original dockerfile as a container to dockerhub, so for example, I would like to do:
Dockerfile
slave/Dockerfile
master/Dockerfile
Where slave/Dockerfile looks something like this:
from ../Dockerfile
Is this (or anything similar) possible ? Or do I have to actually convert the top level Dockerfile to a container, and commit it as a dockerhub image, before I can leverage it using the docker FROM directive.
You don't have to push your images to dockerhub to be able to use them as base images. But you need to build them locally so that they are stored in your local docker repository. You can not use an image as a base image based on a relative path such as ../Dockerfile- you must base your own images on other image files that exists in your local repository.
Let's say that your base image uses the following (in the Dockerfile):
FROM centos7:centos7
// More stuff...
And when you build it you use the following:
docker build -t my/base .
What happens here is that the image centos7:centos7 is downloaded from dockerhub. Then your image my/base is built and stored (without versioning) in your local repository. You can also provide versioning to your docker container by simply providing version information like this:
docker build -t my/base:2.0 .
When an image has been built as in the example above it can then be used as a FROM-image to build other sub-images, at least on the same machine (the same local repository). So, in your sub-image you can use the following:
FROM my/base
Basically you don't have to push anything anywhere. All images lives locally on your machine. However, if you attempt to build your sub-image without a previously built base image you get an error.
For more information about building and tagging, check out the docs:
docker build
docker tag
create your own image

Resources