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

Compiling with multi-stage builds

In a multi-stage build, you have multiple FROM instructions in your Dockerfile, where each FROM instruction starts a new stage in the build. Docker executes all the instructions when you build the image, and later stages can access the output from earlier stages, but only the final stage is used for the completed image.

I can write a multi-stage Dockerfile for the .NET Core console app by combining the previous two Dockerfiles into one:

# build stage
FROM microsoft/dotnet:1.1-sdk-nanoserver AS builder
WORKDIR /src
COPY src/ .
RUN dotnet restore; dotnet publish

# final image stage
FROM microsoft/dotnet:1.1-runtime-nanoserver
WORKDIR /dotnetapp
COPY --from=builder /src/bin/Debug/netcoreapp1.1/publish .
CMD ["dotnet", "HelloWorld.NetCore.dll"]

There are a couple of things that are new here. The first stage uses the large base image, with the .NET Core SDK installed. I've named that stage builder, using the AS option in the FROM instruction. The rest of that stage goes on to copy in the source code and publish the application. When the builder stage completes, the published application will be stored in an intermediate container.

The second stage uses the runtime .NET Core image, which doesn't have the SDK installed. In that stage I copy the published output from the previous stage, specifying --from=builder in the COPY instruction. Anyone can compile this application from source, without needing .NET Core installed on their machine.

Multi-stage Dockerfiles for Windows apps are completely portable. To compile the app and build the image, the only pre-requisite is to have a Windows machine with Docker installed, and a copy of the code. The builder stage contains the SDK and all the compiler tools, but the final image just has the minimum needed to run the application.

This approach isn't just for .NET Core. You can write a multi-stage Dockerfile for a .NET Framework app, where the first stage uses an image with MSBuild installed, which you use to compile your application. There are plenty of examples of that later in this book.

Whichever approach you take, there are just a few more Dockerfile instructions you need to understand in order to build more complex application images, which can integrate with other systems.

主站蜘蛛池模板: 阳谷县| 子洲县| 全州县| 漳州市| 平顺县| 泉州市| 贵南县| 陆河县| 瓮安县| 都昌县| 新宁县| 萨嘎县| 徐闻县| 江安县| 淳化县| 河南省| 安顺市| 凌源市| 望江县| 高密市| 渑池县| 瑞金市| 收藏| 贡觉县| 桐乡市| 兴文县| 逊克县| 南华县| 张掖市| 辛集市| 镇赉县| 绍兴县| 沙坪坝区| 钟山县| 兴国县| 松阳县| 顺平县| 潜山县| 宜黄县| 锦屏县| 保康县|