官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 漳浦县| 江津市| 夏河县| 留坝县| 鄂托克前旗| 左云县| 大田县| 句容市| 公主岭市| 封丘县| 南乐县| 武陟县| 肇源县| 黄梅县| 灵台县| 承德县| 万州区| 伊金霍洛旗| 南木林县| 中牟县| 武定县| 沁源县| 沂源县| 桓台县| 平和县| 常德市| 大渡口区| 张家港市| 黎川县| 铜山县| 翁牛特旗| 沁阳市| 友谊县| 巴彦县| 长寿区| 繁峙县| 安陆市| 巴林右旗| 广安市| 阳曲县| 泗阳县|