- Docker on Windows
- Elton Stoneman
- 369字
- 2021-07-02 12:47:56
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:2.2-sdk-nanoserver-1809 AS builder
WORKDIR /src
COPY src/ .
USER ContainerAdministrator
RUN dotnet restore && dotnet publish
# final image stage
FROM microsoft/dotnet:2.2-runtime-nanoserver-1809
WORKDIR /dotnetapp
COPY --from=builder /src/bin/Debug/netcoreapp2.2/publish .
CMD ["dotnet", "HelloWorld.NetCore.dll"]
There are a couple of things that are new here. The first stage uses a large base image, with the .NET Core SDK installed. I've named this stage builder, using the AS option in the FROM instruction. The rest of the 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 this stage I copy the published output from the previous stage, specifying --from=builder in the COPY instruction. Anyone can compile this application from the source using Docker, 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 prerequisite 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 this later in the book.
Whichever approach you take, there are just a few more Dockerfile instructions that you need to understand in order to build more complex application images, for software which integrates with other systems.
- PLC控制程序精編108例
- Linux Mint Essentials
- Google系統架構解密:構建安全可靠的系統
- 精通Linux內核開發
- Windows Phone 7.5 Data Cookbook
- Linux自動化運維:Shell與Ansible(微課版)
- Windows 7中文版從入門到精通(修訂版)
- Ceph分布式存儲實戰
- Delphi Programming Projects
- Linux基礎使用與案例
- 分布式高可用架構之道
- Linux系統最佳實踐工具:命令行技術
- Docker容器技術與應用
- Learn Quantum Computing with Python and IBM Quantum Experience
- Azure Serverless Computing Cookbook