Summary:
In this article we will learn about Container Image (Layer Reuse)
Container Images:
Definition: A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings
- Container Images consist of Application (Eg: IIS, ASP.net, python, NodeJS), and Base OS application & Library (Eg: Cmd, PowerShell).
- Container Image becomes a Container when they run on Docker Engine. Moreover, we can also run native OS Apps (Eg: CMD and Powershell) in Containers
Container Images layers are reused.
Eg: Windows Server Core or Window Nano Server OS layers may be common to multiple images. These common layers are reused when we run Container using such Container Images (Eg: IIS or ASP.Net Container imaged has Windows Server Core/Nano Server as Base Image)
These Layers are Read-Only, any modification to these layers are saved in R/W layer
- Container image delivers User Space (OS Apps & Libraries) along with File System and Windows Registry
- File System and Window Registry in Windows Container is isolated from File System and Registry on Container Host
Example: How the Container Images are layered and How these layers are reused
Lets consider 3 Docker Images:
Base OS Images: mcr.microsoft.com/windows/nanoserver:1809-amd64
NodeJS Image: ‘stefanscherer/node–windows’, with nanoserver:1809-amd64 as base OS
Visualizer Image: ‘virlualizer’ with NodeJS, ‘stefanscherer/node-windows’ as base image
- List available Docker Images
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
virlualizer latest e7d708a97f38 2 weeks ago 462MB
stefanscherer/node-windows latest 46b86f7ea7e6 3 months ago 352MB
mcr.microsoft.com/windows/nanoserver 1809-amd64 9e7d556b2b51 5 weeks ago 251MB
- Dump Container Images to file.
docker save stefanscherer/node-windows -o nodejs.tar
docker save mcr.microsoft.com/windows/nanoserver:1809-amd64 -o iis.tar
docker save virlualizer-o visualizer.tar
Image: mcr.microsoft.com/windows/nanoserver:1809-amd64
Layers:
Contents of Layer: 04905cddc67c…….825394f516b3e0aaeac
Content of the Windows Nano Server is identical to the File Structure of C:\ on any Windows Operating System:
Node JS Images: stefanscherer/node-windows
Layers:
In NodeJS images, Layer ‘c21555943ef4…….1b8f09b1683467c‘ contains ‘nanoserver:1809-amd64’ files
Nano Server files in Layer ‘c21555943ef4…….1b8f09b1683467c’ is common in both Container Images
Layer: ‘ec17de076a2d28b…….644c74c31706c7‘, contains NodeJS application files
Visualizer Image: visualizer
Layers:
Visualizer Images Layer: ‘ec17de076a2d…….644c74c31706c7‘ contains NodeJs application files
NodeJS application layer, ‘ec17de076a2d……644c74c31706c7‘ is common in both Container Images
Layer: ‘c21555943ef44de6…..b8f09b1683467c‘ contains ‘nanoserver:1809-amd64’ files
Nano Server files in Layer ‘c21555943ef44de6…..b8f09b1683467c‘ is common in both Container Images
While pulling Container Image: stefanscherer/node-windows, Docker Engine identified base images in ‘stefanscherer/node-windows’ dockerfile is “mcr.microsoft.com/windows/nanoserver:1809“
Sample Dockerfile for Container Image: stefanscherer/node-windows
ARG core=mcr.microsoft.com/windows/servercore:ltsc2019
ARG target=mcr.microsoft.com/windows/nanoserver:1809
FROM $core as download
As we already have Container Image: mcr.microsoft.com/windows/nanoserver:1809-amd64 on container Host, Docker will not download these layers again. Docker will reuse layers of container images, ‘mcr.microsoft.com/windows/nanoserver:1809-amd64’ for container image, ‘stefanscherer/node-windows’
docker pull stefanscherer/node-windows
PS> docker pull stefanscherer/node-windows
Using default tag: latest
latest: Pulling from stefanscherer/node-windows
a35da61c3562: Already exists
ca3ce7e0505e: Already exists
467304420746: Already exists
82261936af73: Already exists
1fc8e88fbc38: Already exists
ce988feaff29: Already exists
e00edaa026f7: Already exists
7492409d885c: Already exists
89b90cd7db71: Already exists
8282e2008c9d: Already exists
Digest: sha256:34adda0e10fdb1ed591b3864616308fde86095ea7caabef5623b65203c0891f8
Status: Downloaded newer image for stefanscherer/node-windows:latest
docker.io/stefanscherer/node-windows:latest
In the dockerfile for Container Image: ‘visualizer’, we are using ‘stefanscherer/node-windows’ as base container images is
Sample: Dockerfile for visualizer
FROM stefanscherer/node-windows:latest
WORKDIR /app
As we already have Container Image: ‘stefanscherer/node-windows:latest’, on container Host, Docker will not download these layers again. Docker will reuse layers of container images, ‘stefanscherer/node-windows:latest’, for container image, ‘visulaizer’
docker build -t visualizer:latest .
PS> docker build -t visualizer:latest .
Sending build context to Docker daemon 683kB
Step 1/10 : FROM stefanscherer/node-windows:latest
---> 46b86f7ea7e6
Step 2/10 : WORKDIR /app
---> Running in 4ffe52d49883
Removing intermediate container 4ffe52d49883
---> bb04e29b5d9e
Step 3/10 : ADD ./package.json /app/package.json
---> 94f8c7c643df
.
.
.
.
.
Successfully built 6c3630785aa2
Successfully tagged visualizer:latest
Moreover, we can use the ‘docker history <Container_Image>:<Tag>‘ to list all layers in the Container Image:
Docker History:
Below Command list all the Layers in the Container Images:
Container Image: mcr.microsoft.com/windows/nanoserver:1809-amd64
PS C:\> docker history mcr.microsoft.com/windows/nanoserver:1809-amd64
IMAGE CREATED CREATED BY SIZE COMMENT
9e7d556b2b51 2 weeks ago Apply image 1809-amd64 251MB
Container images: stefanscherer/node-windows
Container Images, ‘stefanscherer/node-windows’ has layers ‘1809-amd64‘ from Container image, ‘mcr.microsoft.com/windows/nanoserver:1809-amd64’
Container Images, ‘stefanscherer/node-windows’ will reuse the layer: 9e7d556b2b51
docker history stefanscherer/node-windows
IMAGE CREATED CREATED BY SIZE COMMENT
46b86f7ea7e6 2 months ago cmd /S /C #(nop) CMD [“node.exe”] 41kB
<missing> 2 months ago cmd /S /C #(nop) USER ContainerUser 41kB
<missing> 2 months ago |1 SETX=/M cmd /S /C setx %SETX% PATH “%PATH… 1.03MB
<missing> 2 months ago cmd /S /C #(nop) USER ContainerAdministrator 41kB
<missing> 2 months ago cmd /S /C #(nop) ARG SETX=/M 41kB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:a51d59e1d654cfdd9d… 41.7MB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:f27d374e0a835e5155… 5.36MB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:3c01c8eb1521cfdaa3… 52.1MB
<missing> 2 months ago cmd /S /C #(nop) ENV NPM_CONFIG_LOGLEVEL=in… 41kB
<missing> 3 months ago Apply image 1809-amd64 251MB
—-
Container images: virlualizer
Container Images, ‘virlualizer’ has following common layers from Container Images: ‘mcr.microsoft.com/windows/nanoserver:1809-amd64’ and ‘stefanscherer/node-windows’
Common NodeJS Layer: 46b86f7ea7e6
docker history virlualizer
IMAGE CREATED CREATED BY SIZE COMMENT
e7d708a97f38 24 hours ago cmd /S /C #(nop) CMD [“npm.cmd” “start”] 41kB
898a4b00de77 24 hours ago cmd /S /C #(nop) HEALTHCHECK &{[“CMD-SHELL”… 41kB
768312f72bd3 24 hours ago cmd /S /C #(nop) EXPOSE 8080 41kB
db93300a2353 24 hours ago cmd /S /C #(nop) ENV MS=200 41kB
6a6700e2d15a 24 hours ago cmd /S /C npm run dist 4.47MB
0fab18912ccb 24 hours ago cmd /S /C #(nop) ADD dir:9a830392bc6b8cbde69… 664kB
e5f5a5b85f73 24 hours ago cmd /S /C npm install –unsafe-perm=true 105MB
12fb64cf413d 24 hours ago cmd /S /C #(nop) ADD file:fab0914b52f929c1d6… 42.8kB
709d2fe4aab7 24 hours ago cmd /S /C #(nop) WORKDIR C:\app 41kB
46b86f7ea7e6 2 months ago cmd /S /C #(nop) CMD [“node.exe”] 41kB
<missing> 2 months ago cmd /S /C #(nop) USER ContainerUser 41kB
<missing> 2 months ago |1 SETX=/M cmd /S /C setx %SETX% PATH “%PATH… 1.03MB
<missing> 2 months ago cmd /S /C #(nop) USER ContainerAdministrator 41kB
<missing> 2 months ago cmd /S /C #(nop) ARG SETX=/M 41kB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:a51d59e1d654cfdd9d… 41.7MB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:f27d374e0a835e5155… 5.36MB
<missing> 2 months ago cmd /S /C #(nop) COPY dir:3c01c8eb1521cfdaa3… 52.1MB
<missing> 2 months ago cmd /S /C #(nop) ENV NPM_CONFIG_LOGLEVEL=in… 41kB
<missing> 3 months ago Apply image 1809-amd64 251MB