Examining how Docker builds an image

Understanding how Docker images are constructed will help you build efficient images. The image build command produces a lot of output, which tells you exactly what Docker does for each step of the build. Each instruction in the Dockerfile is executed as a separate step that produces a new image layer, and the final image will be a combined stack of all of the layers. The following code snippet is the output from building my image:

> docker image build --tag dockeronwindows/ch02-powershell-env:2e .

Sending build context to Docker daemon 4.608kB
Step 1/3 : FROM mcr.microsoft.com/windows/servercore:ltsc2019
---> 8b79386f6e3b
Step 2/3 : COPY scripts/print-env-details.ps1 C:\print-env.ps1
---> 5e9ed4527b3f
Step 3/3 : CMD ["powershell.exe", "C:\print-env.ps1"]
---> Running in c14c8aef5dc5
Removing intermediate container c14c8aef5dc5
---> 5f272fb2c190
Successfully built 5f272fb2c190
Successfully tagged dockeronwindows/ch02-powershell-env:2e

This is what happens when Docker builds the image:

  1. The FROM image already exists in my local cache, so Docker doesn't need to download it. The output is the ID of Microsoft's Windows Server Core image (starting 8b79).
  2. Docker copies the script file from the build context into a new image layer (ID 5e9e).
  3. Docker configures the command to execute when a container is run from the image. It creates a temporary container from the Step 2 image, configures the startup command, saves the container as a new image layer (ID 5f27), and deletes the intermediate container (ID c14c).

The final layer is tagged with the image name, but all the intermediate layers are also added to the local cache. This layered approach means Docker can be very efficient when it builds images and runs containers. The latest Windows Server Core image is over 4 GB uncompressed, but when you run multiple containers based on Windows Server Core, they will all use the same base image layers, so you don't end up with multiple copies of the 4 GB image.

You'll understand more about image layers and storage later in the chapter, but first I'll look at some more complex Dockerfiles which package .NET and .NET Core applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.215.96