how to get the layers of a docker image on local host - docker

Just started playing with docker on Ubuntu. I pulled a docker image as follows.
docker pull coreos/apache
Now I see there there three layers being pulled.
Using default tag: latest
latest: Pulling from coreos/apache
a3ed95caeb02: Pull complete
5e160ca0bb5a: Pull complete
1f92e2761bfd: Pull complete
Digest: sha256:9af520cee7bedcda564970ff790cdf2e72b6daccce8539f6b3c880ed7fc21091
Status: Downloaded newer image for coreos/apache:latest
From the above, I see that there are three layers for this image.
But now, lets say I close and reopen the terminal, how can I get the layers(same info above) of this image. What is the command?
When I do docker images I get this.
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0584b3d2cf6d 9 days ago 196.5 MB
hello-world latest c54a2cc56cbb 4 months ago 1.848 kB
coreos/apache latest 5a3024d885c8 2 years ago 294.4 MB
Now that I know the image id, how can I get the layers for that image id.
The tree option as in
docker images --tree
is not working anymore. It seems to be removed. If not one command how can I get that using a sequence of command at least.

If you have the busybox, here is how I would get it.
docker inspect busybox - would show long configuration in json format and below is the excerpt from it to get the layers.
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e88b3f82283bc59d5e0df427c824e9f95557e661fcb0ea15fb0fb6f97760f9d9"
]
}
You can simple filter it by below command using --format option of docker inspect which would give exactly what OP is looking for.
rao # ubuntu $ docker inspect --format '{{.RootFS.Layers}}' busybox
[sha256:e88b3f82283bc59d5e0df427c824e9f95557e661fcb0ea15fb0fb6f97760f9d9]

now that the image has been downloaded to your local host (in layers), even if you kill your terminal and log back in, the image will still exist on your host. it's downloaded in layers, but resides as an image on your localhost that you can now instantiate into a container using 'docker run'
if you want to pull the image down again and see all the layers being pulled, you can first erase your image (docker rmi ), then do your 'docker pull coreos/apache' again. HTH
edit: to show actual layers that went into an image, run:
docker history <imageid>

Related

`docker images --digests` with and without tag give different answers

I am trying to figure out how to determine the digest of an image on my local machine.
If I ask docker images to tell me about the digests for my image, it will happily tell me the correct answer:
rcv#mymachine:~$ docker images -a --digests docker.myregistry.net/myproject/myimage
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
docker.myregistry.net/myproject/myimage 8.0.0-rc9 sha256:56993a4bdf42a6337c1e9a32603c8758d9a67b42b525c18c6d500b8688f50c47 2198f19aede7 3 days ago 1.84GB
However, if I try asking specifically about the tag I'm interested in it will tell me that there's no digest.
rcv#mymachine:~$ docker images -a --digests docker.myregistry.net/myproject/myimage:8.0.0-rc9
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
docker.myregistry.net/myproject/myimage 8.0.0-rc9 <none> 2198f19aede7 3 days ago 1.84GB
I've been alternating between building this image locally, and just pulling it back down from the registry. Is it possible that docker images is confused, or am I missing some subtle difference between the above two commands?
Thanks!
That appears to be a bug in the docker CLI. This shouldn't have any impact on the underlying digest. If you know the image you want to query, it's probably easier to inspect that image for the digest:
$ docker image inspect nginx:latest --format '{{index .RepoDigests 0}}'
nginx#sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36

Pulling from repository

I just created an image :
docker build firstimage .
and tagged it as sohrabp72/firstimage and then pushed it into my repo on the Docker hub:
docker tag firstimage sohrabp72/firstimage
Then I removed the tagged image and the original image from my local machine:
docker rmi sohrabp72/firstimage
docker rmi firstimage
Now when I want to pull the image from the Docker hub repository, my local Docker does not pull it:
C:\Users\Sohrab> docker pull sohrabp72/firstimage
Using default tag: latest
latest: Pulling from sohrabp72/firstimage
cbdbe7a5bc2a: Already exists
9287919c3a0f: Already exists
43a47bbd54c9: Already exists
3c1bcea295c4: Already exists
53e2ab46e733: Already exists
3b08dc288a15: Already exists
e01ad7774a4c: Already exists
Digest: sha256:f16f1cfd9e777898511259e7ff512947c27b7e7bb4f4333dd27bd809bdc77995
Status: Downloaded newer image for sohrabp72/firstimage:latest
docker.io/sohrabp72/firstimage:latest
PS C:\Users\Sohrab>
docker logs show some lines and say already exist. and when I look at images on my local docker, that image is there:
C:\Users\Sohrab> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
getting-started latest bfc5750a86e5 11 hours ago 231MB
sohrabp72/firstimage latest 4e197282638a 22 hours ago 179MB
node 12-alpine d8b74300d554 7 days ago 89.6MB
docker/getting-started latest 1f32459ef038 3 months ago 26.8MB
Is there something like a cache for images we've pushed to Docker hub?
When docker prepares an image, it does step by step and each step creates an intermediate step with an ID. Docker by default removes the intermediate image and the last step provides a final image with an ID.
When you ran build command it actually created a final image with an ID and tagged the final image ID with your name 'firstimage'
When you tag second name, it added additional tag 'sohrabp72/firstimage'
When you ran docker rmi command on both tags, it removed tags only while image created still is there.
You can run
docker images -a
which will list image with <<"none">> in repository and name but final image created will be there with ID
you can
docker image prune -a
to clear all image and try docker pull command

Docker pull hello-world showing successful create but when using docker ps or docker ps -a not shoing images

I used docker ps/docker ps -a/docker ps -n 1 all not showing my first image.
But it after I using docker pull hello-world it saying it installed successfully
docker pull pulls an image (and all the layers that make it up) to your local machine, but doesn't run anything.
docker ps lists containers on your system.
Once you run that container (using docker run hello-world), you'll see it in dokcer ps.
To view the image you pulled, you could use docker images.
As you find from the previous answer docker pull will download the image (mostly from the docker hub) and when trying to pull next time, it finds the image already in your local machine. To see all the images you have locally, use docker image ls.

How to get a list of immutable identifier (digest) from a Docker image?

I have a Docker image in dockerhub and this has been built several times because I need to update the PHP version to the newest. I need to use a previous version of that image and I think the way to go is by using the immutable identifier aka digest.
Here is the documentation in how to pull a given image by it's digest but I can't find a way to get all the digest from that image.
If you double click on a given build you obtain certain information like a build code, for example: berpxpunhmqe7bqh6lce5ub but I don't think that is such digest.
How do I find that digest for a given build?
Assuming you have a tag/identifier for the previous version and/or have a version in you local image cache, finding the digest to use with pull by digest can be done with a docker image inspect as follows:
$ docker image inspect --format "{{.RepoDigests}}" alpine:3.6
[alpine#sha256:b40e202395eaec699f2d0c5e01e6d6cb8e6b57d77c0e0221600cf0b5940cf3ab]
In this example I'm looking at the 3.6 tag of the alpine image, and the response is a string I can use with commands like docker pull:
$ docker pull alpine#sha256:b40e202395eaec699f2d0c5e01e6d6cb8e6b57d77c0e0221600cf0b5940cf3ab
sha256:b40e202395eaec699f2d0c5e01e6d6cb8e6b57d77c0e0221600cf0b5940cf3ab: Pulling from library/alpine
Digest: sha256:b40e202395eaec699f2d0c5e01e6d6cb8e6b57d77c0e0221600cf0b5940cf3ab
Status: Image is up to date for alpine#sha256:b40e202395eaec699f2d0c5e01e6d6cb8e6b57d77c0e0221600cf0b5940cf3ab
The potential problem with your specific image is that it looks like the latest tag has been used for all your builds, so unless you have a local cache of an older image, it may be quite difficult to find the older sha256 digest references to prior builds.
There are a few possible ways to find the digest of a prior image if local cached information hasn't been deleted via docker system prune or other cleanup utilities:
docker images -a | grep <image name> can be used to display all images, including those which have been untagged. A below example shows an updated ubuntu:latest where I still have access to the older image. Using that ID (which is not a digest), I can use the same docker image inspect --format '{{.RepoDigests}}' <image ID> to retrieve the actual digest of an older "build" of ubuntu.
If I had a container that is running or exited using a prior version of the image, I could find the digest of that image by first inspecting the container and finding the image ID, and then inspecting that image ID as above and retrieving the older image's digest. In this somewhat contrived example I have an exited container, 1edd.., which I inspect to find the image ID, which happens to still be validly tagged, but using it's id I can then use image inspect to get the digest, even if it is no longer tagged in my image cache.
Example 1:
$ docker images -a | grep ubuntu
ubuntu latest 747cb2d60bbe 3 weeks ago 122MB`
ubuntu <none> ebcd9d4fca80 5 months ago 118MB
$ docker image inspect --format '{{.RepoDigests}}' ebcd9
[ubuntu#sha256:382452f82a8bbd34443b2c727650af46aced0f94a44463c62a9848133ecb1aa8]
Example 2:
$ docker ps -aq
1edd14b528db
$ docker container inspect 1edd | grep Image
"Image": "sha256:76da55c8019d7a47c347c0dceb7a6591144d232a7dd616242a367b8bed18ecbc",
"Image": "alpine:3.6",
$ docker image inspect --format '{{.RepoDigests}}' 76da55
[alpine#sha256:f006ecbb824d87947d0b51ab8488634bf69fe4094959d935c0c103f4820a417d]

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