containerize-aspnet-framework
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseASP.NET .NET Framework Containerization Prompt
ASP.NET .NET Framework 容器化指引
Containerize the ASP.NET (.NET Framework) project specified in the containerization settings below, focusing exclusively on changes required for the application to run in a Windows Docker container. Containerization should consider all settings specified here.
REMEMBER: This is a .NET Framework application, not .NET Core. The containerization process will be different from that of a .NET Core application.
根据以下容器化设置中指定的内容,将ASP.NET (.NET Framework)项目容器化,仅需关注让应用在Windows Docker容器中运行所需的修改。容器化过程需考虑此处指定的所有设置。
注意: 这是一个.NET Framework应用,而非.NET Core应用。其容器化流程与.NET Core应用的容器化流程不同。
Containerization Settings
容器化设置
This section of the prompt contains the specific settings and configurations required for containerizing the ASP.NET (.NET Framework) application. Prior to running this prompt, ensure that the settings are filled out with the necessary information. Note that in many cases, only the first few settings are required. Later settings can be left as defaults if they do not apply to the project being containerized.
Any settings that are not specified will be set to default values. The default values are provided in .
[square brackets]本部分包含ASP.NET (.NET Framework)应用容器化所需的具体设置和配置。在执行本指引前,请确保已填写必要的信息。请注意,多数情况下仅需填写前几项设置,后续设置若不适用当前项目,可保留默认值。
未指定的设置将采用默认值,默认值标注在中。
[方括号]Basic Project Information
基础项目信息
-
Project to containerize:
[ProjectName (provide path to .csproj file)]
-
Windows Server SKU to use:
[Windows Server Core (Default) or Windows Server Full]
-
Windows Server version to use:
[2022, 2019, or 2016 (Default 2022)]
-
Custom base image for the build stage of the Docker image ("None" to use standard Microsoft base image):
[Specify base image to use for build stage (Default None)]
-
Custom base image for the run stage of the Docker image ("None" to use standard Microsoft base image):
[Specify base image to use for run stage (Default None)]
-
待容器化的项目:
[项目名称(提供.csproj文件路径)]
-
要使用的Windows Server SKU:
[Windows Server Core(默认)或Windows Server Full]
-
要使用的Windows Server版本:
[2022、2019或2016(默认2022)]
-
Docker镜像构建阶段的自定义基础镜像(使用“None”则采用标准Microsoft基础镜像):
[指定构建阶段使用的基础镜像(默认None)]
-
Docker镜像运行阶段的自定义基础镜像(使用“None”则采用标准Microsoft基础镜像):
[指定运行阶段使用的基础镜像(默认None)]
Container Configuration
容器配置
-
Ports that must be exposed in the container image:
- Primary HTTP port:
[e.g., 80] - Additional ports:
[List any additional ports, or "None"]
- Primary HTTP port:
-
User account the container should run as:
[User account, or default to "ContainerUser"]
-
IIS settings that must be configured in the container image:
[List any specific IIS settings, or "None"]
-
容器镜像中必须暴露的端口:
- 主HTTP端口:
[例如:80] - 额外端口:
[列出所有额外端口,或填写“None”]
- 主HTTP端口:
-
容器运行时使用的用户账户:
[用户账户,默认使用“ContainerUser”]
-
容器镜像中必须配置的IIS设置:
[列出具体的IIS设置,或填写“None”]
Build configuration
构建配置
-
Custom build steps that must be performed before building the container image:
[List any specific build steps, or "None"]
-
Custom build steps that must be performed after building the container image:
[List any specific build steps, or "None"]
-
构建容器镜像前必须执行的自定义构建步骤:
[列出具体的构建步骤,或填写“None”]
-
构建容器镜像后必须执行的自定义构建步骤:
[列出具体的构建步骤,或填写“None”]
Dependencies
依赖项
-
.NET assemblies that should be registered in the GAC in the container image:
[Assembly name and version, or "None"]
-
MSIs that must be copied to the container image and installed:
[MSI names and versions, or "None"]
-
COM components that must be registered in the container image:
[COM component names, or "None"]
-
需要在容器镜像的GAC中注册的.NET程序集:
[程序集名称和版本,或填写“None”]
-
必须复制到容器镜像并安装的MSI:
[MSI名称和版本,或填写“None”]
-
必须在容器镜像中注册的COM组件:
[COM组件名称,或填写“None”]
System Configuration
系统配置
-
Registry keys and values that must be added to the container image:
[Registry paths and values, or "None"]
-
Environment variables that must be set in the container image:
[Variable names and values, or "Use defaults"]
-
Windows Server roles and features that must be installed in the container image:
[Role/feature names, or "None"]
-
必须添加到容器镜像的注册表项和值:
[注册表路径和值,或填写“None”]
-
必须在容器镜像中设置的环境变量:
[变量名称和值,或填写“使用默认值”]
-
必须在容器镜像中安装的Windows Server角色和功能:
[角色/功能名称,或填写“None”]
File System
文件系统
-
Files/directories that need to be copied to the container image:
[Paths relative to project root, or "None"]- Target location in container:
[Container paths, or "Not applicable"]
-
Files/directories to exclude from containerization:
[Paths to exclude, or "None"]
-
需要复制到容器镜像的文件/目录:
[相对于项目根目录的路径,或填写“None”]- 容器中的目标位置:
[容器内路径,或填写“不适用”]
-
容器化过程中需要排除的文件/目录:
[要排除的路径,或填写“None”]
.dockerignore Configuration
.dockerignore配置
- Patterns to include in the file (.dockerignore will already have common defaults; these are additional patterns):
.dockerignore- Additional patterns:
[List any additional patterns, or "None"]
- Additional patterns:
- 需要添加到文件的模式(.dockerignore已包含常见默认模式,以下为额外模式):
.dockerignore- 额外模式:
[列出所有额外模式,或填写“None”]
- 额外模式:
Health Check Configuration
健康检查配置
-
Health check endpoint:
[Health check URL path, or "None"]
-
Health check interval and timeout:
[Interval and timeout values, or "Use defaults"]
-
健康检查端点:
[健康检查URL路径,或填写“None”]
-
健康检查间隔和超时时间:
[间隔和超时值,或填写“使用默认值”]
Additional Instructions
额外说明
-
Other instructions that must be followed to containerize the project:
[Specific requirements, or "None"]
-
Known issues to address:
[Describe any known issues, or "None"]
-
容器化项目必须遵循的其他说明:
[具体要求,或填写“None”]
-
需要解决的已知问题:
[描述已知问题,或填写“None”]
Scope
范围
- ✅ App configuration modification to ensure config builders are used to read app settings and connection strings from the environment variables
- ✅ Dockerfile creation and configuration for an ASP.NET application
- ✅ Specifying multiple stages in the Dockerfile to build/publish the application and copy the output to the final image
- ✅ Configuration of Windows container platform compatibility (Windows Server Core or Full)
- ✅ Proper handling of dependencies (GAC assemblies, MSIs, COM components)
- ❌ No infrastructure setup (assumed to be handled separately)
- ❌ No code changes beyond those required for containerization
- ✅ 修改应用配置,确保使用配置生成器从环境变量中读取应用设置和连接字符串
- ✅ 为ASP.NET应用创建并配置Dockerfile
- ✅ 在Dockerfile中指定多阶段构建/发布应用,并将输出复制到最终镜像
- ✅ 配置Windows容器平台兼容性(Windows Server Core或Full)
- ✅ 正确处理依赖项(GAC程序集、MSI、COM组件)
- ❌ 不涉及基础设施搭建(假设由其他环节处理)
- ❌ 除容器化所需的修改外,不进行其他代码变更
Execution Process
执行流程
- Review the containerization settings above to understand the containerization requirements
- Create a file to track changes with check marks
progress.md - Determine the .NET Framework version from the project's .csproj file by checking the element
TargetFrameworkVersion - Select the appropriate Windows Server container image based on:
- The .NET Framework version detected from the project
- The Windows Server SKU specified in containerization settings (Core or Full)
- The Windows Server version specified in containerization settings (2016, 2019, or 2022)
- Windows Server Core tags can be found at: https://github.com/microsoft/dotnet-framework-docker/blob/main/README.aspnet.md#full-tag-listing
- Ensure that required NuGet packages are installed. DO NOT install these if they are missing. If they are not installed, the user must install them manually. If they are not installed, pause executing this prompt and ask the user to install them using the Visual Studio NuGet Package Manager or Visual Studio package manager console. The following packages are required:
Microsoft.Configuration.ConfigurationBuilders.Environment
- Modify the file to add configuration builders section and settings to read app settings and connection strings from environment variables:
web.config- Add ConfigBuilders section in configSections
- Add configBuilders section in the root
- Configure EnvironmentConfigBuilder for both appSettings and connectionStrings
- Example pattern:
xml
<configSections> <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" /> </configSections> <configBuilders> <builders> <add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" /> </builders> </configBuilders> <appSettings configBuilders="Environment"> <!-- existing app settings --> </appSettings> <connectionStrings configBuilders="Environment"> <!-- existing connection strings --> </connectionStrings>
- Create a file in the folder where the Dockerfile will be created by copying the reference
LogMonitorConfig.jsonfile at the end of this prompt. The file's contents MUST NOT not be modified and should match the reference content exactly unless instructions in containerization settings specify otherwise.LogMonitorConfig.json- In particular, make sure the level of issues to be logged is not changed as using level for EventLog sources will cause unnecessary noise.
Information
- In particular, make sure the level of issues to be logged is not changed as using
- Create a Dockerfile in the root of the project directory to containerize the application
- The Dockerfile should use multiple stages:
- Build stage: Use a Windows Server Core image to build the application
- The build stage MUST use a base image unless a custom base image is specified in the settings file
mcr.microsoft.com/dotnet/framework/sdk - Copy sln, csproj, and packages.config files first
- Copy NuGet.config if one exists and configure any private feeds
- Restore NuGet packages
- Then, copy the rest of the source code and build and publish the application to C:\publish using MSBuild
- The build stage MUST use a
- Final stage: Use the selected Windows Server image to run the application
- The final stage MUST use a base image unless a custom base image is specified in the settings file
mcr.microsoft.com/dotnet/framework/aspnet - Copy the file to a directory in the container (e.g., C:\LogMonitor)
LogMonitorConfig.json - Download LogMonitor.exe from the Microsoft repository to the same directory
- The correct LogMonitor.exe URL is: https://github.com/microsoft/windows-container-tools/releases/download/v2.1.1/LogMonitor.exe
- Set the working directory to C:\inetpub\wwwroot
- Copy the published output from the build stage (in C:\publish) to the final image
- Set the container's entry point to run LogMonitor.exe with ServiceMonitor.exe to monitor the IIS service
ENTRYPOINT [ "C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc" ]
- The final stage MUST use a
- Build stage: Use a Windows Server Core image to build the application
- Be sure to consider all requirements in the containerization settings:
- Windows Server SKU and version
- Exposed ports
- User account for container
- IIS settings
- GAC assembly registration
- MSI installation
- COM component registration
- Registry keys
- Environment variables
- Windows roles and features
- File/directory copying
- Model the Dockerfile after the example provided at the end of this prompt, but ensure it is customized to the specific project requirements and settings.
- IMPORTANT: Use a Windows Server Core base image unless the user has specifically requested a full Windows Server image in the settings file
- The Dockerfile should use multiple stages:
- Create a file in the root of the project directory to exclude unnecessary files from the Docker image. The
.dockerignorefile MUST include at least the following elements as well as additional patterns as specified in the containerization settings:.dockerignore- packages/
- bin/
- obj/
- .dockerignore
- Dockerfile
- .git/
- .github/
- .vs/
- .vscode/
- **/node_modules/
- *.user
- *.suo
- **/.DS_Store
- **/Thumbs.db
- Any additional patterns specified in the containerization settings
- Configure health checks if specified in the settings:
- Add HEALTHCHECK instruction to Dockerfile if health check endpoint is provided
- Add the dockerfile to the project by adding the following item to the project file:
<None Include="Dockerfile" /> - Mark tasks as completed: [ ] → [✓]
- Continue until all tasks are complete and Docker build succeeds
- 查看上述容器化设置,明确容器化要求
- 创建文件,用于勾选标记跟踪变更
progress.md - 通过检查项目.csproj文件中的元素,确定.NET Framework版本
TargetFrameworkVersion - 根据以下条件选择合适的Windows Server容器镜像:
- 从项目中检测到的.NET Framework版本
- 容器化设置中指定的Windows Server SKU(Core或Full)
- 容器化设置中指定的Windows Server版本(2016、2019或2022)
- Windows Server Core镜像标签可在此查看:https://github.com/microsoft/dotnet-framework-docker/blob/main/README.aspnet.md#full-tag-listing
- 确保已安装所需的NuGet包。若包缺失,请勿自行安装。如果未安装,用户需手动安装。若未安装,暂停执行本指引,要求用户通过Visual Studio NuGet包管理器或Visual Studio包管理器控制台安装以下包:
Microsoft.Configuration.ConfigurationBuilders.Environment
- 修改文件,添加配置生成器部分和设置,以从环境变量中读取应用设置和连接字符串:
web.config- 在configSections中添加ConfigBuilders节
- 在根节点添加configBuilders节
- 为appSettings和connectionStrings配置EnvironmentConfigBuilder
- 示例格式:
xml
<configSections> <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" /> </configSections> <configBuilders> <builders> <add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" /> </builders> </configBuilders> <appSettings configBuilders="Environment"> <!-- 现有应用设置 --> </appSettings> <connectionStrings configBuilders="Environment"> <!-- 现有连接字符串 --> </connectionStrings>
- 在Dockerfile所在目录创建文件,复制本指引末尾的参考
LogMonitorConfig.json文件内容。除非容器化设置中有明确说明,否则该文件内容不得修改,必须与参考内容完全一致。LogMonitorConfig.json- 特别注意:不要修改日志记录的级别,因为对EventLog源使用级别会产生不必要的冗余日志。
Information
- 特别注意:不要修改日志记录的级别,因为对EventLog源使用
- 在项目根目录创建Dockerfile以将应用容器化
- Dockerfile应采用多阶段构建:
- 构建阶段:使用Windows Server Core镜像构建应用
- 除非设置文件中指定了自定义基础镜像,否则构建阶段必须使用基础镜像
mcr.microsoft.com/dotnet/framework/sdk - 先复制sln、csproj和packages.config文件
- 若存在NuGet.config则复制,并配置任何私有源
- 还原NuGet包
- 然后复制其余源代码,使用MSBuild构建并发布应用到C:\publish
- 除非设置文件中指定了自定义基础镜像,否则构建阶段必须使用
- 最终阶段:使用选定的Windows Server镜像运行应用
- 除非设置文件中指定了自定义基础镜像,否则最终阶段必须使用基础镜像
mcr.microsoft.com/dotnet/framework/aspnet - 将文件复制到容器中的某个目录(例如:C:\LogMonitor)
LogMonitorConfig.json - 从Microsoft仓库下载LogMonitor.exe到同一目录
- 正确的LogMonitor.exe下载链接为:https://github.com/microsoft/windows-container-tools/releases/download/v2.1.1/LogMonitor.exe
- 将工作目录设置为C:\inetpub\wwwroot
- 将构建阶段的发布输出(位于C:\publish)复制到最终镜像
- 设置容器的入口点,运行LogMonitor.exe和ServiceMonitor.exe以监控IIS服务
ENTRYPOINT [ "C:\\LogMonitor\\LogMonitor.exe", "C:\\ServiceMonitor.exe", "w3svc" ]
- 除非设置文件中指定了自定义基础镜像,否则最终阶段必须使用
- 构建阶段:使用Windows Server Core镜像构建应用
- 务必考虑容器化设置中的所有要求:
- Windows Server SKU和版本
- 暴露的端口
- 容器运行用户账户
- IIS设置
- GAC程序集注册
- MSI安装
- COM组件注册
- 注册表项
- 环境变量
- Windows角色和功能
- 文件/目录复制
- 以本指引末尾提供的示例Dockerfile为模板,但需根据具体项目需求和设置进行定制。
- 重要提示: 除非用户在设置文件中明确要求使用完整Windows Server镜像,否则请使用Windows Server Core基础镜像
- Dockerfile应采用多阶段构建:
- 在项目根目录创建文件,以排除Docker镜像中不必要的文件。
.dockerignore文件必须至少包含以下元素,以及容器化设置中指定的额外模式:.dockerignore- packages/
- bin/
- obj/
- .dockerignore
- Dockerfile
- .git/
- .github/
- .vs/
- .vscode/
- **/node_modules/
- *.user
- *.suo
- **/.DS_Store
- **/Thumbs.db
- 容器化设置中指定的任何额外模式
- 如果设置中指定了健康检查,则进行配置:
- 若提供了健康检查端点,在Dockerfile中添加HEALTHCHECK指令
- 通过在项目文件中添加以下项,将dockerfile添加到项目中:
<None Include="Dockerfile" /> - 将任务标记为已完成:[ ] → [✓]
- 持续执行直到所有任务完成且Docker构建成功
Build and Runtime Verification
构建与运行时验证
confirm that Docker build succeeds once the Dockerfile is completed. Use the following command to build the Docker image:
bash
docker build -t aspnet-app:latest .If the build fails, review the error messages and make necessary adjustments to the Dockerfile or project configuration. Report success/failure.
Dockerfile完成后,确认Docker构建成功。使用以下命令构建Docker镜像:
bash
docker build -t aspnet-app:latest .如果构建失败,查看错误信息并对Dockerfile或项目配置进行必要调整。报告构建成功/失败情况。
Progress Tracking
进度跟踪
Maintain a file with the following structure:
progress.mdmarkdown
undefined维护一个文件,结构如下:
progress.mdmarkdown
undefinedContainerization Progress
容器化进度
Environment Detection
环境检测
- .NET Framework version detection (version: ___)
- Windows Server SKU selection (SKU: ___)
- Windows Server version selection (Version: ___)
- .NET Framework版本检测(版本:___)
- Windows Server SKU选择(SKU:___)
- Windows Server版本选择(版本:___)
Configuration Changes
配置变更
- Web.config modifications for configuration builders
- NuGet package source configuration (if applicable)
- Copy LogMonitorConfig.json and adjust if required by settings
- 为配置生成器修改Web.config
- NuGet包源配置(如适用)
- 复制LogMonitorConfig.json并根据设置调整(如需要)
Containerization
容器化
- Dockerfile creation
- .dockerignore file creation
- Build stage created with SDK image
- sln, csproj, packages.config, and (if applicable) NuGet.config copied for package restore
- Runtime stage created with runtime image
- Non-root user configuration
- Dependency handling (GAC, MSI, COM, registry, additional files, etc.)
- Health check configuration (if applicable)
- Special requirements implementation
- 创建Dockerfile
- 创建.dockerignore文件
- 使用SDK镜像创建构建阶段
- 复制sln、csproj、packages.config以及(如适用)NuGet.config以还原包
- 使用运行时镜像创建运行阶段
- 非根用户配置
- 依赖项处理(GAC、MSI、COM、注册表、额外文件等)
- 健康检查配置(如适用)
- 特殊需求实现
Verification
验证
- Review containerization settings and make sure that all requirements are met
- Docker build success
Do not pause for confirmation between steps. Continue methodically until the application has been containerized and Docker build succeeds.
**YOU ARE NOT DONE UNTIL ALL CHECKBOXES ARE MARKED!** This includes building the Docker image successfully and addressing any issues that arise during the build process.- 检查容器化设置,确保所有要求均已满足
- Docker构建成功
步骤间无需暂停等待确认。按步骤执行,直到应用完成容器化且Docker构建成功。
**所有复选框标记完成前,任务不算结束!** 这包括成功构建Docker镜像,以及解决构建过程中出现的任何问题。Reference Materials
参考资料
Example Dockerfile
示例Dockerfile
An example Dockerfile for an ASP.NET (.NET Framework) application using a Windows Server Core base image.
dockerfile
undefined以下是使用Windows Server Core基础镜像的ASP.NET (.NET Framework)应用示例Dockerfile。
dockerfile
undefinedescape=`
escape=`
The escape directive changes the escape character from \ to `
escape指令将转义字符从\改为`
This is especially useful in Windows Dockerfiles where \ is the path separator
这在Windows Dockerfile中尤其有用,因为\是路径分隔符
============================================================
============================================================
Stage 1: Build and publish the application
阶段1:构建并发布应用
============================================================
============================================================
Base Image - Select the appropriate .NET Framework version and Windows Server Core version
基础镜像 - 选择合适的.NET Framework版本和Windows Server Core版本
Possible tags include:
可用标签包括:
- 4.8.1-windowsservercore-ltsc2025 (Windows Server 2025)
- 4.8.1-windowsservercore-ltsc2025(Windows Server 2025)
- 4.8-windowsservercore-ltsc2022 (Windows Server 2022)
- 4.8-windowsservercore-ltsc2022(Windows Server 2022)
- 4.8-windowsservercore-ltsc2019 (Windows Server 2019)
- 4.8-windowsservercore-ltsc2019(Windows Server 2019)
- 4.8-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.8-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7.2-windowsservercore-ltsc2019 (Windows Server 2019)
- 4.7.2-windowsservercore-ltsc2019(Windows Server 2019)
- 4.7.2-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7.2-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7.1-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7.1-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7-windowsservercore-ltsc2016(Windows Server 2016)
- 4.6.2-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.6.2-windowsservercore-ltsc2016(Windows Server 2016)
- 3.5-windowsservercore-ltsc2025 (Windows Server 2025)
- 3.5-windowsservercore-ltsc2025(Windows Server 2025)
- 3.5-windowsservercore-ltsc2022 (Windows Server 2022)
- 3.5-windowsservercore-ltsc2022(Windows Server 2022)
- 3.5-windowsservercore-ltsc2019 (Windows Server 2019)
- 3.5-windowsservercore-ltsc2019(Windows Server 2019)
- 3.5-windowsservercore-ltsc2019 (Windows Server 2016)
- 3.5-windowsservercore-ltsc2019(Windows Server 2016)
Uses the .NET Framework SDK image for building the application
使用.NET Framework SDK镜像构建应用
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2022 AS build
ARG BUILD_CONFIGURATION=Release
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2022 AS build
ARG BUILD_CONFIGURATION=Release
Set the default shell to PowerShell
将默认shell设置为PowerShell
SHELL ["powershell", "-command"]
WORKDIR /app
SHELL ["powershell", "-command"]
WORKDIR /app
Copy the solution and project files
复制解决方案和项目文件
COPY YourSolution.sln .
COPY YourProject/.csproj ./YourProject/
COPY YourOtherProject/.csproj ./YourOtherProject/
COPY YourSolution.sln .
COPY YourProject/.csproj ./YourProject/
COPY YourOtherProject/.csproj ./YourOtherProject/
Copy packages.config files
复制packages.config文件
COPY YourProject/packages.config ./YourProject/
COPY YourOtherProject/packages.config ./YourOtherProject/
COPY YourProject/packages.config ./YourProject/
COPY YourOtherProject/packages.config ./YourOtherProject/
Restore NuGet packages
还原NuGet包
RUN nuget restore YourSolution.sln
RUN nuget restore YourSolution.sln
Copy source code
复制源代码
COPY . .
COPY . .
Perform custom pre-build steps here, if needed
在此处执行自定义预构建步骤(如需要)
Build and publish the application to C:\publish
构建并发布应用到C:\publish
RUN msbuild /p:Configuration=$BUILD_CONFIGURATION
/p:PublishUrl=C:\publish `
/p:DeployDefaultTarget=WebPublish
/p:WebPublishMethod=FileSystemRUN msbuild /p:Configuration=$BUILD_CONFIGURATION
/p:PublishUrl=C:\publish `
/p:DeployDefaultTarget=WebPublish
/p:WebPublishMethod=FileSystemPerform custom post-build steps here, if needed
在此处执行自定义后构建步骤(如需要)
============================================================
============================================================
Stage 2: Final runtime image
阶段2:最终运行时镜像
============================================================
============================================================
Base Image - Select the appropriate .NET Framework version and Windows Server Core version
基础镜像 - 选择合适的.NET Framework版本和Windows Server Core版本
Possible tags include:
可用标签包括:
- 4.8.1-windowsservercore-ltsc2025 (Windows Server 2025)
- 4.8.1-windowsservercore-ltsc2025(Windows Server 2025)
- 4.8-windowsservercore-ltsc2022 (Windows Server 2022)
- 4.8-windowsservercore-ltsc2022(Windows Server 2022)
- 4.8-windowsservercore-ltsc2019 (Windows Server 2019)
- 4.8-windowsservercore-ltsc2019(Windows Server 2019)
- 4.8-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.8-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7.2-windowsservercore-ltsc2019 (Windows Server 2019)
- 4.7.2-windowsservercore-ltsc2019(Windows Server 2019)
- 4.7.2-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7.2-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7.1-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7.1-windowsservercore-ltsc2016(Windows Server 2016)
- 4.7-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.7-windowsservercore-ltsc2016(Windows Server 2016)
- 4.6.2-windowsservercore-ltsc2016 (Windows Server 2016)
- 4.6.2-windowsservercore-ltsc2016(Windows Server 2016)
- 3.5-windowsservercore-ltsc2025 (Windows Server 2025)
- 3.5-windowsservercore-ltsc2025(Windows Server 2025)
- 3.5-windowsservercore-ltsc2022 (Windows Server 2022)
- 3.5-windowsservercore-ltsc2022(Windows Server 2022)
- 3.5-windowsservercore-ltsc2019 (Windows Server 2019)
- 3.5-windowsservercore-ltsc2019(Windows Server 2019)
- 3.5-windowsservercore-ltsc2019 (Windows Server 2016)
- 3.5-windowsservercore-ltsc2019(Windows Server 2016)
Uses the .NET Framework ASP.NET image for running the application
使用.NET Framework ASP.NET镜像运行应用
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2022
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2022
Set the default shell to PowerShell
将默认shell设置为PowerShell
SHELL ["powershell", "-command"]
WORKDIR /inetpub/wwwroot
SHELL ["powershell", "-command"]
WORKDIR /inetpub/wwwroot
Copy from build stage
从构建阶段复制内容
COPY --from=build /publish .
COPY --from=build /publish .
Add any additional environment variables needed for your application (uncomment and modify as needed)
添加应用所需的其他环境变量(根据需要取消注释并修改)
ENV KEY=VALUE
ENV KEY=VALUE
Install MSI packages (uncomment and modify as needed)
安装MSI包(根据需要取消注释并修改)
COPY ./msi-installers C:/Installers
COPY ./msi-installers C:/Installers
RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'
RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'
Install custom Windows Server roles and features (uncomment and modify as needed)
安装自定义Windows Server角色和功能(根据需要取消注释并修改)
RUN dism /Online /Enable-Feature /FeatureName:YOUR-FEATURE-NAME
RUN dism /Online /Enable-Feature /FeatureName:YOUR-FEATURE-NAME
Add additional Windows features (uncomment and modify as needed)
添加额外Windows功能(根据需要取消注释并修改)
RUN Add-WindowsFeature Some-Windows-Feature; `
RUN Add-WindowsFeature Some-Windows-Feature; `
Add-WindowsFeature Another-Windows-Feature
Add-WindowsFeature Another-Windows-Feature
Install MSI packages if needed (uncomment and modify as needed)
安装MSI包(根据需要取消注释并修改)
COPY ./msi-installers C:/Installers
COPY ./msi-installers C:/Installers
RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'
RUN Start-Process -Wait -FilePath 'msiexec.exe' -ArgumentList '/i', 'C:\Installers\your-package.msi', '/quiet', '/norestart'
Register assemblies in GAC if needed (uncomment and modify as needed)
在GAC中注册程序集(根据需要取消注释并修改)
COPY ./assemblies C:/Assemblies
COPY ./assemblies C:/Assemblies
RUN C:\Windows\Microsoft.NET\Framework64\v4.0.30319\gacutil -i C:/Assemblies/YourAssembly.dll
RUN C:\Windows\Microsoft.NET\Framework64\v4.0.30319\gacutil -i C:/Assemblies/YourAssembly.dll
Register COM components if needed (uncomment and modify as needed)
注册COM组件(根据需要取消注释并修改)
COPY ./com-components C:/Components
COPY ./com-components C:/Components
RUN regsvr32 /s C:/Components/YourComponent.dll
RUN regsvr32 /s C:/Components/YourComponent.dll
Add registry keys if needed (uncomment and modify as needed)
添加注册表项(根据需要取消注释并修改)
RUN New-Item -Path 'HKLM:\Software\YourApp' -Force; `
RUN New-Item -Path 'HKLM:\Software\YourApp' -Force; `
Set-ItemProperty -Path 'HKLM:\Software\YourApp' -Name 'Setting' -Value 'Value'
Set-ItemProperty -Path 'HKLM:\Software\YourApp' -Name 'Setting' -Value 'Value'
Configure IIS settings if needed (uncomment and modify as needed)
配置IIS设置(根据需要取消注释并修改)
RUN Import-Module WebAdministration; `
RUN Import-Module WebAdministration; `
Set-ItemProperty 'IIS:\AppPools\DefaultAppPool' -Name somePropertyName -Value 'SomePropertyValue'; `
Set-ItemProperty 'IIS:\AppPools\DefaultAppPool' -Name somePropertyName -Value 'SomePropertyValue'; `
Set-ItemProperty 'IIS:\Sites\Default Web Site' -Name anotherPropertyName -Value 'AnotherPropertyValue'
Set-ItemProperty 'IIS:\Sites\Default Web Site' -Name anotherPropertyName -Value 'AnotherPropertyValue'
Expose necessary ports - By default, IIS uses port 80
暴露必要端口 - IIS默认使用80端口
EXPOSE 80
EXPOSE 80
EXPOSE 443 # Uncomment if using HTTPS
EXPOSE 443 # 若使用HTTPS则取消注释
Copy LogMonitor from the microsoft/windows-container-tools repository
从microsoft/windows-container-tools仓库复制LogMonitor
WORKDIR /LogMonitor
RUN curl -fSLo LogMonitor.exe https://github.com/microsoft/windows-container-tools/releases/download/v2.1.1/LogMonitor.exe
WORKDIR /LogMonitor
RUN curl -fSLo LogMonitor.exe https://github.com/microsoft/windows-container-tools/releases/download/v2.1.1/LogMonitor.exe
Copy LogMonitorConfig.json from local files
从本地文件复制LogMonitorConfig.json
COPY LogMonitorConfig.json .
COPY LogMonitorConfig.json .
Set non-administrator user
设置非管理员用户
USER ContainerUser
USER ContainerUser
Override the container's default entry point to take advantage of the LogMonitor
覆盖容器默认入口点,以使用LogMonitor
ENTRYPOINT [ "C:\LogMonitor\LogMonitor.exe", "C:\ServiceMonitor.exe", "w3svc" ]
undefinedENTRYPOINT [ "C:\LogMonitor\LogMonitor.exe", "C:\ServiceMonitor.exe", "w3svc" ]
undefinedAdapting this Example
示例适配说明
Note: Customize this template based on the specific requirements in the containerization settings.
When adapting this example Dockerfile:
- Replace ,
YourSolution.sln, etc. with your actual file namesYourProject.csproj - Adjust the Windows Server and .NET Framework versions as needed
- Modify the dependency installation steps based on your requirements and remove any unnecessary ones
- Add or remove stages as needed for your specific workflow
注意: 请根据容器化设置中的具体要求定制此模板。
适配示例Dockerfile时:
- 将、
YourSolution.sln等替换为实际文件名YourProject.csproj - 根据需要调整Windows Server和.NET Framework版本
- 根据需求修改依赖项安装步骤,移除不必要的步骤
- 根据具体工作流程添加或移除构建阶段
Notes on Stage Naming
阶段命名说明
- The syntax gives each stage a name
AS stage-name - Use to copy files from a previous stage
--from=stage-name - You can have multiple intermediate stages that aren't used in the final image
- 语法为每个阶段指定名称
AS stage-name - 使用从之前的阶段复制文件
--from=stage-name - 可以包含多个不用于最终镜像的中间阶段
LogMonitorConfig.json
LogMonitorConfig.json
The LogMonitorConfig.json file should be created in the root of the project directory. It is used to configure the LogMonitor tool, which monitors logs in the container. The contents of this file should look exactly like this to ensure proper logging functionality:
json
{
"LogConfig": {
"sources": [
{
"type": "EventLog",
"startAtOldestRecord": true,
"eventFormatMultiLine": false,
"channels": [
{
"name": "system",
"level": "Warning"
},
{
"name": "application",
"level": "Error"
}
]
},
{
"type": "File",
"directory": "c:\\inetpub\\logs",
"filter": "*.log",
"includeSubdirectories": true,
"includeFileNames": false
},
{
"type": "ETW",
"eventFormatMultiLine": false,
"providers": [
{
"providerName": "IIS: WWW Server",
"providerGuid": "3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83",
"level": "Information"
},
{
"providerName": "Microsoft-Windows-IIS-Logging",
"providerGuid": "7E8AD27F-B271-4EA2-A783-A47BDE29143B",
"level": "Information"
}
]
}
]
}
}LogMonitorConfig.json文件应创建在项目根目录,用于配置LogMonitor工具以监控容器内的日志。该文件内容必须与以下内容完全一致,以确保日志功能正常:
json
{
"LogConfig": {
"sources": [
{
"type": "EventLog",
"startAtOldestRecord": true,
"eventFormatMultiLine": false,
"channels": [
{
"name": "system",
"level": "Warning"
},
{
"name": "application",
"level": "Error"
}
]
},
{
"type": "File",
"directory": "c:\\inetpub\\logs",
"filter": "*.log",
"includeSubdirectories": true,
"includeFileNames": false
},
{
"type": "ETW",
"eventFormatMultiLine": false,
"providers": [
{
"providerName": "IIS: WWW Server",
"providerGuid": "3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83",
"level": "Information"
},
{
"providerName": "Microsoft-Windows-IIS-Logging",
"providerGuid": "7E8AD27F-B271-4EA2-A783-A47BDE29143B",
"level": "Information"
}
]
}
]
}
}