Understanding layers

Intermediate Docker

Mike Metzger

Data Engineering Consultant

Docker layers

  • Docker images are made up of layers
  • A layer generally references a change or command within a Dockerfile
  • Layers can be cached / reused
  • The order of commands within a Dockerfile can affect whether layers are reused

Dockerfile to Layers

Intermediate Docker

Why do we care about layers?

  • Reusability
    • Faster build time
    • Smaller builds
Intermediate Docker

docker image inspect

  • How to determine the layers within an image?
  • docker image inspect <img id | name>
  • Provides much information about the content of a Docker image
  • The RootFS:Layers section provides details about layers in a given Docker image
repl@host:~$ docker image inspect alpine
[
    {        "Id": "sha256:05455a08881ea9cf0e752bc48e61bbd71a34c029bb13df01e40e3e70e0d007bd",
        "RepoTags": [
            "alpine:latest"
        ],
        "Created": "2024-01-27T00:30:48.743965523Z",
Intermediate Docker

docker image inspect example

bash> docker image inspect postgres:latest

"RootFS": {
   "Type": "layers",
   "Layers": [
"sha256:6f2d01c02c30cc1ffac781aff795cba8eeb29cc27756fe37bf525169856369c6",
"sha256:c6ad2d5a3cad837ae66b5560e9c577bfad062556b1f00791d8d733ce44a577ce",
"sha256:2153552a84ccbf7e4a28a50e766b72345072e59f8af0ff068baf98b413132e0c",
"sha256:6c00217b1e4b15c25eb3f6e28b1af8c295f469568014621e31a4c5eb5a8aca6f",
"sha256:167177d78e2a33aa822faebe9f01683c648ae78179059db05cd25737f215c305",
...
Intermediate Docker

jq command-line tool

  • Sometimes difficult to analyze the results from docker image inspect
  • jq commandline tool is used to read JSON data, like what's returned from docker image inspect
  • Can use jq to query data
Intermediate Docker

jq recipes with Docker

  • Method to see just a specific section, for example the RootFS data:
    • docker image inspect <id> | jq '.[0] | .RootFS'
      {
      "Type": "layers",
      "Layers": [ "sha256:0f5c115c5eea96...", 
                  "sha256:20792593831cdc..."
                ]
      }
      
Intermediate Docker

jq recipes with Docker (part 2)

  • Method to count number of layers using jq:
    • docker image inspect <id> | jq '.[0] | {LayerCount: .RootFS.Layers | length}'
      {
        "LayerCount": 2
      }
      
Intermediate Docker

Let's practice!

Intermediate Docker

Preparing Video For Download...