title: Is there a way to determine all Docker base layers from a given Dockerfile or image?
body: <p>I'm tasked with &quot;auditing&quot; Docker images: so that a user can run a <strike>script</strike> command to determine versions of all contributing Docker images.</p>
  <p>This is probably a <em>very</em> contrived example, but suppose a Dockerfile was:</p>
  <pre><code># Dockerfile
  FROM python:3 as python
  FROM gcc:12.1.0 as gcc
  
  FROM alpine:3.7 as base
  COPY --from=python /usr/local/bin/python /usr/local/bin/python
  COPY --from=gcc /usr/local/bin/gcc /usr/local/bin/gcc
  </code></pre>
  <p>Is there some way to &quot;audit&quot;/inspect/parse either the Dockerfile or its resulting image, and get some kind of structured output (JSON would be nice) that would report, effectively, something along the lines of:</p>
  <pre><code>python:3
  gcc:12.1.0
  alpine:3.7
  </code></pre>
  <p>?</p>
  <p>There's probably some clever homebrewed string-parsing solution, but for this question, I'd specifically like to learn if there's an off-the-shelf, or &quot;standard&quot; way to do this (i.e. preferably not a self-written script, unless that is standard practice).</p>
  <p>Is there, perhaps, a native Docker command that can get this information?</p>
  <p>Naively, the only thing I could think of was <code>docker inspect ${image_name}</code>, but that did not reveal the noted desired information:</p>
  <pre><code>$ docker inspect tmp:tmp
  [
      {
          &quot;Id&quot;: &quot;sha256:82c6b7eae3e8a982579aedc3787751d71036578fe8b45340268b9fe909479809&quot;,
          &quot;RepoTags&quot;: [
              &quot;tmp:tmp&quot;
          ],
          &quot;RepoDigests&quot;: [],
          &quot;Parent&quot;: &quot;&quot;,
          &quot;Comment&quot;: &quot;buildkit.dockerfile.v0&quot;,
          &quot;Created&quot;: &quot;2022-05-16T09:00:52.810796312-07:00&quot;,
          &quot;Container&quot;: &quot;&quot;,
          &quot;ContainerConfig&quot;: {
              &quot;Hostname&quot;: &quot;&quot;,
              &quot;Domainname&quot;: &quot;&quot;,
              &quot;User&quot;: &quot;&quot;,
              &quot;AttachStdin&quot;: false,
              &quot;AttachStdout&quot;: false,
              &quot;AttachStderr&quot;: false,
              &quot;Tty&quot;: false,
              &quot;OpenStdin&quot;: false,
              &quot;StdinOnce&quot;: false,
              &quot;Env&quot;: null,
              &quot;Cmd&quot;: null,
              &quot;Image&quot;: &quot;&quot;,
              &quot;Volumes&quot;: null,
              &quot;WorkingDir&quot;: &quot;&quot;,
              &quot;Entrypoint&quot;: null,
              &quot;OnBuild&quot;: null,
              &quot;Labels&quot;: null
          },
          &quot;DockerVersion&quot;: &quot;&quot;,
          &quot;Author&quot;: &quot;&quot;,
          &quot;Config&quot;: {
              &quot;Hostname&quot;: &quot;&quot;,
              &quot;Domainname&quot;: &quot;&quot;,
              &quot;User&quot;: &quot;&quot;,
              &quot;AttachStdin&quot;: false,
              &quot;AttachStdout&quot;: false,
              &quot;AttachStderr&quot;: false,
              &quot;Tty&quot;: false,
              &quot;OpenStdin&quot;: false,
              &quot;StdinOnce&quot;: false,
              &quot;Env&quot;: [
                  &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;
              ],
              &quot;Cmd&quot;: [
                  &quot;/bin/sh&quot;
              ],
              &quot;ArgsEscaped&quot;: true,
              &quot;Image&quot;: &quot;&quot;,
              &quot;Volumes&quot;: null,
              &quot;WorkingDir&quot;: &quot;&quot;,
              &quot;Entrypoint&quot;: null,
              &quot;OnBuild&quot;: null,
              &quot;Labels&quot;: null
          },
          &quot;Architecture&quot;: &quot;amd64&quot;,
          &quot;Os&quot;: &quot;linux&quot;,
          &quot;Size&quot;: 5506254,
          &quot;VirtualSize&quot;: 5506254,
          &quot;GraphDriver&quot;: {
              &quot;Data&quot;: {
                  &quot;LowerDir&quot;: &quot;/var/lib/docker/overlay2/4geqlfntwc8msdnxvvfnw56hk/diff:/var/lib/docker/overlay2/6c0605313c6032543bb3c24031ae2b49b28b92c989cf8bda6ba6faaf0ed49504/diff&quot;,
                  &quot;MergedDir&quot;: &quot;/var/lib/docker/overlay2/kyyejpagoiaf3ugvsdsrj0k1o/merged&quot;,
                  &quot;UpperDir&quot;: &quot;/var/lib/docker/overlay2/kyyejpagoiaf3ugvsdsrj0k1o/diff&quot;,
                  &quot;WorkDir&quot;: &quot;/var/lib/docker/overlay2/kyyejpagoiaf3ugvsdsrj0k1o/work&quot;
              },
              &quot;Name&quot;: &quot;overlay2&quot;
          },
          &quot;RootFS&quot;: {
              &quot;Type&quot;: &quot;layers&quot;,
              &quot;Layers&quot;: [
                  &quot;sha256:3fc64803ca2de7279269048fe2b8b3c73d4536448c87c32375b2639ac168a48b&quot;,
                  &quot;sha256:7d03c98637d73bbaf877bdde459bbb8d223e95e6f1f127f8cf49c1c1d6841a04&quot;,
                  &quot;sha256:58d81723e6de774a51e8327362abe1f49d6be7a60d53c3b02f9c390da1e413dd&quot;
              ]
          },
          &quot;Metadata&quot;: {
              &quot;LastTagTime&quot;: &quot;2022-05-16T09:00:52.927739055-07:00&quot;
          }
      }
  ]
  </code></pre>