- Docker on Windows
- Elton Stoneman
- 368字
- 2021-07-02 19:53:19
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 the combined stack of all the layers. This is the output from building my image:
> docker image build --tag dockeronwindows/ch02-powershell-env .
Sending build context to Docker daemon 3.584kB
Step 1/3 : FROM microsoft/nanoserver
---> d9bccb9d4cac
Step 2/3 : COPY scripts/print-env-details.ps1 c:\\print-env.ps1
---> a44026142eaa
Removing intermediate container 9901221bbf99
Step 3/3 : CMD powershell.exe c:\print-env.ps1
---> Running in 56af93a47ab1
---> 253feb55a9c0
Removing intermediate container 56af93a47ab1
Successfully built 253feb55a9c0
Successfully tagged dockeronwindows/ch02-powershell-env:latest
This is what's happening in these execution steps:
- Step 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 Nano Server image (starting d9b).
- Step 2: Docker creates a temporary, intermediate container from the base image and copies the script file from the build context into the container. Then it saves the container as a new image layer (ID a44) and removes the intermediate container (ID 990).
- Step 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 253), and deletes the intermediate container (ID 56a).
The final layer is tagged with the image name, but all the intermediate layers are also added to the local cache. The layered approach means Docker can be very efficient when it builds images and runs containers. The latest Windows Nano Server image is over 900 MB uncompressed, but when you run multiple containers based from Nano Server they will all use the same base image layers, you don't end up with multiple copies of the 900 MB image.
You'll understand more about image layers and storage later in the chapter, but first I'll look at some more complex Dockerfiles that package .NET and .NET Core applications.