Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command to find out the base image of a Docker image?

Tags:

docker

I am curious about what base image my image is based upon. There seems no easy and straightforward way to do that. So for example I want to know the base image of openjdk:11-jdk.

First of all, I am fetching the relevant image id from my local repository by doing docker images | grep openjdk, which gives me below output.

↳ docker images | grep openjdk
openjdk 11-jdk              612d4d483eee        8 days ago          606MB
openjdk <none>              3a40000f62f1        3 weeks ago         605MB
openjdk <none>              243e95d792e3        2 months ago        605MB

The relevant image here is 612d4d483eee, so I want to see the history of that image by doing docker history 612d4d483eee which gives me below output (I left out the --no-trunc option because it messes up the formatting).

↳ docker history 612d4d483eee
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
612d4d483eee        8 days ago          /bin/sh -c #(nop)  CMD ["jshell"]               0B                  
<missing>           8 days ago          /bin/sh -c set -eux;   dpkgArch="$(dpkg --pr…   322MB               
<missing>           8 days ago          /bin/sh -c #(nop)  ENV JAVA_URL_VERSION=11.0…   0B                  
<missing>           8 days ago          /bin/sh -c #(nop)  ENV JAVA_BASE_URL=https:/…   0B                  
<missing>           8 days ago          /bin/sh -c #(nop)  ENV JAVA_VERSION=11.0.6      0B                  
<missing>           3 weeks ago         /bin/sh -c { echo '#/bin/sh'; echo 'echo "$J…   27B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV PATH=/usr/local/openj…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV JAVA_HOME=/usr/local/…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV LANG=C.UTF-8             0B                  
<missing>           3 weeks ago         /bin/sh -c set -eux;  apt-get update;  apt-g…   9.68MB              
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install…   142MB               
<missing>           3 weeks ago         /bin/sh -c set -ex;  if ! command -v gpg > /…   7.81MB              
<missing>           3 weeks ago         /bin/sh -c apt-get update && apt-get install…   23.3MB              
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:8f7dc710e276f54a3…   101MB 

I can see the layers of the image, but I see no base image.

What I usually do is going to the Docker hub page entry (https://hub.docker.com/_/openjdk) and see if there is a repository mentioned anywhere.

Luckily in this case there is a Github repository which is https://github.com/docker-library/openjdk. So I go to that repository and navigate to the Dockerfile of my specific image (https://github.com/docker-library/openjdk/blob/master/11/jdk/Dockerfile). On top of that file, I see the following line:

FROM buildpack-deps:stretch-scm

So the question is why is buildpack-deps:stretch-scm not visible anywhere in my command outputs? On top of that I want to know if there is an easier way of finding out the base image.

like image 521
Jack Sierkstra Avatar asked Jan 24 '20 08:01

Jack Sierkstra


1 Answers

For pulled images, I don't think there is a way to find the base image without seeing the actual dockerfile because when you pull an image, image manifest is downloaded only for the leaf layer. So the image id of the non-leaf layers in marked as <missing> in docker history and you wouldn't know the repo tags of those layers.


If the image is built on your machine but if you don't have the dockerfile, you can find the base image as follows:

docker history prints the image ids of the layers. Then you can get the repo tags using docker inspect. The base image used will usually be the last layer in the output of docker history.

eg:

$ docker history t:1
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
10b4cce00fb8        3 days ago          /bin/sh -c #(nop)  CMD ["flask" "run"]          0B
824987ef6cab        3 days ago          /bin/sh -c #(nop) COPY dir:1973b65388e92428e…   406B
d4b6f433a5df        3 days ago          /bin/sh -c pip install -r requirements.txt      4.98MB
8827b3f01d00        3 days ago          /bin/sh -c #(nop) COPY file:98271fcaff00c6ef…   0B
65b8c98138e6        2 weeks ago         /bin/sh -c apk add --no-cache gcc musl-dev l…   113MB
01589531f46d        2 weeks ago         /bin/sh -c #(nop)  ENV FLASK_RUN_HOST=0.0.0.0   0B
6c4640b8027a        2 weeks ago         /bin/sh -c #(nop)  ENV FLASK_APP=app.py         0B
b4c8fc7f03d6        2 weeks ago         /bin/sh -c #(nop) WORKDIR /code                 0B
16a54299f91e        2 weeks ago         /bin/sh -c #(nop)  CMD ["python3"]              0B
$ docker inspect 16a54299f91e
[
    {
        "Id": "sha256:16a54299f91ef62cf19d7329645365fff3b7a3bff4dfcd8d62f46d0c9845b9c6",
        "RepoTags": [
            "python:3.7-alpine"   ---> Base image used in FROM instruction. 

Following the output of docker history in reverse order, you can approximately recreate the Dockerfile.


You can also use the chenzj/dfimage image which runs a script which does the same to reconstruct the dockerfile.

alias dfimage="docker run -v /var/run/docker.sock:/var/run/docker.sock --rm chenzj/dfimage"

$ dfimage 10b4cce00fb8
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN /bin/sh -c apk add --no-cache gcc musl-dev linux-headers
COPY file:98271fcaff00c6efb6e58bd09ca726c29947e0cfe7031a8d98878cc01561fbbf in requirements.txt
RUN /bin/sh -c pip install -r requirements.txt
COPY dir:1973b65388e92428e30f835a67ebc8c7b00ec648fbea0717af6d501af162186b in .
CMD ["flask" "run"]
bash-3.2$
like image 71
Shashank V Avatar answered Oct 26 '22 15:10

Shashank V